I encountered this C6262 Warning: “Function uses ‘103820’ bytes of stack. Consider moving some data to heap.” while I was trying out Box2D version 2.4.1 on Visual Studio 2022. This warning was highlighted on my main function:
#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.2fn", position.x, position.y, angle);
}
world.DestroyBody(body);
world.DestroyBody(groundBody);
body = NULL;
groundBody = NULL;
return 0;
}
It is worth pointing out that the warning message appeared when I opened the solution of this project today. The message did not appear yesterday when I was checking if this message appear after executing the code several times.
This isn’t the first time I encountered this error, as I also faced this error when using the same version of Box2D for the first time when I was following the HelloBox2D tutorial in the docs. The code snippet above is in fact the result upon following the tutorial. The same warning also appeared when I implemented Box2D on my C++ game (which I’m currently working on). So far, the code didn’t result in any crashes, but I have a feeling it might crash eventually when I keep on adding more bodies.
I tried increasing the stack commit size and stack reserve size to 1048576 and 2097152, but it didn’t suppress the error. I’m assuming that this error was caused by the b2World world
object since having that line alone used 103288 bytes.
This has been bugging me for a couple of days, so is it best to just ignore this warning? If not, how should I address this?
rokson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.