borglab / wrap

Wrapper generator for C++ projects with multiple language support
Other
28 stars 10 forks source link

Matlab Pointer Handling #48

Closed varunagrawal closed 2 years ago

varunagrawal commented 3 years ago

Currently, as a workaround, the wrapper handles both raw and shared pointers the same way.

We need to investigate what the correct way to do this is via the MEX documentation and be wary of any weird behavior.

mattking-smith commented 3 years ago

@varunagrawal just so you know, I think there may be a memory leak due to this pointer issue.

I currently have a GTSAM and GPMP2 installed and integrated in MATLAB and I find that clearing graph variables does not correctly reallocate memory and causes the memory requested by MATLAB to grow without bound.

varunagrawal commented 3 years ago

That's possible. I unfortunately don't have the expertise with Mex files to delve deep into this. Hopefully someone else can figure this out?

ProfFan commented 3 years ago

Yes, there is definitely a memory leak. However to deal with this we need a lot of work...

mattking-smith commented 3 years ago

I have some experience with MATLAB Mexing of C++ code. Although to be fair, I am not sure where to begin with a project this large...

Any suggestions on how to properly free memory given the current setup without having to shut down MATLAB as I have been doing?

ProfFan commented 3 years ago

There is code that tries to free memory, BUT they have bugs. I think you could start from a small sample project with gtwrap and make that work (no memory leak), and at that time GTSAM should also have no leaks.

dellaert commented 3 years ago

Yes, I think we register some functions that de-allocate the C++ memory. This was a BIG effort to get that right back in the day as we would run VERY large datasets for DARPA. I hope that code survived the transition from our old wrapper code.

varunagrawal commented 3 years ago

Not gonna lie, getting to my current understanding of the wrapper was non-trivial. At least now I've added docs, comments, and broken down a lot of the larger files into smaller ones so it should be more manageable.

My recommendation would be to get an understanding of how the interface parser works in interface_parser within the gtwrap directory. Depending on your python expertise, YMMV, but for Matlab wrapping you only then need to look at matlab_wrapper.py.

The project isn't as large as you think (at least in comparison to GTSAM) so an approach that involves chunking the concepts and things going on will go a long way.

Alas this is when being in a pandemic really hurts since I can't pair program with you. Though we could possibly do it on Friday once I get the ICRA camera ready in.

varunagrawal commented 2 years ago

Let's move the conversation about potential Matlab memory leaks to #133. This thread is getting tangential to the original issue.