Struct members of cdef'd classes don't get destroyed
See original GitHub issueIf I create a cdef
’d class in a cython file having a struct as class member, and then instantiate that class in python, the structs do not get destroyed when the python object is deleted. Adding an explicit call to the struct destructor in __dealloc__
will do the job however.
Example: cppheader.hpp:
#include <vector>
typedef struct MyObject {
int val;
std::vector<double> big_obj;
} MyObject;
typedef struct InfoHolder {
std::vector<MyObject> v;
int some_val;
} InfoHolder;
void increase_size(MyObject &my_obj)
{
my_obj.big_obj.resize(10000000, 1);
}
void force_destroy(InfoHolder &info_holder)
{
info_holder.~InfoHolder();
}
cyfile.pyx:
from libcpp.vector cimport vector
cdef extern from "cppheader.hpp":
ctypedef struct MyObject:
int val
vector[double] big_obj
ctypedef struct InfoHolder:
vector[MyObject] v
int some_val
void increase_size(MyObject &my_obj)
void force_destroy(InfoHolder &info_holder)
cdef class PyInfoHolder:
cdef InfoHolder info_holder
def __init__(self):
self.info_holder.v.resize(1);
increase_size(self.info_holder.v.at(0))
def say_hello(self):
print("hello")
pyfile.py:
from cyfile import PyInfoHolder
for i in range(100):
py_obj = PyInfoHolder()
py_obj.say_hello()
print("end")
Watch the memory usage when running python pyfile.py
, and then compare against the following alternative cython class:
cdef class PyInfoHolder:
cdef InfoHolder info_holder
def __init__(self):
self.info_holder.v.resize(1);
increase_size(self.info_holder.v.at(0))
def __dealloc__(self):
force_destroy(self.info_holder)
def say_hello(self):
print("hello")
(For some reason, the structs also cannot call their destructor explicitly within cython - e.g. self.info_holder.~InfoHolder()
fails to compile)
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
c# - Why structs cannot have destructors? - Stack Overflow
Ironically, destructors don't destroy classes, but rather delay their destruction until all their affairs have been put in order. – supercat.
Read more >Fluent TUI command - mechanical option in CFD solver
Hello there, I am facing a problem using fluent with TUI command on a calcul center. I run a basic 2D CFD problem...
Read more >Struct being destroyed - C++ - Unreal Engine Forums
If the structure in the array is a plain C++ structure (not inheriting from UObject), they will be destroyed when the owning class...
Read more >Marine CFD Best Practice Guidelines_final
initiatives that have sought to structure existing knowledge in the form of ... The range of CFD tools available for these classes of...
Read more >How does OpenVSP export the really usable file for CFD ...
They want it broken up at least some. However, they don't really like to have an airfoil split along the exact leading edge...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s probably very easily worked around - just declare the struct type as a cppclass instead. It really doesn’t matter to Cython whether the definition in C++ is a struct or a class.
The only thing you lose is the auto-conversation to dicts
Would it be possible to backport this fix to 0.29.x? We are running into this issue in rapidsai/cudf#8935 and are currently restricted to
cython >=0.29,<0.30