Re: [ROOT] Saving a object in a ROOT file with a NULL pointer

From: Pierre-Luc Drouin (pldrouin@physics.carleton.ca)
Date: Fri Jun 06 2003 - 18:01:44 MEST


Here's my header file:

#include <cstdio>
#include "Rtypes.h"
#include "TObject.h"

#ifndef _TEST_
#define _TEST_

class Test:public TObject{
 public:
  Test():testvar(2),testptr(NULL){printf("Test()\n");};
  Test(const Test& rhs){printf("Test(const Test& rhs)\n");};
  virtual ~Test(){printf("~Test()\n"); if(testptr) delete testptr;};
  void SetTest(){if(!testptr) testptr=new Int_t;};
  
 private:
  Int_t testvar;
  Int_t* testptr; //[1]

  ClassDef(Test,1)
};

#endif

Now my source file (no member function here):

#include "Test.h"

ClassImp(Test)

The code I've tried is this one:
TFile f1("test.root","NEW");
Test* mytest=new Test;
f1.Add(mytest);
f1.Write();

It crashes after the TFile::Write() call...
Everything works when I call Test::SetTest() before calling
TFile::Add() (notice that Test::SetTest() does nothing if testptr
is non-null) because it passes an address to testptr.

I want to be able to write a Test instance in the case the function
SetTest() isn't called

I'm using ROOT 3.05/05 with gcc 3.2.2

Thank you

Pierre-Luc Drouin


On Fri, 6 Jun 2003, Rene Brun wrote:

> Hi Pierre-Luc,
> 
> ROOT supports null pointers in I/O.
> I suspect a problem with your destructor.
> Make sure to reset to 0 the pointer when you delete the 
> object.
> 
> As always, try to send an example as short as possible reproducing the 
> problem. You and me will gain time.
> 
> Rene Brun
> 
> 
> On 
> Fri, 6 Jun 2003, Pierre-Luc 
> Drouin 
> wrote:
> 
> > Hi,
> > 
> > I've a class that contains a member variable that is a pointer to another
> > object. This pointer can be NULL, if there's no need for a hold
> > object. The code of my class takes it into account, with
> > if(myptr!=NULL) conditions when operations on myptr have to be
> > done. However, I've noticed that ROOT craches when I try to save this
> > object into a ROOT file. I could use a Int_t variable 'hasptr' that would
> > have a value of 0 when there's no object hold by myptr and a value of 1
> > when there's one and I could indicate a size of '// [hasptr]' for ROOT in
> > the header file, but this would take useless memory...
> > 
> > Is there a Streamer or similar ROOT function I could overload for this
> > class in a way that it could handle a NULL pointer without crashing
> > ROOT?
> > 
> > Thank you!
> > 
> > Pierre-Luc Drouin
> > 
> 



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:12 MET