Re: [ROOT] delete vs. Delete()

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Wed Oct 11 2000 - 10:59:59 MEST


Hi Mike,

   Delete() makes most sense in the context of containers where it is
used to delete the objects stored in the container (the implementation
provided in TObject might be useful in the interpreter environment).
However, in general for any object that you want to destroy use "delete".
And that is exactly what you want to do in the example below. See also
the example $ROOTSYS/tutorials/hserv.C (where also delete is used in the
same context).

Cheers, Fons.


Mike Kordosky wrote:
> 
> Hi,
> 
> I am a rather new C++/ROOT programmer and I am confused about the operator
> delete and the member function Delete().  Here is a code section that I
> want to use as an example:
> 
> while(1){
>         TMessage *dmess;
>         // daqsock is a TSocket*, created earlier
>         // len is an Int_t, declared earlier
>         // mtype is a Unt_t, declared earlier
>         len=daqsock->Recv(dmess);
>         mtype = dmess->What();
> //
> //      other code here
> //
>         // now done with dmess
>         dmess->Delete();
> 
>         // kMESS_RAWI32 is a message type that I defined
>         if( mtype == kMESS_RAWI32)
>                 continue;
>         else
>                 break;
> }
> 
> My confusion arises over the fact that this loop will only execute
> once.  The second time through it causes a seg fault when I do
> len=daqsock->Recv(dmess);.  What exactly did Delete do here?
> 
> The solution to this problem is to substitute:
> 
> delete dmess; <-> dmess->Delete();
> 
> Everything seems to work ok in this case, but I don't really know why!  I
> think that I understand the role of delete in removing objects from the
> heap, but, then what does Delete() do?  Does Delete() remove objects from
> the stack(!)?  My question is, when do I use Delete()?  What, precisely,
> does Delete() do?
> 
> Thanks!
> 
> Mike Kordosky
> 
> --
> Graduate Research Assistant  // High Energy Physics Lab
> kordosky@hep.utexas.edu     // University of Texas at Austin
> ph: (512) 471-3526

-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland
E-Mail: Fons.Rademakers@cern.ch              Phone: +41 22 7679248
WWW:    http://root.cern.ch/~rdm/            Fax:   +41 22 7677910



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:35 MET