Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.
|
|
|
|
Re: Making C++ objects persistent between mex calls, and robust.
Posted:
Jan 22, 2013 5:07 AM
|
|
"Oliver" wrote: > They were both pointers, so to copy they need to be de-referenced?
Exactly. You want to copy the value, not the pointer.
> Yes, your code is much simpler and straightforward. I thought I was freeing the memory in a destructor by calling freedata (which executed mxFree) in the first part of the "delete". Why didn't this work properly?
Firstly freedata is a member function, but not the destructor (which would be ~dummy()), so you have to remember to call it everywhere that an object of that class is destroyed, whereas the destructor gets called automatically. Secondly, since you were overwriting the pointer reference returned by mxCalloc, you were not then able to free that memory.
> And why isn't mexMakeMemoryPersistent needed? Is it the mexLock in class_handle.hpp that is making everything in the class object persistent?
Memory allocated using new is not kept track of by MATLAB, so it is not freed when the mex function is exited. However, it is freed when the mex file is cleared from memory, which is why you need the mexLock.
|
|
|
|