Page 1 of 1

New and delete in CFFI

Posted: Sat Mar 22, 2014 1:58 pm
by joeish80829
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;
    }

Re: New and delete in CFFI

Posted: Sat Mar 22, 2014 6:12 pm
by schoppenhauer
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!

Re: New and delete in CFFI

Posted: Sat Mar 22, 2014 8:23 pm
by joeish80829
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:)

Re: New and delete in CFFI

Posted: Sun Mar 23, 2014 6:45 am
by Goheeca
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)