Warning in <TH2::SetBinContent>: this method must be overrid

Hi,
I am writting a class “MRayTracer” derived from TNamed. It has a pointer to TH2Poly object (basket) hence i am writting a copy constructor
MRayTracer::MRayTracer(const MRayTracer& initObj)
basket = new TH2Poly(*initObj.basket)
This is giving me warning: "Warning in TH2::SetBinContent: this method must be overridden"
Why is it so ?

Intending your code as:

MRayTracer::MRayTracer(const MRayTracer& initObj){
[...]       
     basket = new TH2Poly(*initObj.basket);
[...]
}

what happens if you do:

MRayTracer::MRayTracer(const MRayTracer& initObj){
[...]       
     basket = initObj.basket;
[...]
}

?

The second question that comes to my mind is: did you inserted the ClassDef() macro in the class declaration header?

#include "TObject.h"

class vec : public TObject{
public:
   vec(){};
   vec(vec& v); 
   [b]ClassDef(vec,1);[/b]
};

The third question: did your copy contructor calls back the TNamed copy contructor? In my mind I’m intending:

MRayTracer::MRayTracer(const MRayTracer& foo):TNamed(foo) { ... }

Hope some of those possible solutions help you!
Have a nice day

 MRayTracer::MRayTracer(const MRayTracer& initObj){
[...]       
     basket = new TH2Poly(*initObj.basket);
[...]
}

Yes. this is what i m doing.

   MRayTracer::MRayTracer(const MRayTracer& initObj){
    [...]       
         basket = initObj.basket;
    [...]
    }

This may lead to situation where, 2 objects A and B will point to same TH2Poly object.
If now A goes out of scope or deletes basket then B will be pointing to invalid object.
I have used ClassDef macro in class declaration header.
I will have to check ur third suggestion though.

Nope… third option also not working… :frowning:
Thanks though for reply. :slight_smile:

Not glad to hear that from you… :cry:
I checked the method you’re calling in the code fragment you posted and no one calls TH2::SetBinContent().
Are you sure the error is from the copy constructor and not from another part of the code?
Anyway if you can keep the third solution I posted coded in your class since it’s the right way to create a derived-class copy constructor!
have a nice day

Gabriele

Hi,
You are right. Problem seems to be coming from following fragment of main() function.
MRayTracer A ; MRayTracer *B = new MRayTracer(A) ;
second line is creating the problem.

Hey there,
Could you please attach me the whole .h and .cpp/.cxx files of your class implementation?
I would like to check one thing
Bye

Gabriele

Hi ,
I posted the reply at wrong thread by mistake. #-o
I am not able to upload post the reply again here. :confused:
Please find the code along with data files at
[root-forum.cern.ch/t/segment-violation-problem-while-deleting-th2poly/18661/1
I am using Makefile to create shared library. but I am not able to upload it.

In addition, present form of the code is causing following memory crash. :frowning:

*** Break *** segmentation violation

===========================================================
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 0x00007f55e0609466 in TUnixSystem::StackTrace() () from /home/chinmay/ROOTv5.34/root/lib/libCore.so
#3 0x00007f55e060bca3 in TUnixSystem::DispatchSignals(ESignals) () from /home/chinmay/ROOTv5.34/root/lib/libCore.so
#4
#5 0x00007f55df6ccf68 in TBufferFile::WriteFastArray(double const*, int) () from /home/chinmay/ROOTv5.34/root/lib/libRIO.so
#6 0x0000000000408e80 in MRayTracer::Streamer (this=0x144f400, R(bool)=…) at MRayTracerDict.cxx:171
#7 0x00007f55df6831d2 in TKey::TKey(TObject const*, char const*, int, TDirectory*) () from /home/chinmay/ROOTv5.34/root/lib/libRIO.so
#8 0x00007f55df5dd52a in TFile::CreateKey(TDirectory*, TObject const*, char const*, int) () from /home/chinmay/ROOTv5.34/root/lib/libRIO.so
#9 0x00007f55df5fa24a in TDirectoryFile::WriteTObject(TObject const*, char const*, char const*, int) () from /home/chinmay/ROOTv5.34/root/lib/libRIO.so
#10 0x00007f55e0539fba in TObject::Write(char const*, int, int) const () from /home/chinmay/ROOTv5.34/root/lib/libCore.so
#11 0x00000000004080e2 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 0x00007f55df6ccf68 in TBufferFile::WriteFastArray(double const*, int) () from /home/chinmay/ROOTv5.34/root/lib/libRIO.so
#6 0x0000000000408e80 in MRayTracer::Streamer (this=0x144f400, R(bool)=…) at MRayTracerDict.cxx:171
#7 0x00007f55df6831d2 in TKey::TKey(TObject const*, char const*, int, TDirectory*) () from /home/chinmay/ROOTv5.34/root/lib/libRIO.so
#8 0x00007f55df5dd52a in TFile::CreateKey(TDirectory*, TObject const*, char const*, int) () from /home/chinmay/ROOTv5.34/root/lib/libRIO.so
#9 0x00007f55df5fa24a in TDirectoryFile::WriteTObject(TObject const*, char const*, char const*, int) () from /home/chinmay/ROOTv5.34/root/lib/libRIO.so
#10 0x00007f55e0539fba in TObject::Write(char const*, int, int) const () from /home/chinmay/ROOTv5.34/root/lib/libCore.so
#11 0x00000000004080e2 in main ()

Hi,
The memory crash problem is solved. I didn’t initialize 2 Double_t arrays in default and copy constructor. :blush:

Hi… this is the corrected source file. There is no crash. But warning is still there.

Hello,
I’ve just managed out how to go around your problem.
First of all I tried your code and I’ve seen that the second time I ran the test macro the warning was no more there.
Since this happens when something goes wrong with the heap I found this solution working:

Your coda was:

I’ve changed to:

In this way you have a failsafe copy created via the Streamer facility, and if your object derives from let’s say, TNamed, the Clone() method calls the right copy constructor. root.cern.ch/root/html/TObject. … ject:Clone

So I attach the working (no runtime warnings) version of the class.

Have fun

Gabriele
MRayTracerWorking.cxx (7.21 KB)

Thanks.
This is “something to be” … :smiley:
Chinmay

Hello,
it was a pleasure!
Have fun

Gabriele