Main Content


Release C++ object from MATLAB



clibRelease(cppObj) releases C++ object from MATLAB®, making it inaccessible. Cases where you might call clibRelease are:

  • The C++ library manages the lifetime of an object on the heap.

  • The C++ object can explicitly delete itself.

For C++ objects created in MATLAB either through class constructors or the clib.array types, MATLAB owns and manages the memory. Do not call clibRelease on this object.


collapse all

This C++ library has functions to create and delete Myclass objects. Use the clibRelease function to manage memory for deleted objects.

Create a header file header.hpp with these statements.

class Myclass{};
Myclass* createMyclass() { return new Myclass(); }
void deleteMyclass(Myclass* myc) { delete myc; }

Build an interface to Myclass.


Create a Myclass object.

cppObj = clib.header.createMyclass
cppObj = 
  Myclass with no properties.

Call the deleteMyclass function. The Myclass object remains in the MATLAB workspace.


Call clibRelease to release the memory.

cppObj = 
  handle to deleted Myclass

Input Arguments

collapse all

C++ object created in MATLAB, specified as a handle. Once released, the underlying C++ object is no longer accessible in MATLAB, and the MATLAB handle cppObj becomes invalid. If the object is returned again from the library, then MATLAB creates a handle.

Version History

Introduced in R2019a