Deleting a TTree without leaking memory

I am using a TTree in some compiled cpp code, and I am having trouble freeing it. I allocate and then create branches by:

TTree *tree = new TTree(etc); tree->Branch(etc); tree->Branch(etc);

then fill it with data and call tree->Write() to store it in a root file. If I delete by doing:

valgrind reports:

==17789== 30 bytes in 1 blocks are possibly lost in loss record 14,268 of 34,201
==17789== at 0x4A06C8E: operator new(unsigned long) (vg_replace_malloc.c:261)
==17789== by 0x3B3AC9C3C8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator const&) (new_allocator.h:89)
==17789== by 0x3B3AC9DDA9: std::string::_M_mutate(unsigned long, unsigned long, unsigned long) (basic_string.tcc:480)
==17789== by 0x3B3AC9DF6B: std::string::_M_replace_safe(unsigned long, unsigned long, char const*, unsigned long) (basic_string.tcc:685)
==17789== by 0x7BA818B: TClassRef::Assign(TClass*) (in /opt/root/lib/libCore.so)
==17789== by 0x7B89536: TBaseClass::GetClassPointer(bool) (in /opt/root/lib/libCore.so)
==17789== by 0x7B97940: TClass::GetBaseClass(TClass const*) (in /opt/root/lib/libCore.so)
==17789== by 0x7B9D828: TClass::InheritsFrom(TClass const*) const (in /opt/root/lib/libCore.so)
==17789== by 0x5CE441D: TBranch::Init(char const*, char const*, int) (in /opt/root/lib/libTree.so)
==17789== by 0x5CE4A10: TBranch::TBranch(TTree*, char const*, void*, char const*, int, int) (in /opt/root/lib/libTree.so)
==17789== by 0x5D2499F: TTree::Branch(char const*, void*, char const*, int) (in /opt/root/lib/libTree.so)
==17789== by 0x405336: main (main.cpp:122)

where main.cpp:122 is the line I call tree->Branch.
If, before “delete tree;” I put “delete tree->GetBranch(“name”);” valgrind makes a fuss about invalid reads on those lines.
I appreciate that “possibly lost” is not always a problem, am I doing something wrong?

Am using ROOT 5.30 and compiling with gcc 4.4.5.

Thanks.

[quote]If, before “delete tree;” I put “delete tree->GetBranch(“name”);” valgrind makes a fuss about invalid reads on those lines.[/quote]Don’t do that, this will lead to a double delete.

[quote]I appreciate that “possibly lost” is not always a problem, am I doing something wrong?
[/quote]In this case this is not a memory leak but an intentional do-not-delete-something-at-the-end-of-the-process. Try using $ROOTSYS/etc/valgrind-root.supp to hide those intentional memory issue.

Cheers,
Philippe.

Ah ha! Excellent, that makes it much easier to see what is going on with any other errors.

Thanks.