From a0ae5a80d80c8e88318187268148021c6e4a55fa Mon Sep 17 00:00:00 2001 From: TothBenoit Date: Mon, 16 Dec 2024 16:16:45 +0100 Subject: [PATCH] Fix memory patch in vhacd --- hl.sln | 15 ++++++++------- libs/heaps/vhacd.cpp | 29 ++++++++++++++--------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/hl.sln b/hl.sln index 941457e8e..1ba82ed33 100644 --- a/hl.sln +++ b/hl.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 -VisualStudioVersion = 17.12.35506.116 d17.12 +VisualStudioVersion = 17.12.35506.116 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sdl", "libs\sdl\sdl.vcxproj", "{12049F27-EA26-4A33-ADF8-E542C4167C00}" ProjectSection(ProjectDependencies) = postProject @@ -17,17 +17,18 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fmt", "libs\fmt\fmt.vcxproj EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hl", "hl.vcxproj", "{BBF750D2-6DD2-4A41-AC3A-07C070B94FA1}" ProjectSection(ProjectDependencies) = postProject - {76E4DB00-8114-4B96-BA76-39D800F8D5EE} = {76E4DB00-8114-4B96-BA76-39D800F8D5EE} + {12049F27-EA26-4A33-ADF8-E542C4167C00} = {12049F27-EA26-4A33-ADF8-E542C4167C00} + {43AAD9DB-4708-46B6-AAE8-A5DB95A0B573} = {43AAD9DB-4708-46B6-AAE8-A5DB95A0B573} {540E0E11-B7B1-43F8-B107-0867B2D97F74} = {540E0E11-B7B1-43F8-B107-0867B2D97F74} - {7DDA1414-6675-45C7-8254-42057901F865} = {7DDA1414-6675-45C7-8254-42057901F865} {6534D221-34DF-404A-AFCD-6DEC9BBC9798} = {6534D221-34DF-404A-AFCD-6DEC9BBC9798} - {12049F27-EA26-4A33-ADF8-E542C4167C00} = {12049F27-EA26-4A33-ADF8-E542C4167C00} + {76E4DB00-8114-4B96-BA76-39D800F8D5EE} = {76E4DB00-8114-4B96-BA76-39D800F8D5EE} + {7DDA1414-6675-45C7-8254-42057901F865} = {7DDA1414-6675-45C7-8254-42057901F865} + {C6213FBF-BC2B-4235-A827-84A60E848C52} = {C6213FBF-BC2B-4235-A827-84A60E848C52} + {E3F735ED-9701-46BE-A86C-C61D3CE0D525} = {E3F735ED-9701-46BE-A86C-C61D3CE0D525} {EC2DCE5C-267A-4050-8DDE-5BF58FF08E31} = {EC2DCE5C-267A-4050-8DDE-5BF58FF08E31} - {F9A2435E-D545-43EB-B471-A4497D96A71B} = {F9A2435E-D545-43EB-B471-A4497D96A71B} {EC79BC9F-9947-4BCC-92F9-14F90CDE4B04} = {EC79BC9F-9947-4BCC-92F9-14F90CDE4B04} - {C6213FBF-BC2B-4235-A827-84A60E848C52} = {C6213FBF-BC2B-4235-A827-84A60E848C52} {F4D939D6-88D6-4FF2-874A-7BECF75A01C2} = {F4D939D6-88D6-4FF2-874A-7BECF75A01C2} - {43AAD9DB-4708-46B6-AAE8-A5DB95A0B573} = {43AAD9DB-4708-46B6-AAE8-A5DB95A0B573} + {F9A2435E-D545-43EB-B471-A4497D96A71B} = {F9A2435E-D545-43EB-B471-A4497D96A71B} EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ui", "libs\ui\ui.vcxproj", "{6534D221-34DF-404A-AFCD-6DEC9BBC9798}" diff --git a/libs/heaps/vhacd.cpp b/libs/heaps/vhacd.cpp index e4456f7e0..813b8af75 100644 --- a/libs/heaps/vhacd.cpp +++ b/libs/heaps/vhacd.cpp @@ -4,7 +4,10 @@ #include #include -typedef VHACD::IVHACD vhacd; +typedef struct { + VHACD::IVHACD* pInstance; + std::vector convexHulls; +} vhacd; struct convex_hull { vbyte* points; @@ -25,21 +28,22 @@ struct convex_hull { }; HL_PRIM vhacd* HL_NAME(create_vhacd)() { - return VHACD::CreateVHACD(); + return new vhacd{ VHACD::CreateVHACD() }; } HL_PRIM bool HL_NAME(vhacd_compute)(vhacd* pVhacd, float* pPoints, uint32_t countPoints, uint32_t* pTriangles, uint32_t countTriangle, VHACD::IVHACD::Parameters* pParameters ) { - return pVhacd->Compute(pPoints, countPoints, pTriangles, countTriangle, *pParameters); + return pVhacd->pInstance->Compute(pPoints, countPoints, pTriangles, countTriangle, *pParameters); } HL_PRIM int HL_NAME(vhacd_get_n_convex_hulls)(vhacd* pVhacd) { - return pVhacd->GetNConvexHulls(); + return pVhacd->pInstance->GetNConvexHulls(); } HL_PRIM bool HL_NAME(vhacd_get_convex_hull)(vhacd* pVhacd, int index, convex_hull* pConvexHull ) { - VHACD::IVHACD::ConvexHull convexHull; - if ( !pVhacd->GetConvexHull(index, convexHull) ) - return false; + pVhacd->convexHulls.emplace_back(); + VHACD::IVHACD::ConvexHull& convexHull = pVhacd->convexHulls.back(); + if ( !pVhacd->pInstance->GetConvexHull(index, convexHull) ) + return false; pConvexHull->points = (vbyte*)convexHull.m_points.data(); pConvexHull->pointCount = (int)convexHull.m_points.size(); @@ -57,21 +61,16 @@ HL_PRIM bool HL_NAME(vhacd_get_convex_hull)(vhacd* pVhacd, int index, convex_hul pConvexHull->boundsMaxY = convexHull.mBmax[1]; pConvexHull->boundsMaxZ = convexHull.mBmax[2]; - // To avoid freeing convex hull memory - std::vector fakePoints{}; - convexHull.m_points = fakePoints; - std::vector fakeTriangles{}; - convexHull.m_triangles = fakeTriangles; - return true; } HL_PRIM void HL_NAME(vhacd_clean)(vhacd* pVhacd) { - pVhacd->Clean(); + pVhacd->pInstance->Clean(); } HL_PRIM void HL_NAME(vhacd_release)(vhacd* pVhacd) { - pVhacd->Release(); + pVhacd->pInstance->Release(); + delete pVhacd; } #define _VHACD _ABSTRACT(vhacd)