Memory management dilemma
Jeff Parsons
jeffrparsons at optusnet.com.au
Sun May 28 06:23:58 PDT 2006
Hi
Straight to the point: I'm having trouble finding a clean way to make
sure memory that's no longer required by my game is freed.
I need to manage memory explicitly since it seems to be impossible to
guarantee long-term smooth operation when using only garbage collection.
I don't see introducing artificial pauses into gameplay during which
garbage is collected, or allowing the game to bloat horribly over time
as solutions.
Example of my problem:
In C++, I could pass my spatial Vector/Vector3D/whatever class, or
Matrix class by value. I could chuck it around in expressions happily,
etc. In D, I have a few concerns:
[*] I can see a LOT of manual "delete whatever" popping up if I'm
dealing with expressions that involve a lot of vector or matrix math. Is
there a way to avoid this (having locally created objects automatically
destroyed when they go out of scope)?
[*] If I explicitly copy my objects when passing them to functions and
explicitly destroy them at the end of the called function, won't this be
allocating a lot on the heap, and therefore be slow?
[*] How are temporaries handled if there's no garbage collector active?
For example, if part of an expression calls a.getB() which returns a
reference to one of a's members called b, does D know not to mess with
this reference when cleaning up temporaries? What if getB() assigned a
new object to b, but returned the original reference. How would it
behave then - would it know it's not safe to destroy it?
Is there a nice neat way to handle this in D or should I expect a lot of
pain when not using the garbage collector? And if I do go down this
path, how can I tell if I -am- leaking memory?
Any and all comments and/or suggestions are appreciated.
Thanks :)
---------
Footer for the bored:
I don't think I've posted here, so to quickly introduce myself, my name
is Jeff Parsons and I'm an undergraduate Computer Science student who
really doesn't want to go crawling back to C++ quite so soon. ;)
More information about the Digitalmars-d
mailing list