[galib] How to delete a genome
adam at nohejl.name
Thu Apr 9 05:30:58 EDT 2009
> Hi everybody.
> I have a probably very stupid question. In my program I need to launch
> sequentially many GA, so I would like to delete my old genome and ga
> free some memory. for example I use
I think that your question is related more to C++ than to galib
itself. If you are new to C++, read at least something about
constructors, destructors and memory management.
You can try this:
and if you still feel lost, get a general book about C++.
> GABin2DecGenome genome(map, Objective, (MyObject *) Object);
> GASimpleGA ga(genome);
In this case, the destructors are called automatically when the
variables go out of scope (i.e. at the end of the block where they
were declared). This should also answer your primary question about
how to reclaim memory.
Never call destructors explicitly (unless you know what you are doing).
> Anyway, my program always breaks down telling me
> "Unhandled exception at 0x004040f5 in MyProgram.exe: 0xC0000005:
> violation reading location 0xdddddddd."
That's probably because the destructor is called twice, once by your
explicit call, once automatically at the end of the enclosing block.
> Also, I tried to use these expression
> delete genome;
> delete ga;
1. Use delete only when you allocate objects using the new oprator.
2. Even better, if you decide for dynamic memory allocation, use smart
pointers and avoid the explicit use of delete. (Look for RAII.)
I hope this helps,
More information about the galib