From c19715f040ffefba1f4d20f7ed3fb3e9a8ebc5da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20H=C3=B6schele?= Date: Sat, 11 Jul 2020 15:47:12 +0200 Subject: [PATCH] fix memory leak --- AmeisenNavigation.Server/Program.cs | 8 +-- .../Properties/AssemblyInfo.cs | 4 +- .../AmeisenNavigation.Wrapper.vcxproj | 1 + .../AmeisenNavigationWrapper.h | 58 ++++++++++--------- AmeisenNavigation/ameisennavigation.cpp | 12 ++-- 5 files changed, 41 insertions(+), 42 deletions(-) diff --git a/AmeisenNavigation.Server/Program.cs b/AmeisenNavigation.Server/Program.cs index c46cef6..1b737fb 100644 --- a/AmeisenNavigation.Server/Program.cs +++ b/AmeisenNavigation.Server/Program.cs @@ -91,7 +91,7 @@ public static List GetPath(Vector3 start, Vector3 end, float maxRadius, switch (movementType) { case MovementType.MoveToPosition: - float* movePath = AmeisenNav.GetPath(mapId, pointerStart, pointerEnd, &pathSize); + float[] movePath = AmeisenNav.GetPath(mapId, pointerStart, pointerEnd, &pathSize); for (int i = 0; i < pathSize * 3; i += 3) { @@ -105,7 +105,7 @@ public static List GetPath(Vector3 start, Vector3 end, float maxRadius, break; case MovementType.MoveAlongSurface: - float* surfacePath = AmeisenNav.MoveAlongSurface(mapId, pointerStart, pointerEnd); + float[] surfacePath = AmeisenNav.MoveAlongSurface(mapId, pointerStart, pointerEnd); path.Add(new Vector3(surfacePath[0], surfacePath[1], surfacePath[2])); break; @@ -123,12 +123,12 @@ public static List GetPath(Vector3 start, Vector3 end, float maxRadius, break; case MovementType.GetRandomPoint: - float* randomPoint = AmeisenNav.GetRandomPoint(mapId); + float[] randomPoint = AmeisenNav.GetRandomPoint(mapId); path.Add(new Vector3(randomPoint[0], randomPoint[1], randomPoint[2])); break; case MovementType.GetRandomPointAround: - float* randomPointAround = AmeisenNav.GetRandomPointAround(mapId, pointerStart, maxRadius); + float[] randomPointAround = AmeisenNav.GetRandomPointAround(mapId, pointerStart, maxRadius); path.Add(new Vector3(randomPointAround[0], randomPointAround[1], randomPointAround[2])); break; } diff --git a/AmeisenNavigation.Server/Properties/AssemblyInfo.cs b/AmeisenNavigation.Server/Properties/AssemblyInfo.cs index 26ca544..03cb7f7 100644 --- a/AmeisenNavigation.Server/Properties/AssemblyInfo.cs +++ b/AmeisenNavigation.Server/Properties/AssemblyInfo.cs @@ -11,5 +11,5 @@ [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("94dd9098-c900-4e3b-8d06-01157198914e")] -[assembly: AssemblyVersion("1.4.0.0")] -[assembly: AssemblyFileVersion("1.4.0.0")] \ No newline at end of file +[assembly: AssemblyVersion("1.4.1.0")] +[assembly: AssemblyFileVersion("1.4.1.0")] \ No newline at end of file diff --git a/AmeisenNavigation.Wrapper/AmeisenNavigation.Wrapper.vcxproj b/AmeisenNavigation.Wrapper/AmeisenNavigation.Wrapper.vcxproj index b827e28..24358fe 100644 --- a/AmeisenNavigation.Wrapper/AmeisenNavigation.Wrapper.vcxproj +++ b/AmeisenNavigation.Wrapper/AmeisenNavigation.Wrapper.vcxproj @@ -93,6 +93,7 @@ WIN32;_DEBUG;%(PreprocessorDefinitions) stdcpp17 + Cdecl AmeisenNavigation.lib;recastnavigation.lib; diff --git a/AmeisenNavigation.Wrapper/AmeisenNavigationWrapper.h b/AmeisenNavigation.Wrapper/AmeisenNavigationWrapper.h index 745252e..8eaea55 100644 --- a/AmeisenNavigation.Wrapper/AmeisenNavigationWrapper.h +++ b/AmeisenNavigation.Wrapper/AmeisenNavigationWrapper.h @@ -9,7 +9,7 @@ using namespace System::Runtime::InteropServices; namespace AmeisenNavigationWrapper { - public ref class AmeisenNav + public ref class AmeisenNav : IDisposable { private: AmeisenNavigation* ameisen_nav; @@ -60,25 +60,43 @@ namespace AmeisenNavigationWrapper return ameisen_nav->CastMovementRay(map_id, start, end); } - float* MoveAlongSurface(int map_id, float start[], float end[]) + array^ MoveAlongSurface(int map_id, float start[], float end[]) { - float* positionToGoTo = new float[3]; + float positionToGoTo[3]; ameisen_nav->MoveAlongSurface(map_id, start, end, reinterpret_cast(positionToGoTo)); - return positionToGoTo; + + array^ position = gcnew array(3); + position[0] = positionToGoTo[0]; + position[1] = positionToGoTo[1]; + position[2] = positionToGoTo[2]; + + return position; } - float* GetRandomPoint(int map_id) + array^ GetRandomPoint(int map_id) { - float* positionToGoTo = new float[3]; + float positionToGoTo[3]; ameisen_nav->GetRandomPoint(map_id, reinterpret_cast(positionToGoTo)); - return positionToGoTo; + + array^ position = gcnew array(3); + position[0] = positionToGoTo[0]; + position[1] = positionToGoTo[1]; + position[2] = positionToGoTo[2]; + + return position; } - float* GetRandomPointAround(int map_id, float start[], float maxRadius) + array^ GetRandomPointAround(int map_id, float start[], float maxRadius) { - float* positionToGoTo = new float[3]; + float positionToGoTo[3]; ameisen_nav->GetRandomPointAround(map_id, start, maxRadius, reinterpret_cast(positionToGoTo)); - return positionToGoTo; + + array^ position = gcnew array(3); + position[0] = positionToGoTo[0]; + position[1] = positionToGoTo[1]; + position[2] = positionToGoTo[2]; + + return position; } /// @@ -92,27 +110,10 @@ namespace AmeisenNavigationWrapper /// The position to go to /// Count of waypoints inside the list, remember each waypoint has 3 floats /// Pointer to the array of waypoints - float* GetPath(int map_id, float start[], float end[], int* path_size) + array^ GetPath(int map_id, float start[], float end[], int* path_size) { float* path = new float[maxPointPath * 3]; ameisen_nav->GetPath(map_id, start, end, reinterpret_cast(path), path_size); - return path; - } - - /// - /// Use this method if you dont want to mess around - /// with an unsafe pointer in your code, the path - /// will be retuned as an 1D array formatted like: - /// [ x1, y1, z1, x2, y2, z2, ...] - /// - /// The MapId as an integer 0 => Eastern Kingdoms... - /// The position to start at - /// The position to go to - /// Count of waypoints inside the list, remember each waypoint has 3 floats - /// Array of waypoints (x, y, z) - array^ GetPathAsArray(int map_id, float start[], float end[], int* path_size) - { - float* path = GetPath(map_id, start, end, path_size); array^ temp_path = gcnew array(*path_size * 3); for (int i = 0; i < *path_size * 3; i += 3) @@ -122,6 +123,7 @@ namespace AmeisenNavigationWrapper temp_path[i + 2] = path[i + 2]; } + delete[] path; return temp_path; } }; diff --git a/AmeisenNavigation/ameisennavigation.cpp b/AmeisenNavigation/ameisennavigation.cpp index b593b8f..fe8a6ed 100644 --- a/AmeisenNavigation/ameisennavigation.cpp +++ b/AmeisenNavigation/ameisennavigation.cpp @@ -4,9 +4,7 @@ AmeisenNavigation::AmeisenNavigation(const std::string& mmapFolder, int maxPolyP : m_MmapFolder(mmapFolder), maxPolyPath(maxPolyPathLenght), maxPointPath(maxPointPathLenght), - m_QueryFilter(dtQueryFilter()), - m_NavMeshMap(std::unordered_map()), - m_NavMeshQueryMap(std::unordered_map()) + m_QueryFilter(dtQueryFilter()) { m_QueryFilter.setIncludeFlags(NAV_GROUND | NAV_WATER); m_QueryFilter.setExcludeFlags(NAV_EMPTY | NAV_GROUND_STEEP | NAV_MAGMA_SLIME); @@ -72,14 +70,14 @@ bool AmeisenNavigation::GetPath(const int mapId, const Vector3& startPosition, c } else { - dtPolyRef* polypath = new dtPolyRef[maxPolyPath]; + const std::unique_ptr polypath(new dtPolyRef[maxPolyPath]); int polypathSize = 0; - if (dtStatusSucceed(m_NavMeshQueryMap[mapId]->findPath(startPoly, endPoly, reinterpret_cast(&closestPointStart), reinterpret_cast(&closestPointEnd), &m_QueryFilter, polypath, &polypathSize, maxPolyPath))) + if (dtStatusSucceed(m_NavMeshQueryMap[mapId]->findPath(startPoly, endPoly, reinterpret_cast(&closestPointStart), reinterpret_cast(&closestPointEnd), &m_QueryFilter, polypath.get(), &polypathSize, maxPolyPath))) { D(std::cout << ">> PolyPath size: " << polypathSize << "/" << maxPolyPath << std::endl); - if (dtStatusSucceed(m_NavMeshQueryMap[mapId]->findStraightPath(reinterpret_cast(&closestPointStart), reinterpret_cast(&closestPointEnd), polypath, polypathSize, reinterpret_cast(path), nullptr, nullptr, pathSize, maxPointPath))) + if (dtStatusSucceed(m_NavMeshQueryMap[mapId]->findStraightPath(reinterpret_cast(&closestPointStart), reinterpret_cast(&closestPointEnd), polypath.get(), polypathSize, reinterpret_cast(path), nullptr, nullptr, pathSize, maxPointPath))) { D(std::cout << ">> PointPath size: " << (*pathSize) << "/" << maxPointPath << std::endl); @@ -89,7 +87,6 @@ bool AmeisenNavigation::GetPath(const int mapId, const Vector3& startPosition, c path[i] = RDToWowCoords(path[i]); } - delete[] polypath; return true; } else @@ -104,7 +101,6 @@ bool AmeisenNavigation::GetPath(const int mapId, const Vector3& startPosition, c *pathSize = 0; path = nullptr; - delete[] polypath; return false; } }