New and delete in CFFI

Discussion of Common Lisp

New and delete in CFFI

Postby joeish80829 » Sat Mar 22, 2014 1:58 pm

This is going to be wrapped in CFFI but I could use help on the C side a bit...I have a whole list of C wrappers for OpenCV C++ functions like the one below. And all of them return a "new". I can't change them because they are becoming part of OpenCV and it would make my library perfect to have a consistently updated skeleton to wrap around.

Code: Select all
    Mat* cv_create_Mat() {
        return new Mat();
    }

I can't rewrite the C wrapper for the C++ function so I wrote a delete wrapper like the below,The memory I'm trying to free is a Mat*, Mat is an OpenCV c++ class...and the delete wrapper works There is absolutely no memory leakage at all, but I have a lot of other C wrappers for OpenCV C++ functions that return a new pointer...there is at least 10 of 15 and my intention is to not have to write a separate delete wrapper for all of them. If you can show me how to write one delete wrapper that would free any pointer I give to it that would be great...I have CvSVMParams*, Brisk*, RotatedRect*, CVANN_MLP* pointers there are a few others as well...Any help at this is greatly valued.

Code: Select all
    void delete_ptr(void* ptr) {
        delete (Mat*)ptr;
    }
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: New and delete in CFFI

Postby schoppenhauer » Sat Mar 22, 2014 6:12 pm

You could try to manually do name mangling, but this is not portable, and doing this would be enough work for an own project, I guess, so I would not suggest it. I would rather generate the necessary c-code programatically.

So you're writing a wrapper for OpenCV? Nice!
Sorry for my bad english.
Visit my blog http://blog.uxul.de/
schoppenhauer
 
Posts: 99
Joined: Sat Jul 26, 2008 2:30 pm
Location: Germany

Re: New and delete in CFFI

Postby joeish80829 » Sat Mar 22, 2014 8:23 pm

Thanks for the info...I was kinda leaning that way...got some pretty good info on S.O. about your suggestion...yeah I'm writing a wrapper for OpenCv's C++ interface, the first of it's kind. it's at this link if you would like to check it out https://github.com/W-Net-AI/lisp-cv Take care:)
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: New and delete in CFFI

Postby Goheeca » Sun Mar 23, 2014 6:45 am

At least you can create C macro for that, for less typing:
Code: Select all
#define delete(what)         \
   void delete_##what(void* ptr) { \
      delete (what*) ptr \
   }

delete(Mat)
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 213
Joined: Thu May 10, 2012 12:54 pm


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 2 guests

cron