Delete not Deallocating

I have this code. The delete[] function (line 7 and 12) isn't freeing up memory assigned with Triangle3D *triangles = new Triangle3D[triangleCount] (line 17). My RAM usage is growing each time face->GetTriangles() is called. I'd be grateful for any assistance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
	Triangle3D *triangles = face->GetTriangles();
	for (int i = 0; i < face->GetTriangleCount(); i++)
	{
		Triangle3D *triangle = &triangles[i];
		if (IntersectTriangle(rayPos, rayDir, closestHit, triangleIndexHit, triangle))
		{
			delete[] triangles;
			triangleIndexHit = i;
			return true;
		}
	}
	delete[] triangles;

Triangle3D *Face3D::GetTriangles()
{
	int triangleCount = VertexCount - 2;
	Triangle3D *triangles = new Triangle3D[triangleCount];
	for (int i = 0; i < triangleCount; i++)
	{
		triangles[i] = Triangle3D(Vertices[0], Vertices[i + 1], Vertices[i + 2]);
		triangles[i].Index = i;
	}
	return triangles;
}

int Face3D::GetTriangleCount()
{
	return VertexCount - 2;
}
Last edited on
isn't freeing up memory

How do you see that?
How do you see that?


I am looking at the RAM usage using Task manager. If I comment out that portion of code (line 1 to 12) the RAM usage stops going up indefinitely.
Last edited on
get rid of that hideous memory management
use std::vector instead
and run your code through valgrind to check for memory leaks


apart from that
IntersectTriangle(rayPos, rayDir, closestHit, triangleIndexHit, triangle)
¿why is `triangle' a pointer there?
¿what's the purpose of `triangleIndexHit'?
get rid of that hideous memory management
use std::vector instead
and run your code through valgrind to check for memory leaks

Will try this, thanks.


why is `triangle' a pointer there?

Good question... I should replace the pointer reference.


what's the purpose of `triangleIndexHit'?

That's code that wen't through many iterations and should go.
get rid of that hideous memory management
use std::vector instead


1
2
3
4
5
6
7
8
9
10
	std::vector<Triangle3D> triangles = face->GetTriangles();
	int triangles_size = triangles.size();
	for (int i = 0; i < triangles_size; i++)
	{
		if (IntersectTriangle(rayPos, rayDir, closestHit, triangles[i]))
		{
			triangleIndexHit = i;
			return true;
		}
	}


Didn't work. My memory usage is still increasing.

and run your code through valgrind to check for memory leaks

I'm using windows, so I installed Dr. Memory instead. Couldn't make any sense of it.


I googled a bit and got some ideas, but no solution. According to some, calling delete doesn't necessarily mark the memory location as available to the OS for reclaiming. I need this part to clear up as it is being called continuously (while the app is idle). Any ideas?
Last edited on
Correct, delete does NOT necessarily mark memory for reclamation by the OS.

If you need that kind of behavior (which you really don’t*), then you will need to write your own memory manager and use that instead.

It is shockingly easy.
https://www.google.com/search?q=c%2B%2B+release+to+os+memory+manager

Hope this helps.
> Didn't work. My memory usage is still increasing.
http://www.eelis.net/iso-c++/testcase.xhtml
(specially point 6)
A testcase consisting of randomly copy&paste'd bits of code that you thought were relevant can obviously not reproduce the problem. Worse, such testcases force your perception of the problem upon us, preventing us from taking a fresh look at it and making an unbiased analysis.



> it is being called continuously (while the app is idle)
the old idle is not idle trick
perhaps you should make the triangles less temporal then.

> I'm using windows, so I installed Dr. Memory instead. Couldn't make any sense of it.
haven't used that tool.
it should be something like
$ drmemory.exe -- c:/path/to/my/app args to my app
and then grep the log file for leaks

I couldn't fulfill point 6 because of the actual size of the code.

Couldn't make any sense of it.

I meant that the result I got from using the tool didn't point me to the cause.

perhaps you should make the triangles less temporal then.

Thanks ne555. I did that, and it led me to the solution. It turns out that Face3D inherits from Surface3D and was returning an array of Triangle3D objects which also inherits from Surface3D.

Duthomhas I didn't have to write my own memory manager and I'm pretty sure it was because of my bad code.
Topic archived. No new replies allowed.