Segment violation problem while deleting TH2Poly

Hi,
I have a class MRayTracer , derived from TNamed. This class has 2 TH2Poly pointers, which get dynamically allocated in constructor. Now in main() i have

 TFile *froot = new TFile("testMRT.root","RECREATE") ;
 MRayTracer A ;
 MRayTracer *B = new MRayTracer(A) ;
 A.Write() ;
 B->Write() ;
 froot->Close() ;

This is causing memory crash. If I do file declaration after declaration of B then I dont get memory crash.
So it hass something to do with froot deleting objects owned by it. But I dont understand what ? output is as follows:
[img]

There was a crash.
This is the entire stack trace of all threads:

#0 0x0000003156abaf3a in waitpid () from /lib64/libc.so.6
#1 0x0000003156a4175e in do_system () from /lib64/libc.so.6
#2 0x00007fabe37b1466 in TUnixSystem::StackTrace() () from /home/chinmay/ROOTv5.34/root/lib/libCore.so
#3 0x00007fabe37b3ca3 in TUnixSystem::DispatchSignals(ESignals) () from /home/chinmay/ROOTv5.34/root/lib/libCore.so
#4
#5 0x0000000000d70640 in ?? ()
#6 0x00007fabe1df80f9 in TH2Poly::~TH2Poly() () from /home/chinmay/ROOTv5.34/root/lib/libHist.so
#7 0x00007fabe1df81c9 in TH2Poly::~TH2Poly() () from /home/chinmay/ROOTv5.34/root/lib/libHist.so
#8 0x000000000040dd90 in MRayTracer::~MRayTracer (this=0x7fffe33c9890, __in_chrg=) at MRayTracer.cxx:182
#9 0x0000000000407c36 in main ()

The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.

#5 0x0000000000d70640 in ?? ()
#6 0x00007fabe1df80f9 in TH2Poly::~TH2Poly() () from /home/chinmay/ROOTv5.34/root/lib/libHist.so
#7 0x00007fabe1df81c9 in TH2Poly::~TH2Poly() () from /home/chinmay/ROOTv5.34/root/lib/libHist.so
#8 0x000000000040dd90 in MRayTracer::~MRayTracer (this=0x7fffe33c9890, __in_chrg=) at MRayTracer.cxx:182
#9 0x0000000000407c36 in main ()

[/img]

line 182 of MRayTracer.cxx is
delete basket ;
where basket is TH2Poly pointer. this line is part of virtual destructor of MRayTracer where all the allocated memories are deleted.

Problem solved by doing
TH2Poly->SetDirectory(0)

Hi,
I am sending you the header and source file.
I am using the Makefile to create shared library. But am not able to upload it.

Copy your “Makefile” as “Makefile.txt” and upload the “Makefile.txt”.

Hi,
Now I am getting the warning "warning in TH2::SetBinContent the method must be overridden!"
Memory crash problem is solved

Hello,
I’ve just answered on your other post (about SetBinContent() ovveriding) and seems also this issue has been solved by my solution.
Have fun

Gabriele