Re: De-alloc Memory

Rene Brun (Rene.Brun@cern.ch)
Sat, 05 Dec 1998 10:27:02 +0100


Hi Jeff,
The solution proposed by Valery is correct. Use a TList to support
your own list of histograms.
The reason why your code below does not work is simple.
In C++, if you have an array of objects (say one[10]),
the statement
delete [] one;
deletes the array of pointers but NOT the objects pointed
by the array. You must delete explicitly these objects yourself.
At a first glance, it looks appealing and simpler to use a simple
array of pointers. In reality, there are many situations where
this apparent simplicity turns to be a serious complication
(Copying an array of objects, writing an array to a file, deleting,etc).
The Root container classes (such as TList for sequential use,
or TObjArray for direct access) are a better solution.
In your example below, and as suggested by Valery, you could use
a TList or better a TObjArray.
You can delete the list of objects via list->Delete();
You can delete the list itself via delete list;
the statement "delete list" (like delete [] one] does not delete
the objects referenced by the list.
You may want to simply delete all objects in the list, then start
filling the list again, etc.

Rene Brun

Jeff Patterson wrote:
>
> Hey Rooters,
>
> I am writing a macro that needs to create ALOT of TH1 and TH2.
> All of these histograms quickly eat up memory. I want to create a handful
> of them, get the numbers from them, deconstruct them (free up their
> memory), then construc the next set of them. I deconstruct them, but the
> memory does not seem to be freed up after words. I have tried many
> things. I use pointer to them.
>
> i.e.
> TH1F *one[10];
> TH2F *two[10];
> for(i=0, i<100, ++i)
> {
> one = new TH1F( STUFF DEPENDIING ON i);
> two = new TH2F(MORE STUFF DEPENDING on i);
> .
> .
> .
>
> //Store the numbers of counts in arrays
> //Now it's time to get rid of them !!!
>
> delete[] one;
> delete[] two;
> }
>
> If I run this loop, all my memory is quickly gone after a couple of
> itteration. The delete[] stuff does not seem to be working. I have also
> tried free(one[0]); free(one[1]); etc.....
> And calling the ~TH1F and ~TH2F desonctructos, and nothing seems
> to work. Where am I going wrong ?
>
> Thanks in advance,
> Jeff Patterson