[ODE] problems with trimesh and capsules

Paul MacKenzie paul.mackenzie at simlog.com
Mon May 22 07:16:13 MST 2006


Hi Jaroslav,

> Well, this is still rather strange to me. How assigning NULL to 
> gLocalContacts before exiting would influence something if we don't test 
> for NULL before reallocating it again (and at any other place in the code).
> It's not a problem to put it in, if it solves the problem for you, but I 
> would prefer to understand what's going on. What's your 
> platform/compiler, Paul?

I am using Visual Studio .NET 2003 on Windows XP.

There are essentially two problems:

The first is that dALLOCA16() allocates memory on the stack within the 
context of the function in which it is called, in this case, 
dCollideCCTL().  So, when dCollideCCTL() exits, the memory pointed to by 
gLocalContacts is no longer valid, and has been returned to the stack. 
Later on, that stack memory is reused elsewhere while gLocalContacts is 
still pointing to it.  By setting gLocalContacts to NULL before the exit 
from dCollideCCTL(), it no longer points to memory that will be invalid, 
and in the next call to dCollideCCTL() it will be reallocated.

The second problem is the "allocate once" idea.  The flags parameter in 
the dCollideCCTL() function containts the maximum number of contacts. 
If the first time I call it I pass flags = 4, and later on flags = 16, 
then if I only allocated gLocalContacts once with a size of 4, and try 
to use it with a size of 16, there will be a buffer overrun.

Setting gLocalContacts to NULL at the end of dCollideCCTL() removes both 
of these problems.

Ideally, gLocalContacts should not be a global variable, and should be 
passed as a parameter to each function that requires it.  However, that 
would involve a bit of refactoring, and the "setting to NULL" solution 
does the job for now.

Cheers,
Paul


More information about the ODE mailing list