how to delete a pointer to a Handle

Hello all,

How is possible to delete pointer to a handle, for example;

Handle_Geom_CartesianPoint* myPointFunc( float x_, float y_, float z_ ) {

Handle(Geom_CartesianPoint)* _point = new Handle(Geom_CartesianPoint);

*_point = new Geom_CartesianPoint( x_, y_, z_);

return _point; }

In a situation like this how to explicitly destroy the returned point in the calling method.

Douglas McCarthy's picture


To add to what Gilles has already answered, you can also find these points in the Open CASCADE documentation.

Specifically: Foundation Classes User's Guide, ch. 2. Basics, section 2.1 Data types.

To summarize: when constructing an object, check to see whether the class inherits from MMgt_TShared. If it does, you have to create it using NEW. You can see inheritance in the doc if the class has been documented, or in the .cdl file if it has not.

Finally, because a handle is a smart pointer, you must not delete it; this is done automatically.

Best regards,


Gilles Debarbouille's picture

My first comment is the way to declare and create an object manipulated by Handle.

You never have to manipulate pure C++ pointers.

Your function could be written as follow :

Handle(Geom_CartesianPoint) myPointFunc( float x_, float y_, float z_ ) { Handle(Geom_CartesianPoint) _point = new Geom_CartesianPoint( x_, y_, z_); return _point; }

You never have to create explicitly the Handle object by a call to the constructor. It is directly the constructor of Geom_CartesianPoint which creates the Handle.

Now how is managed objects manipulated by Handle ?

The Handle is an object manipulated by value which has as field a pointer on an entity object.

This entity object has as field a use count which is automatically incremented by the call of the constructor.

For the assignement between two Handles the use count of the left member is incremented and the use count of the right member decremented.

As soon as the use count field becomes to zero the handle an the object addressed are destroyed.

If now in your function you go out of the scope of your Handle variable a call to the destructor is inserted by the compiler and called which in a first time decrement the use count and if it is null destroyed the object.

In consequence you never have to call explicitly the destructor of an object manipulated by Handle. All the new delete operator= methods are overloaded to manage correctly the memory.