Heap error on exit when 9+ bodies created

I've been struggling with this heap error for a while and i've boiled it down to a simple issue. If i spawn more than 9 bodies, I get a heap error on application close. It's so strange. I've set up a simple test where I create a world and i create 9 bodies (storing no pointers), and delete them 5 seconds later. Then once I close the program I get the error.

"HEAP[test.win32.exe]: HEAP: Free Heap block 55bb4b0 modified at 55bb684 after it was freed
Windows has triggered a breakpoint in test.win32.exe.

This may be due to a corruption of the heap, which indicates a bug in test.win32.exe or any of the DLLs it has loaded."

But when I change the program to create 8 bodies, there is no error. What possible reason could there be for this? If its relevant I'm using box2d within cocos2d-x win32.

Any help would be appreciated!
By "delete" you mean use world->DestroyBody to destroy the bodies as explained in the Box2D manual right? You should not delete a b2Body by calling delete on it directly.

I am a bit puzzle about how you can do anything to the bodies if you don't store a pointer to them...

In any case, as the message says, your program is modifying memory after it was freed. This is often due to using delete twice on the same thing. Since it's happening when you close the program, I would suspect it's a class destructor that is doing some cleanup on something that has already been deleted. You should be able to use the debugger to see exactly where it's happening.
Sorry I misspoke a little. By delete the bodies I meant delete the entire world. Nothing box2d exists anymore, but the program still crashes on exit ONLY when the already deleted world had 9 or more bodies in it.
Okay. Are you using the latest version of the source code? The only thing I can think of that sounds similar is this, which has been fixed: viewtopic.php?f=4&t=7796
Possibly not related but maybe that thread will give you a hint about something.

In the meantime, to give us back-seat debuggers a hint, where does it crash? Can you provide a simple reproduction case?
I'm using the version that came with the most recent version of cocos2d-x so i'm hoping its fairly new. How do i check the version?

Here's the basic program

Create and run cocos2d-x scene.

Scene contains declaration
Code:
b2World *world;

Constructor
Code:
world = new b2World(b2Vec2(0,0));
      world->SetAllowSleeping(true);


with a loop to fill with random bodies
Code:
for(int i = 0; i < 9; i++)
      {
         
         b2BodyDef bd;
         bd.type = b2_kinematicBody;
         bd.position.Set(0, i);
         bd.userData = NULL;

         b2Body *b = world->CreateBody(&bd);

         b2CircleShape sd;
         sd.m_radius = i+1;

         b2FixtureDef fd;
         fd.shape = &sd;
         fd.restitution = 1;
         fd.density = 1;
         fd.friction = 1;
         b->CreateFixture(&fd);
      }


After 5 seconds update calls this once

Code:
if(world)
      {
         delete world;
         world = NULL;
      }


Destructor of scene does the same as a precaution but it's never reached

I exit the program and the debugger breaks in free.c at the line indicated
Code:
void __cdecl _free_base (void * pBlock)
{

        int retval = 0;


        if (pBlock == NULL)
            return;

        RTCCALLBACK(_RTC_Free_hook, (pBlock, 0));

        retval = HeapFree(_crtheap, 0, pBlock); <------------- At this line
        if (retval == 0)
        {
            errno = _get_errno_from_oserr(GetLastError());
        }
}


Full error

Code:
HEAP[Test.win32.exe]: HEAP: Free Heap block 63cb588 modified at 63cb67c after it was freed
Windows has triggered a breakpoint in Test.win32.exe.

This may be due to a corruption of the heap, which indicates a bug in Test.win32.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while Test.win32.exe has focus.

The output window may have more diagnostic information.
cocos2d: deallocing CCDirector 00E47FA8
The thread 'Win32 Thread' (0x152c) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x718) has exited with code 0 (0x0).
The program '[6132] Test.win32.exe: Native' has exited with code 0 (0x0).


It mentions the CCDirector but i have no idea how that could be affecting my box2d implementation.

And this is the call stack, I searched through and didn't understand how any of it related to box2d if i'm not mistaken heap errors can be unrelated to where they break the program.

Image

Thank you for your help!
At first glance it looks like a bug in cocos2d-x, but I would think many people would have already discovered it if it was...
You can check the version of Box2D in the file b2Settings.cpp
If you are using some already pre-compiled version of Box2D, you can check the values of:
b2_version.major
b2_version.minor
b2_version.revision
Then again, if you are relying on some pre-compiled version, that might be the problem. I have heard that debug and release builds sometimes do not work together, even if they link ok.
The only other (really long shot) thing I can think of is that the 'world' member already exists (at least in cocos2d iPhone in the Box2D template there is an already existing world variable in the hello world class) and your variable is trampling on that, if that's even possible for your compiler, I don't know.

Original thread: https://box2d.org/forum/viewtopic.php?t=9292