2012-12-17 75 views
0

只是試圖建立的Box2D世界上果醬項目帶來的訪問衝突:內存訪問衝突

#include "s3e.h" 
#include "Iw2D.h" 
#include "game.h" 
#include "Box2D\Box2D.h" 

CGame::CGame() 
: m_Position(0,0) 
, m_Size(Iw2DGetSurfaceHeight()/10, Iw2DGetSurfaceHeight()/10) 
{ 
b2Vec2 gravity(0.0f, -10.0f); 
bool doSleep = true; 
b2World world(gravity, doSleep); <------this line is the one 
b2BodyDef groundBodyDef; 
groundBodyDef.position.Set(0.0f, -10.0f); 
b2Body* groundBody = world.CreateBody(&groundBodyDef); 
b2PolygonShape groundBox; 
groundBox.SetAsBox(50.0f, 10.0f); 
groundBody->CreateFixture(&groundBox, 0.0f); 

} 

調試它,表明在b2Settings.cpp「的malloc(1024)」導致訪問衝突

 #include <Box2D/Common/b2Settings.h> 
    #include <cstdlib> 

    b2Version b2_version = {2, 2, 0}; 

    // Memory allocators. Modify these to use your own allocator. 
    void* b2Alloc(int32 size) 
    { 
    return malloc(size); <----this is the one 
    } 

    void b2Free(void* mem) 
    { 
    free(mem); 
    } 
+1

如果世界是在堆上創建的,例如。 b2World * world = new b2World(gravity,doSleep); – iforce2d

回答

0

究竟是什麼iforce2d在評論中說過,你需要創建一個指向b2World的指針並且在其上調用new。你不能直接調用它的constructor,而不要致電new就可以了。很顯然,正確的說法是 -

b2World* world = new b2World(gravity, doSleep);