Hi All, On Mon, 16 Apr 2001 20:38:23 -0400 "Flemming Videbaek" <videbaek@sgs1.hirg.bnl.gov> wrote concerning ": brat and root v3.00.6": > Many of the problems I had end of last week has clearly been related > to improper initialization as I had also found - Today I had > problems writing/reading matched tracking stuff, but after linking > with libNew.so things worked. I hope you don't suggest that we link against libNew per default? The overhead of garbagecollection, ya di da, we really don't need. > This clearly points to the warning given in the rootv3.00,.06 > description - and to us pointing out to be much more carefull > in writing constructors (and destructors). There are more loose end > to be found. In summary: If your class contains pointers to objects, make sure that the constructor initialises them properly. Assigning Zero to a pointer is also an initialisation: class BrFoo : BrBar { private: BrGnus* fGnus; // An external reference BrGnat* fGnat; // Owned by this object public: BrFoo() { fGnus = 0; fGnat = new BrGnat; } void SetGnus(BrGnus* gnus=0) { fGnus = gnus; } void Print(Option_t* option="") { cout << "BrFoo::Print(" << option << ")" << endl; if (fGnus) fGnus->Print(option); if (fGnat) fGnat->Print(option); } ... If your class is the owner of a pointer to an object, then you must delete it in the destructor. Test before deleting a pointer that it points to something. ~BrFoo() { // fGnus is an external reference if (fGnat) delete fGnat; } The ANSI/ISO C++ standard says [3.7.3.2 paragraph 3], ... the deallocation function [operator delete] shall deallocate the storage referenced by the pointer, rendering invalid [= 0] all pointers refering to any part of the _deallocated_ _storage_. However, experience has shown that not all C++ compilers are ANSI/ISO compliant (even though they claim to be), so care should be taken to not dereference pointers to deallocated storage. GCC is compliant, however. In general, one should always be carefull when dereferencing pointers. If you can _guaranty_ that a pointer is always initialised and allocated in your class and no one else has deallocated the storage, you can safely dereference it. However, if you at some point in the execution of your class _may_ deallocated the storage, or the storage can be deallocated outside of the class (as in the case of external references), one should always test wether the pointer actually points to something, using a conditional statement as in BrFoo::Print above. Yours, Christian ----------------------------------------------------------- Holm Christensen Phone: (+45) 35 35 96 91 Sankt Hansgade 23, 1. th. Office: (+45) 353 25 305 DK-2200 Copenhagen N Web: www.nbi.dk/~cholm Denmark Email: cholm@nbi.dk
This archive was generated by hypermail 2b29 : Tue Apr 17 2001 - 05:54:02 EDT