Open meder411 opened 7 years ago
Hi,
flann::Matrix
I'm not sure why I'd need free. I have no leaks or anything. It's just that the C++ example provided in the docs uses it, so I was curious why it wasn't available.
Ok. Dont use free, because you should allocate the memory with new. But all roads lead to rome, i havent tried it so far but it must be possible that you allocate the memory with malloc. But then you have to use the deallocation in this way free(distMat.ptr()), or there is a old version of FLANN where in matrix.h the function free is offered...
Greets from Munich
I actually found that the more memory-safe C++(03+) way to do it is to allocate space using std::vector
rather than with new
or malloc
. For example:
std::vector<double> distances;
distances.reserve(<necessary size>);
Then you can just provide the pointer to the vector data using distances.data()
. I believe it was with C++03 that the standard ensures that vectors are contiguous in memory. Then you can rely on the std::vector
destructor to handle any deletion instead of worrying about delete
or free()
.
The only downside appears to be that, in the case of tight memory constraints, you are giving up complete control of your memory allocation. This is because myVec.reserve(n)
guarantees at least a capacity of n
, but its own optimizations may lead to allocations of more than n
blocks in memory. See the docs.
The reason for my question was really because I thought I may have been missing something when reading through the docs and saw that they used some free()
function.
flann::Matrix is just a wrapper around a memory buffer, it doesn't do any allocation/de-allocation. You should handle do the memory de-allocation yourself. In the example above, the std::vector class will take care of de-allocating the memory.
In testing the C++ example from the docs, I tried to call
and got the errors
When I leave out these commands, the code works just fine. I'm just worried I'm creating memory leaks. Why might this be occurring?
If it helps, here is the rest of the code: