From 2d9b3d6d7b16207222724954201e88ead0fb5b09 Mon Sep 17 00:00:00 2001 From: fangyunfeng Date: Mon, 12 Oct 2020 21:08:30 +0800 Subject: [PATCH 1/3] fix memory leak --- src/marching_cubes.cpp | 9 ++++++++ src/marching_cubes.h | 5 +++-- src/python_wrapper.cpp | 48 +++++++++++++++++++++++++++++++----------- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/marching_cubes.cpp b/src/marching_cubes.cpp index 9457737..f845023 100644 --- a/src/marching_cubes.cpp +++ b/src/marching_cubes.cpp @@ -312,6 +312,15 @@ Mesh::Mesh(size_t vertexCount, Point* vertices, Point* normals, size_t faceCount Mesh::Mesh() {} +Mesh::~Mesh() +{ + if (!vertices) + delete[] vertices; + if (!normals) + delete[] normals; + if (!faces) + delete[] faces; +} template Mesh march(const T* volume, size_t xDim, size_t yDim, size_t zDim, T isoLevel) diff --git a/src/marching_cubes.h b/src/marching_cubes.h index ff2b296..9bf0218 100644 --- a/src/marching_cubes.h +++ b/src/marching_cubes.h @@ -30,13 +30,14 @@ struct Triangle struct Mesh { size_t vertexCount; - Point* vertices; + Point* vertices = nullptr; Point* normals; size_t faceCount; size_t* faces; Mesh(size_t, Point*, Point*, size_t, size_t*); Mesh(); + ~Mesh(); }; /** @@ -48,4 +49,4 @@ struct Mesh */ template Mesh march(const T* volume, size_t xDim, size_t yDim, size_t zDim, T isoLevel); -#endif \ No newline at end of file +#endif diff --git a/src/python_wrapper.cpp b/src/python_wrapper.cpp index cd1eb19..0f67185 100644 --- a/src/python_wrapper.cpp +++ b/src/python_wrapper.cpp @@ -56,18 +56,42 @@ namespace marching_cubes throw py::value_error("vertex or face count are zero: terminating marching cubes"); } - std::string vert_format = py::format_descriptor::value; - auto vert_info = py::buffer_info(mesh.vertices, sizeof(float), vert_format, 2, { vc, 3 }, { sizeof(float) * 3, sizeof(float) }); - auto vert_array = py::array(vert_info); - - std::string norm_format = py::format_descriptor::value; - auto norm_info = py::buffer_info(mesh.normals, sizeof(float), norm_format, 2, { vc, 3 }, { sizeof(float) * 3, sizeof(float) }); - auto norm_array = py::array(norm_info); - - std::string face_format = py::format_descriptor::value; - auto face_info = py::buffer_info(mesh.faces, sizeof(size_t), face_format, 2, { fc, 3 }, { sizeof(size_t) * 3, sizeof(size_t) }); - auto face_array = py::array(face_info); - + //std::string vert_format = py::format_descriptor::value; + //auto vert_info = py::buffer_info(mesh.vertices, sizeof(float), vert_format, 2, { vc, 3 }, { sizeof(float) * 3, sizeof(float) }); + //auto vert_array = py::array(vert_info); + + //std::string norm_format = py::format_descriptor::value; + //auto norm_info = py::buffer_info(mesh.normals, sizeof(float), norm_format, 2, { vc, 3 }, { sizeof(float) * 3, sizeof(float) }); + //auto norm_array = py::array(norm_info); + + //std::string face_format = py::format_descriptor::value; + //auto face_info = py::buffer_info(mesh.faces, sizeof(size_t), face_format, 2, { fc, 3 }, { sizeof(size_t) * 3, sizeof(size_t) }); + //auto face_array = py::array(face_info); + + py::capsule free_vertices_when_done(mesh.vertices, [](void *f) { + float* foo = reinterpret_cast(f); + delete[] foo; + }); + py::capsule free_normals_when_done(mesh.normals, [](void *f) { + float* foo = reinterpret_cast(f); + delete[] foo; + }); + py::capsule free_faces_when_done(mesh.faces, [](void *f) { + size_t* foo = reinterpret_cast(f); + delete[] foo; + }); + auto vert_array = py::array_t({vc,3}, + {sizeof(float)*3,sizeof(float)}, + (float*)mesh.vertices, + free_vertices_when_done); + auto norm_array = py::array_t({vc,3}, + {sizeof(float)*3,sizeof(float)}, + (float*)mesh.normals, + free_normals_when_done); + auto face_array = py::array_t({vc,3}, + {sizeof(size_t)*3,sizeof(size_t)}, + (size_t*)mesh.faces, + free_faces_when_done); return std::make_tuple(vert_array, norm_array, face_array); } From a87e1d351a0d47ffe9d2e66a0d05cddc70faf834 Mon Sep 17 00:00:00 2001 From: fangyunfeng Date: Mon, 12 Oct 2020 22:26:03 +0800 Subject: [PATCH 2/3] rm nullptr --- src/marching_cubes.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/marching_cubes.h b/src/marching_cubes.h index 9bf0218..6337562 100644 --- a/src/marching_cubes.h +++ b/src/marching_cubes.h @@ -30,7 +30,7 @@ struct Triangle struct Mesh { size_t vertexCount; - Point* vertices = nullptr; + Point* vertices; Point* normals; size_t faceCount; size_t* faces; From cd9e7de3c4b824697507b270d39b2abc21d31c4d Mon Sep 17 00:00:00 2001 From: fangyunfeng Date: Fri, 16 Oct 2020 16:43:30 +0800 Subject: [PATCH 3/3] fix python_wrapper vc->fc --- src/python_wrapper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/python_wrapper.cpp b/src/python_wrapper.cpp index 0f67185..f62013b 100644 --- a/src/python_wrapper.cpp +++ b/src/python_wrapper.cpp @@ -88,7 +88,7 @@ namespace marching_cubes {sizeof(float)*3,sizeof(float)}, (float*)mesh.normals, free_normals_when_done); - auto face_array = py::array_t({vc,3}, + auto face_array = py::array_t({fc,3}, {sizeof(size_t)*3,sizeof(size_t)}, (size_t*)mesh.faces, free_faces_when_done);