• Advertisement
Sign in to follow this  

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

This topic is 578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by codeBoggs

Share this post


Link to post
Share on other sites
Advertisement

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.

Edited by codeBoggs

Share this post


Link to post
Share on other sites

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 );
}
Edited by Alpheus

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites

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?

Edited by codeBoggs

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by codeBoggs

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement