Skip to main content
GameDev.net gamedev.net
🔒 Locked

Malloc sans Free

Started by drown Jun 1, 2003 at 4:28 AM 13 replies 1.4k views
Original Post
drown
drown
Yes I am anal about freeing every drop of dynamic memory I happen to allocate after I''m done. However, while briefly browsing through the Quake3 tool source, I noticed that the program calls exit() without concern for any sort of deallocation with free(). Leading to some gray areas for me... Questions: 1. Does the Windows OS automatically deallocate all memory (allocated with malloc()) for a process after it exits without crash? How about with a crash? And if so... 2. What versions of Windows does this apply to? Does it also apply to any application types (ie. console, win32 app, etc.)? 3. This doesn''t apply to new/delete operators correct? I assume not calling delete after new is still a big no-no to avoid. Some insight is appreciated.
http://www.freewebs.com/somebodyshootme/
The Heretic
The Heretic
Nope... It''s up to you to do that. You misread the source code I''m sure because Carmack doesn''t make mistakes like that. Keep doing what you''re doing now.

BTW, it''s better to use new and delete if you''re not doing strict C because they are typesafe, init classes (if you use them), etc.
Prosper/LOADED
Prosper/LOADED
1. Any serious OS will free the memory used by a program once it''s killed, either by a normal exit or a crash.

2. In practice however, it doesn''t always work that well. NT based versions of Windows (NT/2k/XP) are ok with it but I''ve noticed several times that 9x based versions sometimes don''t properly release the memory, and you have to reboot your system once in a while. Since it''s pretty random and this can even append when you call free(), there''s not much you can do about it.

3. This apply to new/delete too, since they call malloc/free. However, although your memory will be freed, your object destructors won''t be automatically called. If you''re just freeing memory inside them it''s not that much of a problem (since it will be freed anyway), but it could be troublesome if you also do other things like writting to a log file in your destructor.
drown
drown
to prosper: yes, this is what I assumed but I just wanted to make sure and get some further info. thnks.

to heretic: hath I offended the god they call Carmack the Great?? the error function prints out a message then calls exit(1). this happens many times after allocation of memory. Misreading? I doubt it...
http://www.freewebs.com/somebodyshootme/
C-Junkie
C-Junkie
quote:
Original post by Prosper/LOADED
2. In practice however, it doesn''t always work that well. NT based versions of Windows (NT/2k/XP) are ok with it but I''ve noticed several times that 9x based versions sometimes don''t properly release the memory, and you have to reboot your system once in a while. Since it''s pretty random and this can even append when you call free(), there''s not much you can do about it.



This has to do with other flaws in the OS, and not malloc/free/new/delete.

You don''t need to worry about explicitly freeing malloc()''d memory if you''re exiting immediately.
antareus
antareus
quote:
Original post by C-Junkie
You don''t need to worry about explicitly freeing malloc()''d memory if you''re exiting immediately.

Which is why Carmack doesn''t, the exit points for errors are probably not where he can actually free() all the memory allocated, and he''s exiting with a non-zero exit code anyway, indicating failure.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
S1CA
S1CA
Any memory allocated from the process heaps (e.g. using malloc, new, HeapAlloc) will be released automatically by the OS becuase it''s all virtual memory and only has relevence to your process - all pages for your process will automatically be wiped out.

However there ARE other types of allocations that DO have a system wide effect that you should be careful about:

- handles & other objects from system DLLs. A very common bug people do is not restoring the default pen/brush etc when they''ve been doing GDI stuff - the internal refcounting means the memory and handle stays allocated inside the GDI. Under Windows 9x if you do that too much the machine grinds to a halt and you get a blue screen or white Windows 3.1 style error box informing you that the system is low on resources.

- kernel memory (i.e. real blocks of memory) - certain functions that use hardware cause the drivers for that hardware to do things like allocate kernel memory buffers (e.g. you can only DMA with real addresses and true contiguous memory so drivers have to allocate physical memory).

- external resources. For example DirectDraw surfaces etc (although in that case, Ddhelp tries to tidy up, and the driver resets the state when a new context is created).

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
jorgander
jorgander
not to change the subject or anything, but this is sorta related...

out of curiosity, does anyone know (or is there a correct way) to reset the resolution after your game/app has changed the res to something other than the desktop setting? one thing i hate, esp. when done by commercial-level games, is when i have to re-stretch the wallpaper after i''ve exitted a game.

maybe it''s something that i''m doing or some setting i have enabled/disabled, but as an example i *always* have to re-stretch the wallpaper after quitting command and conquer generals.
Qw3r7yU10p!
Qw3r7yU10p!
carmack isn''t the only programmer at id
DigitalDelusion
DigitalDelusion
quote:
Original post by petewood
carmack isn''t the only programmer at id


I really dubt that Carmack is that much of a *programmer* he''s
a genius alright, he solves problems and invent stuff noone
else could even have imagined, he probably writes the meanest
code ever, yup, he''s a god in that sense alright...

But ohh mhyyy ghoood he could also enter any obfuscation
contest with his regular source... and win!!!

Really as far as idols goes he''s one of greatest but
after trying to dechiper some of the stuff Id have released
Im quite confident that I wouldn''t want to try to maintain
or modify anything he have written...

HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
S1CA
S1CA
quote:
Original post by jorgander
not to change the subject or anything, but this is sorta related...

out of curiosity, does anyone know (or is there a correct way) to reset the resolution after your game/app has changed the res to something other than the desktop setting? one thing i hate, esp. when done by commercial-level games, is when i have to re-stretch the wallpaper after i''ve exitted a game.

maybe it''s something that i''m doing or some setting i have enabled/disabled, but as an example i *always* have to re-stretch the wallpaper after quitting command and conquer generals.


That sounds like the game in question using the desktop HWND as their parent (or even worse, main) window. i.e. a lame coding practice.

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
MDI
MDI
quote:

I really dubt that Carmack is that much of a *programmer* he''s
a genius alright, he solves problems and invent stuff noone
else could even have imagined, he probably writes the meanest
code ever, yup, he''s a god in that sense alright...



Not to sound too much of a cynic, but many of the techniques Carmack uses in his code are standard techniques / techniques invented by someone else that wrongly get attributed to him.

A recent example seems to be his fast inverse square root, which is a standard Newton Raphson method with a floating point hack apparently invented by some guy at NVidia.



This is the tale of a Northern Soul, looking to find his way back home
drown
drown
quote:
But ohh mhyyy ghoood he could also enter any obfuscation
contest with his regular source... and win!!!


agreed :D
http://www.freewebs.com/somebodyshootme/
Martee
Martee
quote:
Original post by Anonymous Poster
Windows is supposed to free all allocated memory on the heap and all the rest too. It doesn''t, we all now that.


We do?
quote:

A c++ memory q.
Allocataing a big hunch of memory with malloc and the handle the dynamic memory allocations yourself by taking parts of this pool can give large performance increases while using c. Is it possible to do this in c++?


It''s certainly possible to try ... whether or not you actually see any performance increase depends on your code and your usage scenarions. For starters, you should have a look at custom allocators, overloaded new/delete operators, and placement new.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.