OCC Destructors

Hello,

I've seen in the OpenCASCADE includes that no destructor is virtual.

The destructor have to be virtual to be able to destroy the "real" object in case of inheritance.

See the book "Effective C++, 50 specific ways to improve your programs and design " from Scott Meyers, in lesson 14.

If you have : class Base { public:

Base(){};

~Base() {}; }

class Derived : public Base {

Derived(){};

~Derived(){}; }

Base* theBase = new Derived(); (...) delete theBase; // here the destructor of Derived is not called.

I think it is a problem in the CDL generator.

Perhaps the OpenCASCADE team in MDTV can see how to correct his problem.

BTW, it is not a problem if ALL the destructors are virtual, but it is a problem if no one is virtual.

If you are agree with me, you can answer this message. If not, please explain why .

Let's fight together to have a better product ;-)

Stephane

Jean Rahuel's picture

Hello Stephane,

You are right : the destructor of the derivated class is not called in that case.

But the memory is correctly deallocated : - the address of the object created with new in the derivated class is the same as the address of the object of the base class (theBase). - if we delete this object (theBase), we call the descructor of the Base class and we free all the memory allocated (for the Derivated and the Base classes).

If there is some kinds of "specific" code in constructors and destructors, there would be problems but it is not normally the case in Open CasCade.

We shall think to your remarks.

Best Regards,

Jean

S. Routelous's picture

Hello Jean,

But I'm using the destructors in my Objects !!!!

Can you really not change the behaviour of the CDL-Extractor to take care of the virtual keyword ? ( I tried to read the Code , but it too complex ).

Thanks,

Stephane