Skip to content

Commit

Permalink
fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnnshschl committed Jul 11, 2020
1 parent 5c9470e commit c19715f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
8 changes: 4 additions & 4 deletions AmeisenNavigation.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static List<Vector3> 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)
{
Expand All @@ -105,7 +105,7 @@ public static List<Vector3> 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;

Expand All @@ -123,12 +123,12 @@ public static List<Vector3> 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;
}
Expand Down
4 changes: 2 additions & 2 deletions AmeisenNavigation.Server/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
[assembly: AssemblyVersion("1.4.1.0")]
[assembly: AssemblyFileVersion("1.4.1.0")]
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<PreprocessorDefinitions>WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile />
<LanguageStandard>stdcpp17</LanguageStandard>
<CallingConvention>Cdecl</CallingConvention>
</ClCompile>
<Link>
<AdditionalDependencies>AmeisenNavigation.lib;recastnavigation.lib;</AdditionalDependencies>
Expand Down
58 changes: 30 additions & 28 deletions AmeisenNavigation.Wrapper/AmeisenNavigationWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace System::Runtime::InteropServices;

namespace AmeisenNavigationWrapper
{
public ref class AmeisenNav
public ref class AmeisenNav : IDisposable
{
private:
AmeisenNavigation* ameisen_nav;
Expand Down Expand Up @@ -60,25 +60,43 @@ namespace AmeisenNavigationWrapper
return ameisen_nav->CastMovementRay(map_id, start, end);
}

float* MoveAlongSurface(int map_id, float start[], float end[])
array<float>^ 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<Vector3*>(positionToGoTo));
return positionToGoTo;

array<float>^ position = gcnew array<float>(3);
position[0] = positionToGoTo[0];
position[1] = positionToGoTo[1];
position[2] = positionToGoTo[2];

return position;
}

float* GetRandomPoint(int map_id)
array<float>^ GetRandomPoint(int map_id)
{
float* positionToGoTo = new float[3];
float positionToGoTo[3];
ameisen_nav->GetRandomPoint(map_id, reinterpret_cast<Vector3*>(positionToGoTo));
return positionToGoTo;

array<float>^ position = gcnew array<float>(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<float>^ 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<Vector3*>(positionToGoTo));
return positionToGoTo;

array<float>^ position = gcnew array<float>(3);
position[0] = positionToGoTo[0];
position[1] = positionToGoTo[1];
position[2] = positionToGoTo[2];

return position;
}

/// <summary>
Expand All @@ -92,27 +110,10 @@ namespace AmeisenNavigationWrapper
/// <param name="end">The position to go to</param>
/// <param name="path_size">Count of waypoints inside the list, remember each waypoint has 3 floats</param>
/// <returns>Pointer to the array of waypoints</returns>
float* GetPath(int map_id, float start[], float end[], int* path_size)
array<float>^ 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<Vector3*>(path), path_size);
return path;
}

/// <summary>
/// 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, ...]
/// </summary>
/// <param name="map_id">The MapId as an integer 0 => Eastern Kingdoms...</param>
/// <param name="start">The position to start at</param>
/// <param name="end">The position to go to</param>
/// <param name="path_size">Count of waypoints inside the list, remember each waypoint has 3 floats</param>
/// <returns>Array of waypoints (x, y, z)</returns>
array<float>^ GetPathAsArray(int map_id, float start[], float end[], int* path_size)
{
float* path = GetPath(map_id, start, end, path_size);

array<float>^ temp_path = gcnew array<float>(*path_size * 3);
for (int i = 0; i < *path_size * 3; i += 3)
Expand All @@ -122,6 +123,7 @@ namespace AmeisenNavigationWrapper
temp_path[i + 2] = path[i + 2];
}

delete[] path;
return temp_path;
}
};
Expand Down
12 changes: 4 additions & 8 deletions AmeisenNavigation/ameisennavigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, dtNavMesh*>()),
m_NavMeshQueryMap(std::unordered_map<int, dtNavMeshQuery*>())
m_QueryFilter(dtQueryFilter())
{
m_QueryFilter.setIncludeFlags(NAV_GROUND | NAV_WATER);
m_QueryFilter.setExcludeFlags(NAV_EMPTY | NAV_GROUND_STEEP | NAV_MAGMA_SLIME);
Expand Down Expand Up @@ -72,14 +70,14 @@ bool AmeisenNavigation::GetPath(const int mapId, const Vector3& startPosition, c
}
else
{
dtPolyRef* polypath = new dtPolyRef[maxPolyPath];
const std::unique_ptr<dtPolyRef> polypath(new dtPolyRef[maxPolyPath]);
int polypathSize = 0;

if (dtStatusSucceed(m_NavMeshQueryMap[mapId]->findPath(startPoly, endPoly, reinterpret_cast<const float*>(&closestPointStart), reinterpret_cast<const float*>(&closestPointEnd), &m_QueryFilter, polypath, &polypathSize, maxPolyPath)))
if (dtStatusSucceed(m_NavMeshQueryMap[mapId]->findPath(startPoly, endPoly, reinterpret_cast<const float*>(&closestPointStart), reinterpret_cast<const float*>(&closestPointEnd), &m_QueryFilter, polypath.get(), &polypathSize, maxPolyPath)))
{
D(std::cout << ">> PolyPath size: " << polypathSize << "/" << maxPolyPath << std::endl);

if (dtStatusSucceed(m_NavMeshQueryMap[mapId]->findStraightPath(reinterpret_cast<const float*>(&closestPointStart), reinterpret_cast<const float*>(&closestPointEnd), polypath, polypathSize, reinterpret_cast<float*>(path), nullptr, nullptr, pathSize, maxPointPath)))
if (dtStatusSucceed(m_NavMeshQueryMap[mapId]->findStraightPath(reinterpret_cast<const float*>(&closestPointStart), reinterpret_cast<const float*>(&closestPointEnd), polypath.get(), polypathSize, reinterpret_cast<float*>(path), nullptr, nullptr, pathSize, maxPointPath)))
{
D(std::cout << ">> PointPath size: " << (*pathSize) << "/" << maxPointPath << std::endl);

Expand All @@ -89,7 +87,6 @@ bool AmeisenNavigation::GetPath(const int mapId, const Vector3& startPosition, c
path[i] = RDToWowCoords(path[i]);
}

delete[] polypath;
return true;
}
else
Expand All @@ -104,7 +101,6 @@ bool AmeisenNavigation::GetPath(const int mapId, const Vector3& startPosition, c

*pathSize = 0;
path = nullptr;
delete[] polypath;
return false;
}
}
Expand Down

0 comments on commit c19715f

Please sign in to comment.