I have a C++ class which implements binary compatible interface (to be used as a shared library), thus returning only C types. strings as const char*, void pointers, and pointers to other classes with binary compatible interface. The question is how shall I organize memory management, shall I return constant pointers to existing class data (danger of using outdated pointer by user), and release memory there myself, or rather pointers to some heap variables and make user responsible for deleting those pointers later, or??? Is there some general guidelines for that?
In a "binary compatible interface", using a C interface, you do not assume a shared heap. Therefore, the party which allocates memory from a heap is the party that returns it to that heap.
You can get spectacular failures and/or silent corruption if you allocate a block from one heap, pass it across a C interface, and then have the other side delete
it.
Make the caller responsible for allocating and freeing memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With