Still other memory leaks!

The following code (OpenCascade 6.3.0 + Visual Studio 2008 Sp1), report memory leaks:

#include "stdafx.h"
#define _CRTDBG_MAP_ALLOC
#include
#include
#include

int _tmain(int argc, _TCHAR* argv[])
{
_CrtMemState s1;
_CrtMemCheckpoint(&s1);
{
TopoDS_Shape sphere = BRepPrimAPI_MakeSphere(gp_Pnt(100.,100.,100.), 500.);
sphere.Nullify();
}
_CrtMemDumpAllObjectsSince(&s1);
return 0;
}

Output:

Dumping objects ->
{361} normal block at 0x022E6BB8, 564 bytes long.
Data: 00 00 00 00 00 00 FD FE 00 00 FD FE 00 00 00 00
{360} normal block at 0x022E69E0, 412 bytes long.
Data: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.
The program '[3824] TestOCC.exe: Native' has exited with code 0 (0x0).

How I can free memory?

AGPX's picture

Looking at it better, if I try to create 3 spheres the memory wasted doesn't grow:

{
TopoDS_Shape sphere = BRepPrimAPI_MakeSphere(gp_Pnt(100.,100.,100.), 500.);
sphere.Nullify();
TopoDS_Shape sphere2 = BRepPrimAPI_MakeSphere(gp_Pnt(100.,100.,100.), 500.);
sphere2.Nullify();
TopoDS_Shape sphere3 = BRepPrimAPI_MakeSphere(gp_Pnt(100.,100.,100.), 500.);
sphere3.Nullify();
}

Output:

Dumping objects ->
{361} normal block at 0x022E6BB8, 564 bytes long.
Data: < > 00 00 00 00 00 00 FD FE 00 00 FD FE 00 00 00 00
{360} normal block at 0x022E69E0, 412 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.
The program '[3824] TestOCC.exe: Native' has exited with code 0 (0x0)

So basically, seems that the memory manager of OpenCascade work on a "block basis". The memory isn't immediately freed, but reused for successive allocations. This is BAD, because make the detection of memory leaks very hard!!!

AGPX's picture

Ok, I figure out that tracking memory leaks is quite impossibile when you use OpenCascade due to its internal memory handler. But IMHO the memory handler should provide some memory analysis tools to detect those leaks!

Bearloga's picture

Hi,
This topic has been already discussed many times. Please, use the search.
The short answer: you can switch off using internal memory manager setting environment variable MMGT_OPT=0.
Regards,
Bearloga

Roman Lygin's picture

To test memory leaks you should set MMGT_OPT=0 before starting Open CASCADE-based app. This forwards Standard::Allocate(), ::Free() and ::Realloc() to plain alloc(), free() and realloc() - see Standard_MMgrRaw.

With that you can use your favorite memory detection tool. I have been successfully using Intel Parallel Inspector for that.

Hope this helps.
Roman

---
opencascade.blogspot.com - the Open CASCADE blog
www.cadexchanger.com - CAD Exchanger, your 3D data translator