Jump to content
Lethal Raptor Games

PolyMesh Memory Leak?

Recommended Posts

I create a collision patch for my terrain on the fly with the following code which increases the memory usage over time.  Commenting out creating the shape seams to stop it.

#include "App.h"
using namespace Leadwerks;
App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}
App::~App() { delete world; delete window; }

Model* box;
Camera* camera;
bool wireframe = false;

bool App::Start()
{
	window = Window::Create("PolyMesh",50,50, 1024, 768);
	window->Show();

	context = Context::Create(window);

	world = World::Create();

	DirectionalLight* light = DirectionalLight::Create();
	light->SetRotation(35, 35, 35);

	camera = Camera::Create();
	
	box = Model::Box();
	box->Move(0, 0, 2);

	return true;
}

bool App::Loop()
{
	if (window->KeyHit(Key::Escape) == true) { return false; }
	if (window->KeyHit(Key::F1) == true) { camera->GetDebugPhysicsMode() == true ? camera->SetDebugPhysicsMode(false) : camera->SetDebugPhysicsMode(true); }
	if (window->KeyHit(Key::F2) == true) {
		if (wireframe == true) {
			camera->SetDrawMode(0);
			wireframe = false;
		}
		else {
			camera->SetDrawMode(2);
			wireframe = true;
		}
	}

	Vec3 rot = box->GetRotation();
	rot.x += 0.2f;
	rot.y += 0.2f;
	rot.z += 0.2f;
	box->SetRotation(rot);

	//Make a collsion surface on the fly for terrain///////////////////
	int xGrid = 3;
	int zGrid = 3;
	int vertIndex = 0;

	Surface* surface = Surface::Create();
	for (int z = 0; z < zGrid; z++) {
		for (int x = 0; x < xGrid; x++) {
			surface->AddVertex(x, 0.0f, z);
			if (z != 0 && x != 0) {
				surface->AddTriangle(vertIndex, vertIndex - xGrid, vertIndex - 1);
				surface->AddTriangle(vertIndex - xGrid, vertIndex - xGrid - 1, vertIndex - 1);
			}
			vertIndex++;
		}
	}

	Shape* shape = Shape::PolyMesh(surface);//memory leak here?
	//box->SetShape(shape);

	shape->Release();
	surface->Release();
	//////////////////////////////////////////////////////////////////////



	Time::Update();
	world->Update();
	world->Render();

	context->SetBlendMode(Blend::Alpha);
	context->DrawStats(10.0f, 10.0f, true);
	context->Sync(true);

	return true;
}

 

Share this post


Link to post

Just tried it with no success.  Also tried opting out of Betas but still nothing.  Haven't tried 4.4 yet because there was a few errors to sort out first.  Evan just create a simple box shape increases the memory used, albeit a bit slower.

bool App::Loop()
{
	if (window->KeyHit(Key::Escape) == true) { return false; }
	if (window->KeyHit(Key::F1) == true) { camera->GetDebugPhysicsMode() == true ? camera->SetDebugPhysicsMode(false) : camera->SetDebugPhysicsMode(true); }
	if (window->KeyHit(Key::F2) == true) {
		if (wireframe == true) {
			camera->SetDrawMode(0);
			wireframe = false;
		}
		else {
			camera->SetDrawMode(2);
			wireframe = true;
		}
	}

	Shape* shape = Shape::Box();//memeory leak here?
	shape->Release();

	Time::Update();
	world->Update();
	world->Render();

	context->SetBlendMode(Blend::Alpha);
	context->DrawStats(10.0f, 10.0f, true);
	context->Sync(true);

	return true;
}

 

Share this post


Link to post

So in your code, you are just constantly creating a shape every cycle of the loop? Seems like that would be something you would want to do in the start code not the loop? thats a lot of shapes being created every second... not to mention the number of surfaces you were making in the first example... Is this your actual code that you are having problems with or just something you are doing to show your problem?

Share this post


Link to post

This is the code to just show the problem, but I'm doing something pretty much the same in game.   The only difference is, the code will only generate a shape if it's changed, like if the player has moved around on the terrain.  I have my own large terrain class and generating all this data at the start will take enormous amounts of memory and time.  (this was my first approach)

Unless there is a way to edit the vertices of the shape itself then I'm stuck with deleting and remaking it every time it's needed.  It's actually not as slow as you might think either, most of the patches I need form 4x4 grid.  So as long as I don't try to create a thousand of them at once, there's no frame drop.

  • Like 1

Share this post


Link to post

Unfortunately it didn't work.  But I did notice something interesting, it only happens in release mode.  I've also noticed that in release mode the memory usually fluctuates a bit for about 20 seconds then stabilizes, but not in debug mode or at least for not as long...

Maybe internally the release mode function is missing some garbage collection stage...?

Share this post


Link to post

Just discovered this code does it too, but again only in release mode.  Perhaps the releasing of any object is not working correctly?

bool App::Loop()
{
	if (window->KeyHit(Key::Escape) == true) { return false; }
	if (window->KeyHit(Key::F1) == true) { camera->GetDebugPhysicsMode() == true ? camera->SetDebugPhysicsMode(false) : camera->SetDebugPhysicsMode(true); }
	if (window->KeyHit(Key::F2) == true) {
		if (wireframe == true) {
			camera->SetDrawMode(0);
			wireframe = false;
		}
		else {
			camera->SetDrawMode(2);
			wireframe = true;
		}
	}

	Model* box = Model::Box();
	box->Release();				//<- Increases memory used in Release mode

	Time::Update();
	world->Update();
	world->Render();

	context->SetBlendMode(Blend::Alpha);
	context->DrawStats(10.0f, 10.0f, true);
	context->Sync(true);

	return true;
}

EDIT : Been watching the debug version of this for nearly 2 minutes and while Leadwerks says a constant memory usage, the diagnostics tools window of Visual Studio shows a steady increase.   About 1MB a minute.

  • Confused 1

Share this post


Link to post

I am seeing the same exact behavior. System::GetMemoryUsage() is reporting constant memory. But Task Manager and the VS debugger are showing a slow increase.

Internally, the Leadwerks GetMemoryUsage() command uses _CrtMemCheckpoint on Windows. I wonder if this includes memory allocated by DLLs the program is using?

Share this post


Link to post

Okay, I found the cause and fixed it.

  • Thanks 1

Share this post


Link to post
Guest
This topic is now closed to further replies.
×
×
  • Create New...