Re: Memory problem when deleting objects in TFile/TDirectory

From: Rene Brun <Rene.Brun_at_cern.ch>
Date: Thu, 10 Nov 2005 23:00:26 +0100 (MET)


Hi Pierre-Luc,

You must get a huge memory leak with your test. You never delete the object newdir. Sinply replace your code by

    TDirectory *newdir=gDirectory->mkdir("testdir");     gDirectory->Delete("testdir;1");
    delete newdir;

or just

    TDirectory *newdir=gDirectory->mkdir("testdir");     delete newdir;

Rene Brun

On Thu, 10
Nov 2005, Pierre-Luc Drouin wrote:

> Hi,
>
> I have a TDirectory in which I create different objects that I delete once I
> am done using them. The objects are created and deleted in a loop. The
> problem is that ROOT takes more and more memory with iterations, even if I
> delete all TObject and TKey after each iteration. Here is a simple program
> that reproduces the problem.
>
> int main(int nargs, char* args[])
> {
> for (Int_t i=0; i<1000000; i++) {
> TDirectory *newdir=gDirectory->mkdir("testdir");
> gDirectory->RecursiveRemove(newdir);
> gDirectory->Delete("testdir;1");
> }
> }
>
> The memory used by this simple program increases at a rate of about 100 MB/s
> on my laptop until the memory is filled and I get a the following error:
> terminate called after throwing an instance of 'std::bad_alloc'
> what(): St9bad_alloc
>
> What can I do to correctly clean the directory and ROOT memory? In the real
> code I am using I have sub-directories in what is equivalent to the "testdir"
> directory of the simple program. That is why I use
> TDirectory::RecursiveRemove to delete "newdir" and everything in its
> subdirectories.
>
> Thank you!
>
> Pierre-Luc Drouin
>
Received on Thu Nov 10 2005 - 23:00:42 MET

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:13 MET