TClonesArray memoryleak questions

I’m using a TClonesArray and I fill it using the following command:

new((*acceptedTriggers)[iAcceptedTriggers_]) TObjString( );

I noted that if I use the Clear method (fast) I have a memory leak while Delete doesn’t (but program run much slower). Reading the forum I understood that the Clear method is safe “if the class in the array in turn does not contain pointers to objects”. Now a couple of questions…

  1. In principle is just a character variable… Do you have any idea why it produce a leak with the Clear method?
  2. Do you have any suggestion on how to change the above code in order to keep the same functionality (have a TClonesArray of string, TString, characters… it’s the same for me) but use the clear method and increase the performance of my code?

Thanks for helping


did you try to use Clear and insert object by:

Edit. I try to reproduce you problem with simple macro but I don’t have any memory leak, are you sure that you have memory leak here?

I’m not sure I understand what you mean… I’m filling the TClonesArray, not reading it… the suggestion you gave me seems to me meant to read the constructed array… And I have definitely a leak (memory increase steadily) while filling it.


the suggestion you gave me seems to me meant to read the constructed array.

The name of the function is missing leading you. It means ‘please return a constructed object at index # of the TClonesArray’. It is a replacement for the pattern “new ( clones[…] ) classname(…)”.

The main difference is that the new-with-placement will always construct (in the C++ meaning of the word) the object while ConstructedAt will construct the object with there was never an object in the slot or return the previous object if there have been one before.


new((*acceptedTriggers)[iAcceptedTriggers_]) TObjString( );
new((*acceptedTriggers)[iAcceptedTriggers_]) TObjString( );

will call the TObjString C++ constructor twice, while

TObjString *str= (TObjStr*)acceptedTriggers->ConstructedAt(iAcceptedTriggers_);
str= (TObjStr*)acceptedTriggers->ConstructedAt(iAcceptedTriggers_);

will only call it twice.


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.