Game works when built in the IDE, but crashes when I run it from the .exe file.

Started by
40 comments, last by JohnnyCode 7 years, 7 months ago

Guys, very nasty problem here, need your help.

I'm using Code::Blocks and C++ and when I press F9 to "Build and Run", the game starts and I can play, everything is fine.

But when I start it from the .exe file in my debug folder( all .dlls are there ), the game starts and crashes in 3 seconds.

I did some research on the stack: http://stackoverflow.com/questions/186237/program-only-crashes-as-release-build-how-to-debug

and it seems that most of the time it happens is because of a wrong variable initialization.

By using good old printf, I traced the problem to this class here. And the program crashes somewhere in the constructor. I can't even use a debugger because it works perfectly fine in CodeBlocks....

Nevermind, here is the header and then the .cpp file, watch for a wrong initialization somewhere:


class ResourceManager
{
private:

    // Load models
    Model fpsArmsIdle = Model( "models/fpsRig/armsIdle.dae" );
    Model fpsArmsPunch = Model( "models/fpsRig/armsAttack.dae" );
    Model ball = Model( "models/bullets/ball.obj" );
    Model humanRun = Model( "models/guyWithSuit/suitGuyIdle2.fbx" );
    Model humanKnockdown = Model("models/human/humanKnockdown.dae");
    Model humanDead = Model("models/human/humanDead.dae");

    vector<const GLchar*> skyboxTextures;
    vector<Model*> firstPersonAnims;
    vector<Model*> humanAnims;


public:

    ResourceManager();
    ~ResourceManager();

    Model mapClassic = Model( "maps/map_classic/classicmap.obj" );
    Model floor = Model( "models/floor/floor.obj" );
    Model lamp = Model( "models/lamp/lamp.obj" );
    Model box = Model( "models/ka6on/ka6on.obj" );

    void loadResources();

    vector<const GLchar*> getSkyboxTextures();
    vector<Model*> getFirstPersonAnims();
    vector<Model*> getHumanAnims();

};

And the constructor that crashes the game:



//The headers
#include "resourcemanager.h"


ResourceManager::ResourceManager()
{
    loadResources();
}

ResourceManager::~ResourceManager()
{
    ;
}

vector<Model*> ResourceManager::getHumanAnims()
{
    return humanAnims;
}

vector<Model*> ResourceManager::getFirstPersonAnims()
{
    return firstPersonAnims;
}

vector<const GLchar*> ResourceManager::getSkyboxTextures()
{
    return skyboxTextures;
}

void ResourceManager::loadResources()
{
    skyboxTextures.push_back( "maps/skyboxes/nebula/right.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/left.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/top.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/bottom.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/back.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/front.tga" );

    firstPersonAnims.push_back( &fpsArmsIdle );
    firstPersonAnims.push_back( &fpsArmsPunch );
    firstPersonAnims.push_back( &ball );

    humanAnims.push_back( &humanRun );
    humanAnims.push_back( &humanKnockdown );
    humanAnims.push_back( &humanDead );
}

If all seems legit, then there is my App class that could be wrong, but I'm sure there are a lot of wrong things going on here.

Basically, I don't know when to use vector of pointers to objects, and when vector of objects, maybe this is where I go wrong somewhere.

Advertisement
Use vectors of objects every time. If you need polymorphic behavior, use vectors of unique_ptr.

Did it. Now my program crashes in the IDE too.

EDIT: Ok, my idea is to have a ResourceManager class, or maybe more precisely, ResourceLoader class. It loads all the models and stuff, and when I need some object, I pass a pointer to that object. That's why I need the vector of pointers.

Maybe what you're doing is legal C++, but it seems weird you're doing initialization in the .h and .cpp files.

In my honest, most likely outdated, opinion, you should move the Model initialization to a loadModels() function in your .cpp file, then call loadModels() then loadResources() in your constructor.

And you're not initializing your Vector/Array.

I believe it should be more like:

.h file


class ResourceManager
{
private:

    // Load models
    Model fpsArmsIdle;
    Model fpsArmsPunch;
    Model ball;
    Model humanRun;
    Model humanKnockdown;
    Model humanDead;

    vector<const GLchar*> skyboxTextures;
    vector<Model*> firstPersonAnims;
    vector<Model*> humanAnims;


public:

    ResourceManager();
    ~ResourceManager();

    Model mapClassic;
    Model floor;
    Model lamp;
    Model box;

    void loadResources();

    vector<const GLchar*> getSkyboxTextures();
    vector<Model*> getFirstPersonAnims();
    vector<Model*> getHumanAnims();

};

.cpp file


ResourceManager::ResourceManager()
{
    loadModels();
    initResources(); // see code snippet further below
    loadResources();
}

void ResourceManager::loadModels()
{
   // Load models
   fpsArmsIdle = Model( "models/fpsRig/armsIdle.dae" );
   fpsArmsPunch = Model( "models/fpsRig/armsAttack.dae" );
   ball = Model( "models/bullets/ball.obj" );
   humanRun = Model( "models/guyWithSuit/suitGuyIdle2.fbx" );
   humanKnockdown = Model("models/human/humanKnockdown.dae");
   humanDead = Model("models/human/humanDead.dae");

   mapClassic = Model( "maps/map_classic/classicmap.obj" );
   floor = Model( "models/floor/floor.obj" );
   lamp = Model( "models/lamp/lamp.obj" );
   box = Model( "models/ka6on/ka6on.obj" );
}

[code=auto:0]void ResourceManager::initResources()
{
    skyboxTextures = new Vector<GLChar*>();
    firstPersonAnims = new Vector<Model*>();
    humanAnims = new Vector<Model*>();
}

vector<Model*> ResourceManager::getHumanAnims()
{
    return humanAnims;
}

vector<Model*> ResourceManager::getFirstPersonAnims()
{
    return firstPersonAnims;
}

vector<const GLchar*> ResourceManager::getSkyboxTextures()
{
    return skyboxTextures;
}

void ResourceManager::loadResources()
{
    skyboxTextures.push_back( "maps/skyboxes/nebula/right.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/left.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/top.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/bottom.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/back.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/front.tga" );

    firstPersonAnims.push_back( &fpsArmsIdle );
    firstPersonAnims.push_back( &fpsArmsPunch );
    firstPersonAnims.push_back( &ball );

    humanAnims.push_back( &humanRun );
    humanAnims.push_back( &humanKnockdown );
    humanAnims.push_back( &humanDead );
}

Beginner in Game Development?  Read here. And read here.

 

And one more thing. You're not initializing your Vector/Array.

That's a C# thing, not a C++ thing, I believe?

Hello to all my stalkers.

And one more thing. You're not initializing your Vector/Array.

That's a C# thing, not a C++ thing, I believe?

After some googling, it seems you're right.

Beginner in Game Development?  Read here. And read here.

 

Guys, this initialization in the header file and out of the constructor seems to be a new C++11 thing.

I traced the crash completely, it happens when Ioad th 4th or 5th model. It seems like the class doesn't have enough space or something. Can it be a compiler bug?

Can it be a compiler bug?

Highly doubtful.

Step through (and into) as much as you can, and provide the exact error messages you get, and the line of code that makes it occur.

Hello to all my stalkers.

The problem is I don't get error messages, it compiles perfectly fine. But I can't run the .exe file. And it crashes in the middle of loading the objects. Exactly here:


void ResourceManager::loadResources()
{
    // Load models
    objects.push_back( Model( "maps/map_classic/classicmap.obj" ) );
    objects.push_back( Model( "models/floor/floor.obj" ) );
    objects.push_back( Model( "models/lamp/lamp.obj" ) ); //Crashes exactly after loading this model.
                                                          //And it's not because of the model because I swapped it with another and it still crashes
                                                          //In the compiler everything works fine. Only the .exe crashes here.
    objects.push_back( Model( "models/ka6on/ka6on.obj" ) );

    skyboxTextures.push_back( "maps/skyboxes/nebula/right.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/left.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/top.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/bottom.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/back.tga" );
    skyboxTextures.push_back( "maps/skyboxes/nebula/front.tga" );

    firstPersonAnims.push_back( Model( "models/fpsRig/armsIdle.dae" ) );
    firstPersonAnims.push_back( Model( "models/fpsRig/armsAttack.dae" ) );
    firstPersonAnims.push_back( Model( "models/bullets/ball.obj" ) );

    humanAnims.push_back( Model( "models/guyWithSuit/suitGuyIdle2.fbx" ) );
    humanAnims.push_back( Model("models/human/humanKnockdown.dae") );
    humanAnims.push_back( Model("models/human/humanDead.dae") );
}

There is nothing wrong with the model, because I tried swapping the models and it still crashes after some of them are loaded. Seems that there isn't enough space somewhere.

Hang on, what's the type of "objects" again? And isn't humanAnims a vector of pointers to models, or did you change it?

Could you post your Model structure?

This topic is closed to new replies.

Advertisement