memory leaks [message #9355] |
Wed, 09 September 2009 14:22 |
Anonymous Poster
|
|
From: *e18.physik.tu-muenchen.de
|
|
Hi everybody,
I found in a few tasks that people do a Clear() call at the beginning of their tasks' Exec() methods on their output TClonesArray. PLease be aware that this is a memory leak. You should replace the Clear() call with a Delete() call.
I didnt use the ticket system because this could concern many developers.
Cheers, Christian
|
|
|
|
Re: memory leaks [message #9357 is a reply to message #9356] |
Wed, 09 September 2009 15:01 |
Anonymous Poster
|
|
From: *e18.physik.tu-muenchen.de
|
|
Hi Ralf,
so, what do you think. Is Clear() in this context a memory leak? I wasnt sure immediately what the ROOT daco was telling me. This fix was something I remembered from a long time ago....
Cheers, Christian
|
|
|
Re: memory leaks [message #9360 is a reply to message #9357] |
Wed, 09 September 2009 16:02 |
StefanoSpataro
Messages: 2736 Registered: June 2005 Location: Torino
|
first-grade participant |
From: *ext.kfa-juelich.de
|
|
Here a summary cut&paste:
Clear():
Clear the clones array. Only use this routine when your objects don't allocate memory
since it will not call the object dtors.
Delete():
Clear the clones array. Use this routine when your objects allocate memory (e.g. objects
inheriting from TNamed or containing TStrings allocate memory).
If not you better use Clear() since if is faster.
As far as I have understod, the usage of clear or delete depends on the data members of the object itself
[Updated on: Wed, 09 September 2009 16:03] Report message to a moderator
|
|
|
Re: memory leaks [message #9361 is a reply to message #9360] |
Wed, 09 September 2009 16:05 |
Anonymous Poster
|
|
From: *e18.physik.tu-muenchen.de
|
|
Hi,
all the classes concerned in PandaROOT allocat lots of memory, so we'd have to use Delete().
Cheers, Christian
|
|
|
|
Re: memory leaks [message #9392 is a reply to message #9364] |
Mon, 14 September 2009 14:07 |
Elwin Dijck
Messages: 16 Registered: June 2009 Location: Groningen, The Netherland...
|
occasional visitor |
From: *KVI.nl
|
|
Whether using Clear() instead of Delete() is a memory leak indeed depends on the type of class in the TClonesArray. I had some problems with TClonesArrays earlier (see this thread); this is what I think that the different calls do, please correct me if I'm wrong:
1. Clear()
Marks the memory occupied by the objects in the TClonesArray as available, without running destructors and without actually deallocating anything. The memory will be reused for new objects put into the array, which will overwrite the remains of the old objects.
2. Clear("C")
Same as previous, but will call the function Clear() on each of the objects in the array before marking the memory as available (note that TObject has an implementation of Clear() that does nothing, which should then be overloaded).
3. Delete()
Will run the destructor for each of the objects in the array, still without actually freeing the memory of the objects themselves and then does the same as 1. This happens in a kind of weird way and is supposed to be slow (probably only matters for large arrays though, not sure).
However, calling Delete() is needed for classes containing for instance TStrings as data members, to make sure the internal (dynamically allocated) storage of the strings is deallocated.
I think this would mean the following:
- Because FairHit doesn't dynamically allocate anything, indeed just using Clear() will be ok.
- Classes that do dynamically allocate objects should have a proper Clear() function to be able to use Clear("C") on the TClonesArray, otherwise Delete() is needed to prevent memory leaks.
- For classes that contain (not dynamically allocated) strings or other objects like containers, that themselves dynamically allocate memory, Delete() is needed. I don't think it would be possible to prevent memory leaks by implementing some Clear() function in these cases (unless all these objects have a function that makes them deallocate their internal storage, but for TString and STL classes, only destructors do that I think, since it would typically bring the objects into an invalid state).
Using Delete() is the safest thing to do but also the slowest, so it might indeed be useful to check when it actually matters.
When just using TClonesArrays while reading from a TTree using GetEntry(), there is normally no need to do any Clear() or Delete() as ROOT will free memory automatically, though it might still be useful when there are transient data members involved.
Best regards,
Elwin Dijck
[Updated on: Wed, 16 September 2009 14:04] Report message to a moderator
|
|
|