Jump to content

  • Log In with Google      Sign In   
  • Create Account

[С++] Memory leak checker


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
24 replies to this topic

#21 Dave   Members   -  Reputation: 1514

Like
0Likes
Like

Posted 29 November 2012 - 05:31 PM

This isn't exactly helpful for the purposes of a general purpose leak-detector, so feel free to disregard Posted Image

The way that I've seen most engine code-bases deal with these issues, is to simply ban the use of standard allocation routines (new/malloc/etc) as part of their coding guidelines. The only 'new' that is allowed is placement-new, and it's probably never used directly, but via an engine macro.
If you want memory, you get it from the engine's allocation API, which becomes the single place where tracking code needs to be inserted.


I agree, reducing memory leaks is best prevented than fixed later. It's an architecture design really.

Sponsor:

#22 AlexB.hpp   Members   -  Reputation: 201

Like
0Likes
Like

Posted 02 December 2012 - 06:28 AM

Damn it, I've stuck a little.

Meet cool bug when some containers of STL use my funcs and do stucking in infinite loop.

You know how it happens - you read about 1000 books of cpp and still doing this stupid mistakes in you code. :D
C x 2 C x o C x C f nice C x C s C x C c

#23 SiCrane   Moderators   -  Reputation: 9604

Like
1Likes
Like

Posted 02 December 2012 - 10:00 AM

Well if you want help with that one you should probably post some code that demonstrates the problem; it doesn't seem like you've checked that into your repository. However, of the code that you have updated in your repository I noticed that you updated your realloc() implementation, but it still does't handle null returns from realloc() properly, and that you've added exception specifications on some of your functions. Using C++ exception specifications is a bad idea.

#24 AlexB.hpp   Members   -  Reputation: 201

Like
0Likes
Like

Posted 02 December 2012 - 11:03 AM

Oh, many thanks again.

Looks like I've fixed the old one problem with infinite loop in stl. So... Actually I've done major work on it.

Ok I pretty sure that realloc is ok now.
It does;
new_ptr = realloc(old_ptr, size)
if (!new_ptr) return old_ptr
return new_ptr

I'll read stuff about exceptions. And figure out how it should be.

I have funny stuff as one of my friends said. I have calls of delete on unregistered ptrs :)
Try to erase unregistered pointer 0x82f10f8!
Memory leak summary:
	    operator new (0x82f1008) missed 4 bytes in main.cpp:5
	    operator new [] (0x82f1040) missed 4 bytes in main.cpp:6
	    malloc (0x82f1078) missed 4 bytes in main.cpp:7
	    calloc (0x82f10b0) missed 4 bytes in main.cpp:8
	    realloc (0x82f10e8) missed 8 bytes in main.cpp:10
Total leaked 24 bytes
Try to erase unregistered pointer 0x82f10f8!
Try to erase unregistered pointer 0x82f10c0!
Try to erase unregistered pointer 0x82f1088!
Try to erase unregistered pointer 0x82f1050!
Try to erase unregistered pointer 0x82f1018!

Edited by AlexB.hpp, 02 December 2012 - 11:04 AM.

C x 2 C x o C x C f nice C x C s C x C c

#25 SiCrane   Moderators   -  Reputation: 9604

Like
1Likes
Like

Posted 02 December 2012 - 11:31 AM

realloc() is still wrong. What happens when realloc() is given an existing buffer to expand, but there isn't enough room to expand it? It leaves the existing memory allocation alone and returns null. What happens when your function encounters that situation? It marks the memory block as freed and then returns a non-null pointer signalling that the resize succeeded. So not only do you have an allocated memory block that won't show up in your leak tracker, you've just told the user that his memory block is larger than it actually is, so he probably is going to write past the end of allocated memory.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS