Memory Leak Problem.

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Memory Leak Problem.

Postby donggas90 » Thu May 14, 2015 2:35 am

Code: Select all
class MyAlloc : public dgMemoryAllocator
{
public:
   static void* MyMalloc(unsigned Size)
   {
      return ::malloc(Size);
   }
   static void MyFree(void* Ptr, unsigned Size)
   {
      ::free(Ptr);
   }

   FORCEINLINE MyAlloc()
      : dgMemoryAllocator(MyMalloc, MyFree)
   {
   }
};

int _tmain(int argc, _TCHAR* argv[])
{
   MyAlloc* Alloc = new MyAlloc();
   dgWorld* World = new (Alloc)dgWorld(Alloc);
   dgDynamicBody* Body = new (Alloc)dgDynamicBody();
   dgCollisionBox* Box = new (Alloc)dgCollisionBox(Alloc, 0, 1, 1, 1);
   dgMatrix Identity = dgGetIdentityMatrix();
   dgCollisionInstance* Instance = new (Alloc)dgCollisionInstance(World, Box, 0, Identity);
   World->InitBody(Body, Instance, Identity);
   delete Instance;
   World->DestroyBody(Body);
   delete World;
   int Mem = dgGetMemoryUsed();
   std::cout << Mem << std::endl;
   delete Alloc;

   return 0;
}


I wrote very simple code like above.
but Memory is not collect perfectly.
I think, the logic is valid because that code assembled from Newton.h and Newton.cpp without MasterList features.
Please check it and tell me why memory leaked.
donggas90
 
Posts: 9
Joined: Mon May 11, 2015 12:02 pm
Location: Busan, Republic of Korea

Re: Memory Leak Problem.

Postby donggas90 » Thu May 14, 2015 5:24 am

Now I got more information.
Code: Select all
MyAlloc* Alloc = new MyAlloc();
   dgWorld* World = new (Alloc)dgWorld(Alloc);
   //dgCollisionBox* Box = new (Alloc)dgCollisionBox(Alloc, 0, 1, 1, 1);
   dgMatrix Identity = dgGetIdentityMatrix();
   dgCollisionInstance* Instance = World->CreateBox(1, 1, 1, 0);
   //dgCollisionInstance* Instance = new (Alloc)dgCollisionInstance(World, Box, 0, Identity);
   //dgDynamicBody* Body = World->CreateDynamicBody(Instance, Identity);
   dgDynamicBody* Body = new (Alloc)dgDynamicBody();
   World->InitBody(Body, Instance, Identity);
   delete Instance;
   World->DestroyBody(Body);
   delete World;
   int Mem = dgGetMemoryUsed();
   std::cout << Mem << std::endl;
   delete Alloc;

Above code does not make leak.
So the dgBodyCollisionList and crc are the reason I think.
donggas90
 
Posts: 9
Joined: Mon May 11, 2015 12:02 pm
Location: Busan, Republic of Korea

Re: Memory Leak Problem.

Postby Julio Jerez » Thu May 14, 2015 2:01 pm

you are trying to use the core CPP engine, that will be hard to do.
It has been more than 10 years since the C interface if the face of newton, that I do not recommend using the C++.

if you want to do C++ you are better off using the dNewton project, this is a true C++ wrapped.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Memory Leak Problem.

Postby donggas90 » Fri May 15, 2015 7:17 am

Julio Jerez wrote:you are trying to use the core CPP engine, that will be hard to do.
It has been more than 10 year since the C interface if the face of newton, that I do no recommend using the C++.

if you want to do C++ you are better using the dNewton project, this is a true C++ wrapped.


Thanks for answer.
I checked those classes but I didn't satisfied because of needless overheads.
They receive vector or matrix as pointer. That's nice feature because vector and matrix are not depend on any class type. so the end application can avoid to copy its vector to Newton vector.
However after analysis, I recognized that it is only SURFACE perspective.
In more low level, they still use dgVector and dgMatrix. and copy from pointer!
It's cancel out advantage of pointer feature in memory's perspective.
In additionally, they are wrap more API layers and have more additional features thus harder to trace code for debug.
In the end, if you change this COPY issue, Newton will be more nice engine that has cool APIs.

BTW, I'll consider to use dNewtons because of your recommend.
donggas90
 
Posts: 9
Joined: Mon May 11, 2015 12:02 pm
Location: Busan, Republic of Korea

Re: Memory Leak Problem.

Postby JoeJ » Fri May 15, 2015 9:44 am

donggas90 wrote:they still use dgVector and dgMatrix. and copy from pointer!


Because dgVector is aligned and the user pointer may not, there is no way to avoid the copy - also it may end up more cache friendly and thus faster.

I'd strongly recommend C or C++ interface because you don't need to rewrite anything when Julio changes internals ;)
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Memory Leak Problem.

Postby donggas90 » Fri May 15, 2015 11:17 am

JoeJ wrote:I'd strongly recommend C or C++ interface

OK, I'll use it. Thanks. :lol:
donggas90
 
Posts: 9
Joined: Mon May 11, 2015 12:02 pm
Location: Busan, Republic of Korea


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests