Jump to content

  • Log In with Google      Sign In   
  • Create Account


My program seems to have a memory leak, but there's absolutely no explanation.


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
12 replies to this topic

#1 Nyxenon   Members   -  Reputation: 104

Like
0Likes
Like

Posted 22 February 2012 - 11:50 PM

I'm trying to make a game, but while looking at the Task manager, I discovered that the memory for my program was increasing with no apparent cause. I decided to stip out any code that might be allocating anything, and I STILL have the memory increasing.

Here's the loop that seems to be what is causing the leak. I can't say it's really a memory leak, but I can't think of any other reason why the memory would continuously increase.

while(!done)		 // Loop That Runs While done=FALSE
{
  while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  {
   if(msg.message == WM_QUIT)
   {
    done = true;
   }
   else
   {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
   }
  }
  // Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
  if (active && !Tick()/*Replace this with Tick()*/) // Active?  Was There A Quit Received?
  {
   done=true;	   // ESC or DrawGLScene Signalled A Quit
  }
  else		 // Not Time To Quit, Update Screen
  {
   SwapBuffers(hDC);	 // Swap Buffers (Double Buffering)
  }
}

The code for "Tick()" is here:

int Tick()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
return 1;
}

I just copied some of the code from one of NeHe's tutorials.

Is it possible that I have a memory leak, or is it normal for the memory to do this?

Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6966

Like
0Likes
Like

Posted 22 February 2012 - 11:55 PM

How much is it increasing by? Because the code you posted certainly isn't leaking memory.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#3 Nyxenon   Members   -  Reputation: 104

Like
0Likes
Like

Posted 22 February 2012 - 11:59 PM

How much is it increasing by? Because the code you posted certainly isn't leaking memory.


In task manager, it goes from "5364 K" to "5368 K" in about 10 seconds, then it continues increasing at that rate, I haven't kept it running for long enough to see how high it gets, but I've let it get as high as "6000 K" and it was still going. I don't know if it's normal for the memory to do that, but it's never done that before. I would post all the source code, but I know that nothing else could be causing the problem because everything else is only called, right before that loop.

#4 Cornstalks   Crossbones+   -  Reputation: 6966

Like
0Likes
Like

Posted 23 February 2012 - 12:10 AM


How much is it increasing by? Because the code you posted certainly isn't leaking memory.


In task manager, it goes from "5364 K" to "5368 K" in about 10 seconds, then it continues increasing at that rate, I haven't kept it running for long enough to see how high it gets, but I've let it get as high as "6000 K" and it was still going. I don't know if it's normal for the memory to do that, but it's never done that before. I would post all the source code, but I know that nothing else could be causing the problem because everything else is only called, right before that loop.

Meh, I wouldn't worry about it. An increase of 2K over a period of 10 seconds isn't much to worry about. The thing is, the task manager isn't very good for finding and detecting memory leaks. It shoes how much memory the OS has had to set aside for your application to run, which may not accurately represent how much memory you've allocated yourself (i.e. you've allocated a few megabytes, but then the OS has to use an additional extra few megabytes (and this may fluctuate) to manage your program).

If I were you, I'd just make sure to be using smart pointers where possible, and also get familiar at some point with some memory leak detector at some point (there are different ones that can be used depending on your compiler/IDE that can give you some nice reports of what memory leaked).
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#5 mhagain   Crossbones+   -  Reputation: 7417

Like
1Likes
Like

Posted 23 February 2012 - 12:22 AM

That's not a memory leak, that's normal operation of OpenGL: http://www.opengl.or...AQ#Memory_Usage

To be honest, worrying about memory leaks and monitoring memory usage at this early stage in a program is incredibly premature. Wait till you start creating some objects and allocating some memory yourself. Even then it's too early to worry about it.

The best memory leak detector is reading documentation and understanding how things are supposed to work. False positives like this are much more dangerous and time-consuming than tracking a real leak.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#6 Nyxenon   Members   -  Reputation: 104

Like
0Likes
Like

Posted 23 February 2012 - 12:35 AM

That's not a memory leak, that's normal operation of OpenGL: http://www.opengl.or...AQ#Memory_Usage

To be honest, worrying about memory leaks and monitoring memory usage at this early stage in a program is incredibly premature. Wait till you start creating some objects and allocating some memory yourself. Even then it's too early to worry about it.

The best memory leak detector is reading documentation and understanding how things are supposed to work. False positives like this are much more dangerous and time-consuming than tracking a real leak.


I'm not exactly early, the code I provided was backed up code because I thought I had a memory leak with a list class that I wrote. Thanks for the help, Cornstalks and mhagain.

#7 Antheus   Members   -  Reputation: 2393

Like
1Likes
Like

Posted 23 February 2012 - 08:39 AM

Task manager doesn't report useful memory usage. It reports number of memory pages requested by process, which is only loosely correlated with leaks.

Or vice versa - a leak will cause number of pages to increase, but it's not the only reason for that number to increase.

I thought I had a memory leak with a list class that I wrote


That's quite possible, but it's not the code that was posted above.

#8 iMalc   Crossbones+   -  Reputation: 2258

Like
0Likes
Like

Posted 23 February 2012 - 12:25 PM

"Task Manager", oh that anchient piece of crap. Every self-respecting developer on Windows uses Process Explorer from Sysinternals.
Heck it's the first thing I install after the OS, I kid you not.
Once you're tried it, you'll never go back!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

#9 NightCreature83   Crossbones+   -  Reputation: 2670

Like
0Likes
Like

Posted 23 February 2012 - 02:18 PM

"Task Manager", oh that anchient piece of crap. Every self-respecting developer on Windows uses Process Explorer from Sysinternals.
Heck it's the first thing I install after the OS, I kid you not.
Once you're tried it, you'll never go back!

You


That's not a memory leak, that's normal operation of OpenGL: http://www.opengl.or...AQ#Memory_Usage

To be honest, worrying about memory leaks and monitoring memory usage at this early stage in a program is incredibly premature. Wait till you start creating some objects and allocating some memory yourself. Even then it's too early to worry about it.

The best memory leak detector is reading documentation and understanding how things are supposed to work. False positives like this are much more dangerous and time-consuming than tracking a real leak.


I'm not exactly early, the code I provided was backed up code because I thought I had a memory leak with a list class that I wrote. Thanks for the help, Cornstalks and mhagain.


Have a look at Visual Leak Detector as a memory leak detector, it gives you callstacks for where the allocations have been made. This will only work if you arent using your own memory manager, it hooks into the VC runtime allocator to figure everything out, but that is true for most leak detectors. You just have to include one header in the main cpp of your app and link against the provided lib file, as a nice added benefit you don't have to remove the header declaration for release builds the header takes care of not being compiled with the code in release.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#10 Potatoman   Members   -  Reputation: 108

Like
0Likes
Like

Posted 24 February 2012 - 09:31 PM

I can't remember the exact syntax, but there are _Crt methods to dump all the allocations since a certain checkpoint. I'm not sure I'd trust task manager to provide the information I need.

_CrtDumpMemoryLeaks is one to try, for picking up true leaks - but it's also possible some code is growing an array (which gets cleaned up on shutdown), which is just as bad. So in this case use the _CrtMemCheckpoint methods to see get a list of all new heap allocations since the last call.

See this page for details http://www.codeguru.com/forum/showthread.php?t=312742

#11 diventurer   Members   -  Reputation: 771

Like
1Likes
Like

Posted 24 February 2012 - 10:51 PM

while(!done)             // Loop That Runs While done=FALSE



Posted ImageSorry. Just had to :)

#12 Martins Mozeiko   Crossbones+   -  Reputation: 1413

Like
0Likes
Like

Posted 25 February 2012 - 12:38 AM

Every self-respecting developer on Windows uses Process Explorer from Sysinternals.

Or ProcessHacker that does the same things and much more. And it's open-source!

#13 iMalc   Crossbones+   -  Reputation: 2258

Like
0Likes
Like

Posted 26 February 2012 - 11:39 PM

Every self-respecting developer on Windows uses Process Explorer from Sysinternals.

Or ProcessHacker that does the same things and much more. And it's open-source!

Thanks, I didn't know about that one!

It looks pretty good, but alas as well as having more in some places it also has slightly less in others.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms




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