We are having some troubles with TH1::Copy(TObject &obj). Its parameter is of type TObject, but then it casts to TH1 without any check. Therefore, if you try to pass a TObject it crashes.
root [0] TH1F* h = new TH1F("asdf", "asdf", 100, 0, 14)
root [1] TObject *obj = new TObject();
root [2] h->Copy(*obj)
*** glibc detected *** free(): invalid pointer: 0x08d0ee20 ***
Shouldn’t there be at least a check ? or shouldn’t the cast be a dynamic_cast ?
It was a mistake in the first place that lead us to this situation. We had an object declared as TObject and we wrongly thought that it was a TH1 and tried to copy another TH1 in it.
Even though it is wrong to copy a TH1 into a TObject (that is not downcastable to a TH1), it would be nicer to output a message and exit gracefully than crashing.
I will modify TH1::Copy to take a TH1 instead of a TObject as argument.
This will avoid a crash at run time and eventually give you an error at compile time
I would rather check that the TObject is inheriting from TH1 and take action if not (return, exception, exit, whatever). If you change the parameter type, you change the signature of the method and therefore you break polymorphism. Or am I wrong ?
yes, you are right since TH1::Copy is a TObject method re-implemented in TH1 and unfortunately does not return an object (like for example TObject::Clone). The only solution is then to put a run-time check, signal an error and leave the passed object unchanged