Advertisement

memory leaks

Started by August 09, 2000 01:14 PM
14 comments, last by Joker2000 24 years, 5 months ago
Ok, I''m kinda confused on the whole issue of memory leaks. I mean, I always heard that once your program ends, all resources that it had in use would be freed. If this is the case, do memory leaks only occur while your program is running? --- Joker2000 Stevie Ray Vaughan - The Legend
If you malloc something in your program and point a pointer to it, if you do not free it before the pointer goes out of scope (eg when the program ends), some memory will be allocated and be not able to be freed. If the memory can''t be freed it is going to stay off limits to other programs until the computer is rebooted.

------------------------------
#pragma twice
Advertisement
Well, it''s true that all resources are released (most of the time--I''ve had some programs not let go of the sound card handle, so I couldn''t use the sound card without rebooting) once your programs end. It''s not like your RAM is forever dead and will never return. But, it''s poor form.

The reason memory leaks are bad is that if memory is allocated in a loop, and never freed, then eventually you will run out of memory and your program will crash. Therefore, we get in the habit of stomping out all memory leaks.
furby: that''s not true under windows. The OS keeps track of what memory the program has allocated, and frees it itself once the program exits. Otherwise, terminating a misbehaving program via ctl-alt-del would have no effect.
quote: Original post by Stoffel

furby: that''s not true under windows. The OS keeps track of what memory the program has allocated, and frees it itself once the program exits. Otherwise, terminating a misbehaving program via ctl-alt-del would have no effect.


I''m not so sure about that. DOS will free the memory.. windows will not..

Memory leaks are not uncommon on many operating systems. Many embedded operating systems may not free memory like some OS''s do. Its much better for "us" the developers when the OS free''s up memory, however this is not a required protocol when writing an OS. I work on an embedded operating system on network switches, and the OS does not free its memory when your done with it.

If you think about it, it is much more efficient if the OS does not have to baby sit our bad code techniques. It should be our job to clean them, the counter argument being that developers would be more productive if they didn''t have to chase memory leaks

My first suggestion is to *not trust* the operating system to free your memory in every given senario.

Secondly, a memory leak in a game can become a very large issue if it was a game like Civilization. Why? I can play Civ for five hours easy, I don''t know about you guys. If your not free''ing memory and your losing 10K to 1MB an hour or more, and someone plays for many hours there is a great risk of windows slapping your game in the face with a "Sorry, but I don''t have enough memory to run" type error. The other issue I''ve seen is a slowdown in OS efficiency when a game has a memory leak. Good, it gets free''d when your done playing....but what if you play for 10 hours straight? Or leave the system on pause and come back later? Bad bad bad.

Lastly, its bad form. Don''t do it if you can help it. Write a memory tool to check for non-free''d data (I''m writing one right now actually). Something that traces your ALLOC''s and FREE''s for missing pairs (3 malloc() calls with 1 free() call? Bad bad bad).

Hope that helps,

CodeMonkey

CODE Monkey
Advertisement
My crappy programs loose Memory All the Time, and Windows just laughs. How is windows supposed to know what memory you used, and if you free''d it or not. Just try it. Alocate 5 megs, then dont free it, just quit. Now run that Sys Monitor and see how much memory it says is free. I bet you its 5 megs less.
Oh, and furby100 is right
Hi,

like i mentioned a few times before in this NG there are tools
out to trace memory leaks and a lot of other weird stuff a programmer can produce. Have a look at RATIONAL ''s tool called PURIFY which i the best i think. There is a free 30 days evaluation copy avail on the web. This tool finds your "leaks" "and array bounds write" stuff and many other things. It''s absolutely easy to use. There is another Product from NuMega called BounceChecker which i have also used before.

For testing on the source code level there are tools available like (PC) lint, but this needs some more time to get it working.

Give it a try.

cu

Peter


HPH
Esap: Why don''t you try it? You''ll see that the memory is freed once your program terminates normally or abnmorally. How does Windows know what memory you allocated? Because every Windows program has their own private address space and everything you allocate is within that private address space. When your program is terminated, it releases that address space and everything you''ve allocated within that address space.

But there are OSes that don''t clean up memory very well and if you plan to port your program it is bad practice to rely on the OS, as others have said.

This topic is closed to new replies.

Advertisement