Call to release and primary object destructor

Discusses CAPE-OPEN specific middleware and corresponding development kit

Moderator: jasper

Call to release and primary object destructor

Postby ahmedsabriz » 02 July 2021, 12:05

Hello,

I am wondering if I should delete any secondary object in the destructor of the primary object.

Example: In a Unit operation implementing COBIA, "Unit" is the primary unit operation class with a few basic secondary objects. In "Unit.h", I tried using the following destructor:
Code: Select all
~Unit() {
      delete(param);
      delete(paramCollection);
      delete(product);
      delete(feed);
      delete(portCollection);
}

Where all of the above are pointers previously allocated their respective secondary objects in the constructor "Unit()".

While debugging I noticed a few calls to the destructor while calculating and ended up with an exception thrown for too many calls to release, so I removed all the delete statements and everything went well. I am curious to know why this happened. I thought the destructor would be called only when I delete the unit from my flowsheet.

Thanks in advance.
ahmedsabriz
 
Posts: 20
Joined: 07 June 2021, 13:54

Re: Call to release and primary object destructor

Postby jasper » 03 July 2021, 10:28

In a typical implementation, such objects are assigned to smart pointers, which will take care of the deletion.

So in your example the parameter collection contains a smart pointer to the parameter. So when the parameter collection ends to exists, the destructor of the smart pointer invokes delete on the parameter. The unit operaton in turn has a smart pointer that contains the reference to the parameter collection. So when the unit operation gets released by the PME it deletes itself, and with that the smart pointer gets destructed and deletes the parameter collection.

Same for the ports and the port collection: the ports are referenced by smart pointers in the port collection (and perhaps also by smart pointers in the unit operation), and the port collection is referenced by the smart pointer in the unit operation.

If you set a break point on the destructor of the parameter, parameter collection, port or port collection, you see the call stack when this happens.
User avatar
jasper
 
Posts: 1128
Joined: 24 October 2012, 15:33
Location: Spain


Return to CAPE-OPEN Binary Interop Architecture

Who is online

Users browsing this forum: No registered users and 2 guests

cron