'How to define a C++ class destructor with Cython?

cdef cppclass ObjectDef:
    string* name_ptr

    ObjectDef():
        this.name_ptr = new string(b"John")
    ~ObjectDef(string _name):    # <= cython error on "~"
        del this.name_ptr

cdef ObjectDef* my_obj_ptr = new ObjectDef()
del my_obj_ptr


Solution 1:[1]

You would define a __dealloc__ in this case

def __init__(self):
    self.name_ptr = new string(b"John")

def __dealloc__(self):
    del self.name_ptr

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cory Kramer