Memory management dilemma
Rémy Mouëza
Rémy_member at pathlink.com
Sun May 28 08:05:15 PDT 2006
In article <e5c89d$6ki$2 at digitaldaemon.com>, Jeff Parsons says...
>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.
I discussed about similar concerns with a friend of mine: is a C programmer and
think nothing is better than C. He discovered that D is binary compatible with C
and we're planning to collaborate on a game engine. Memory management was (and
might still be) the first problem we needed to solve.
>[*] 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)?
Yes, there is a way: auto variable.
# Result aFunction ()
# {
# auto Matrix matrix = new Matrix ;
# // ... work with your matrix
# return result ;
# }
# // getting out of scope deletes
# // the auto allocated matrix.
You can also make an auto class :
auto class Matrix { ... }
All the instances of this class will be deleted when getting out of scope.
There are restriction in the use of auto variable : you can't return an auto
variable and... I've forgotten, they're due to the temporary nature of such
instances. See:
http://www.digitalmars.com/d/declaration.html#AutoDeclaration
http://www.digitalmars.com/d/class.html#deallocators, the section after
deallocators deals with auto classes.
There is also the scope ( exit ) {} statement that defer the execution of its
instructions at the exit of the current scope.
http://www.digitalmars.com/d/statement.html#scope
Less convenient than the auto variable.
>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?
People says lot of good things about Valgrind, a software that trace your memory
allocations. I don't know if it works for D.
More information about the Digitalmars-d
mailing list