This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

New and delete in CFFI

4 posts · 3482 views

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.
    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.
    void delete_ptr(void* ptr) {
        delete (Mat*)ptr;
    }

Re: New and delete in CFFI

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/

Re: New and delete in CFFI

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

At least you can create C macro for that, for less typing:
#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). Temporary mirrors of aferomentioned: CLHS and a dark version.