Pls ignore last post from me - here is new info

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;
    }

   ;; void operator delete  ( void* ptr );
    (defcfun ("delete" del) :void
      (ptr :pointer))



















On Saturday, March 22, 2014 3:37 PM, Joeish W <joeish80829@yahoo.com> wrote:
Thanks for your reply..I can't rewrite the C wrapper for the C++ function so  I wrote  a delete wrapper like the below because I was getting a warning when it didn't have the char* and I heard you could  also use char* explicitly to remove warning.  My defcfun is standard it accepts a :pointer and returns void....I use it in my programs wherever memory need to get deleted ...I'm pretty good at knowing where to put the memory freeing functions but I still am getting memory leaks that make me have to restart.The memory I'm trying to free is a Mat*, Mat is an OpenCV c++ class...any help is appreciated

void delete_ptr(void* ptr) {
    delete (char*)ptr;
}




On Saturday, March 22, 2014 2:31 PM, Daniel Herring <dherring@tentpost.com> wrote:
On Sat, 22 Mar 2014, Joeish W wrote:


> 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. My question is in this case how would one of you free these "new" memory allocators...When I run them in any kind of loop.  It just eats up my ram, I end up having to restart my pc. Should I make a
> "delete" wrapper and use that.
> I've tried using foreign-free but I still have the same issue of having to restart. Any help is appreciated.
>
> Mat* cv_create_Mat() {
>     return new Mat();

> }


Hi Joeish,

Long story short, you need to follow new() with delete().

C++ new() and delete() extend C's malloc() and free() in roughly the
following way.

T * new(args) {
  T *x=(T *)malloc(sizeof(T));
  x->T(args); // constructor (aka ctor)
  return x;
}

void delete(T *x) {
  if(x) {
    x->~T(); // destructor (aka dtor)
    free(x);
  }
}

Note that both the constructor and destructor are fairly arbitrary
functions, and it is common for them to do additional memory management.


- Daniel