0

I have implemented the solution from this question in my cffi code:

How to free memory in cffi

In an effort to remove a clearly observable memory leak in python3. I have already reduced this memory leak, and identified a different culprit (opencv-python), which is something I can't control, but I do see valgrind complaining that I've mismatched free | delete | delete [].

So, the question becomes: how do I delete [] a thing?

I have tried defining a deletion method in the cdecl header for cffi:

void destroyArr(void * arr){ delete [] arr; }

However this yields errors when I map the header onto a dl. There must be some way to control dynamically allocated arrays returned by a library that does not define an interface that accepts a new pointer...the question is: how?

** (posted prior to the comments) I understand that delete [] is not strictly part of the C standard, but neither is new; cffi has supported new, but not delete | delete []..

Chris
  • 28,822
  • 27
  • 83
  • 158
  • `delete` is not part of C at all but C++. – Ulrich Eckhardt Jan 20 '22 at 16:56
  • I know next to nothing about cffi, but `new` and `delete` are both [C++ operators](https://en.wikipedia.org/wiki/New_and_delete_(C%2B%2B)). – martineau Jan 20 '22 at 16:57
  • `new` is within `cffi`; I am interfacing with a `C`/`C++` library and calling `free`, which I have magically matched in `cffi` due to the standard library being opened behind the scenes. This is all sort of a hack. So I guess the answer is: `cffi` does not do this...? @UlrichEckhardt – Chris Jan 20 '22 at 16:58
  • @martineau added comment above – Chris Jan 20 '22 at 16:59
  • The question you link has a link to cffi docs that explain that `cffi.new()` gives you an object with ownership, i.e. one that makes sure things are deallocated after use. No action required. Any additional deallocation operation is even wrong and could cause complaints by memory debuggers. – Ulrich Eckhardt Jan 20 '22 at 16:59
  • @UlrichEckhardt yes, that is within my level of understanding. I think this is a higher level question, with more knowledge than the basics, thanks. – Chris Jan 20 '22 at 17:00
  • If cffi provides a `new` but not a `delete`, then it's broken IMO. – martineau Jan 20 '22 at 17:09
  • @martineau you (and may the OP) are confusing `ffi.new()` and the C++ operator `new`. I'm not sure I understand the question, however: why focus on `delete[]` when the error message says there is a missing `free or delete or delete[]`? – Armin Rigo Jan 21 '22 at 07:10
  • @ArminRigo The error says there is a mismatch. I can wrap a C library whose documentation calls for the delete method, expose the free method in cffi's cdecl, and free it... but Valgrind claims there is a free/delete mismatch, and a memory leak persists. So there must be some way -- the question -- to satisfy this lib's reqs w/cffi. Or did I write something else? (I am on a cellphone). – Chris Jan 21 '22 at 13:55
  • @ArminRigo Ya'll want to noob stomp. I am sorry, but that is not what this is an opportunity to do. This is CFFI wrapping libtesseract, CFFI being unable to free memory in the exact manner the library calls for, and valgrind identifying this issue, along with an opencv memory leak. So altogether this is a crap show, but I'd like to control something. – Chris Jan 21 '22 at 13:57
  • 1
    @Chris Ah, if your library's documentation says you need to call `delete[]`, then yes, your attempt to write `void destroyArr(void * arr){ delete [] arr; }` is probably close to what you need. Maybe tell us more than "there was an error". As a guess, you may need to write `extern "C" { }` somewhere, to force the function to be exposed as C code even though it contains C++ code (the `delete[]` operator)? Sorry, not extremely familiar with C++. – Armin Rigo Jan 22 '22 at 17:11
  • @ArminRigo I think the dl knows what delete[] is. Libtesseract is a library written in the 90s before C++'s initial standardization (presumably before C++ was a thing of significance), and it has a C-style function architecture. However, I compile it with g++ in C++17 std. So it seems as if google has taken the original library and wrapped it in a C++ build system. Anyways, the dl should know what the delete operator is or does, but I'll have to access it indirectly. I am also compiling it locally so I can fork a branch. Thanks for talking this through -- think I've got a plan. – Chris Jan 22 '22 at 17:18

0 Answers0