zarquon42b/Rvcg

Potential memory leak

Opened this issue · 0 comments

Hi @zarquon42b

I was testing my code with valgrind and it led me to the following code:

Rvcg/src/Rdijkstra.cpp

Lines 96 to 99 in a7c34a5

MyMesh::PerVertexAttributeHandle<VertexPointer> sourcesHandle;
sourcesHandle = tri::Allocator<MyMesh>::AddPerVertexAttribute<MyMesh::VertexPointer> (m, "sources");
MyMesh::PerVertexAttributeHandle<VertexPointer> parentHandle;
parentHandle = tri::Allocator<MyMesh>::AddPerVertexAttribute<MyMesh::VertexPointer> (m, "parent");

I wonder should AddPerVertexAttribute be used with DeletePerVertexAttribute together?

https://vcg.isti.cnr.it/vcglib/attributes.html states that:

Remember that the scope of a handle does not interfere with the memory allocation of the attribute. If you do not delete an attribute explicitly, it will stay allocated until the mesh itself is destroyed, even if you do not have any more handles to it.

Also the following code may segfault when the path does not exist (not connected)

Rvcg/src/Rdijkstra.cpp

Lines 117 to 121 in a7c34a5

while(current_vertex != source) {
MyMesh::VertexPointer parent = parentHandle[current_vertex];
int next_vertex = indices[parent];
current_vertex = next_vertex;
path.push_back(current_vertex + 1);

When the segfault happens, parent will be NULL, hence indexing indices[parent] will crash the process.