Box2D 2.4.1在VS2022中触发C6262警告的解决咨询
Box2D 2.4.1 + VS2022 下C6262栈内存警告的疑问
在Visual Studio 2022中使用Box2D 2.4.1版本时,触发了C6262警告:
Function uses '103820' bytes of stack. Consider moving some data to heap.
警告指向main函数,代码为Box2D官方HelloBox2D教程的实现:
#include <box2d/box2d.h> #include <cstdio> // initialize gravity vector const b2Vec2 GRAVITY(0.f, -10.f); int main() { // create new world b2World world(GRAVITY); // define ground box b2BodyDef groundBodyDef; groundBodyDef.position.Set(0.f, -10.f); // set ground to be a rigid body so that it is immovable b2Body* groundBody = world.CreateBody(&groundBodyDef); // sets shape of the ground to be rectangular // length of groundBox is 100 units // height of groundBox is 20 units b2PolygonShape groundBox; groundBox.SetAsBox(50.f, 10.f); // bind ground shape to ground rigid body groundBody->CreateFixture(&groundBox, 0.f); // define dynamic body // setting type to dynamic body allows it to move // in response to forces b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(0.f, 4.f); b2Body* body = world.CreateBody(&bodyDef); // set shape of the dynamic box to be a square // size is 2 units b2PolygonShape dynamicBox; dynamicBox.SetAsBox(1.f, 1.f); // define a fixture for the dynamic box b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicBox; fixtureDef.density = 1.f; fixtureDef.friction = 0.3f; // bind fixture to dynamic body body->CreateFixture(&fixtureDef); // declare time step // at each time step, the engine will do the physics math // (it's like calculating each point on the graph) const float TIME_STEP = 1.f / 60.f; // declare number of iterations to iterate // all constraints to achieve accurate results const int32 VELOCITY_ITERATIONS = 6; const int32 POSITION_ITERATIONS = 2; // begin simulation loop for (int32 i = 0; i < 60; i++) { world.Step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS); b2Vec2 position = body->GetPosition(); float angle = body->GetAngle(); printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle); } world.DestroyBody(body); world.DestroyBody(groundBody); body = NULL; groundBody = NULL; return 0; }
该警告今日打开项目时才出现,昨日多次运行代码均未触发。此前首次学习该教程及在自研C++游戏中集成Box2D时,也曾遇到相同警告。目前代码未崩溃,但担心后续添加更多物体时会出现崩溃问题。
我尝试将栈提交大小和栈保留大小分别调整为1048576和2097152,但未消除警告。经排查,推测是栈上创建的b2World对象占用了103288字节内存导致该问题。
想咨询:
- 是否可以忽略该警告?
- 若不可忽略,应如何解决?
内容的提问来源于stack exchange,提问作者rokson
相关产品推荐
相关产品推荐

