Sign in to follow this  
Geometrian

Memory Leak from Unrun Code

Recommended Posts

Geometrian    1810

Hi,
 
I've been running into a number of problems of a horribly annoying form. The general story looks like this:
 
--I run the program (developing on MSVC 2010, using CRT for memory leak checking)
--Oh noes! A memory leak!
--Nothing is obvious. Removing files/recompiling eventually tracks it down to one line in a particular file. When this line is commented, no leak. Uncommented, leak.
--But this line is never ever called. Ever. Using a debugger and putting it right before, it's obvious that it isn't.
 
How can a line that is never called cause a memory leak? I've made some silly conjectures, but really I don't know the answer.
 
Code should not be terribly relevant, but FWIW, the line that's causing the issue this time is:

menu = new QMenu();

QMenu comes from Qt 4.8.4. I've had problems with Bullet Physics though too.


What could be causing this? Thanks,
Ian

Share this post


Link to post
Share on other sites
Khatharr    8812

All you've told us about is the problem itself. Without more context we have no way of knowing what's causing it.

 

If the code is indeed not being run then obviously it's not directly leaking memory, but it may be something else in that compilation unit that's screwing up the heap and removing that line simply hides the problem.

Edited by Khatharr

Share this post


Link to post
Share on other sites
EWClay    659
That line does look kind of leaky though. Is menu deleted properly? Are you sure the line is not being run? Debuggers can lie sometimes. Try a full rebuild and make sure to check it in debug mode.

Share this post


Link to post
Share on other sites
TheChubu    9447

I have no idea about MSVS workings but it could be that the memory leak detector just points to a place where memory could be allocated and not de-allocated? ie, potential memory leak rather than "this is leaking while the program is running".

Share this post


Link to post
Share on other sites
Khatharr    8812

The default MSVC debugger just says that there's a leak and I think gives the address of the allocation. I'd have to mess with it to see but *effort*...

 

If you're concerned about leaks and don't want to do it the 'right' way (using STL) then you may want to check out VLD, which will give you more useful info when a leak is detected:

 

http://vld.codeplex.com/

Share this post


Link to post
Share on other sites
C0lumbo    4411

My guess would be that with your line commented out the compiler is able to dead-strip swathes or even the entirety of the qt library from your project.

 

The memory leak itself might be resulting from some global constructor within the qt library that gets called when your line is present, but is stripped out when the line is not present.

 

That's just a guess though. I'd recommend you learn the tools necessary to track down memory leaks more directly rather than through trial and error. If your memory leak is reported then presumably there's some associated information, if it's only the address, then you might have some luck with a hardware breakpoint on that address of the leak which may shed some light.

Share this post


Link to post
Share on other sites
Geometrian    1810

That line does look kind of leaky though. Is menu deleted properly? Are you sure the line is not being run? Debuggers can lie sometimes. Try a full rebuild and make sure to check it in debug mode.

If you want, put some messagebox before and after the call to the constructor and see if the code is run.

If it were run, the menu would be deleted properly--but I'm know that I'm never calling the method. I can put the "delete menu;" immediately after the allocation with the same results. "printf" shows that the code is in fact not being run (yes, being careful to flush IO buffers too).

 

you may want to check out VLD, which will give you more useful info when a leak is detected

I've used VLD before, and I've found that it sometimes doesn't pick up memory leaks. In this case, it reports "No memory leaks detected." on exit.

Actually, the problem isn't reproducible in a separate example, which, as I mentioned, points to it being some kind of problem within the application itself. I just don't know exactly what kinds of problems to look for. This:

My guess would be that with your line commented out the compiler is able to dead-strip swathes or even the entirety of the qt library from your project.

The memory leak itself might be resulting from some global constructor within the qt library that gets called when your line is present, but is stripped out when the line is not present.

That's just a guess though. I'd recommend you learn the tools necessary to track down memory leaks more directly rather than through trial and error. If your memory leak is reported then presumably there's some associated information, if it's only the address, then you might have some luck with a hardware breakpoint on that address of the leak which may shed some light.

. . . was kindof my guess, but as above the fact that the problem isn't reproduced in a simple example would seem to suggest otherwise.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this