Sorry but code errors again :(

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
lisacvuk
Posts: 21
Joined: Thu Apr 17, 2014 4:50 pm

Sorry but code errors again :(

Post by lisacvuk »

Hello, this is my 4th post about an error. Yup, 4th. I'm sorry but it seems I really can't do anything without an error. I split my project into two, engine and game. Main class is inside engine project which compiles into dynamic lib and does all renderering, sound, physics etc. Game is project which uses classes and functions from engine project and cares for weapons, items, maps and so on. I get segmetation fault on my rendering loop inside main class and function renderer.
Workspace name is "Misteries" because its in-dev name for the game.
Code:
main.cpp

Code: Select all

 
#include <engine.h>
#include <irrlicht.h>
 
using namespace irr;
using namespace video;
using namespace Engine;
 
int main()
{
    CGame Game;
    Game.initialize(640, 480, 0, EDT_OPENGL);
    Game.renderer();
    return 0;
 
}
 
engine.h

Code: Select all

 
#ifndef ENGINE_H_INCLUDED
#define ENGINE_H_INCLUDED
#include <irrlicht.h>
namespace Engine
{
    class CGame
    {
    public:
        irr::IrrlichtDevice *device;
        irr::video::IVideoDriver* driver;
        irr::scene::ISceneManager* smgr;
        irr::gui::IGUIEnvironment* guienv;
        void initialize(int screenw, int screenh, bool fullscreen, irr::video::E_DRIVER_TYPE driverType);
        void renderer();
    };
}
#endif // ENGINE_H_INCLUDED
 
engine.cpp

Code: Select all

 
#include <irrlicht.h>
#include "engine.h"
 
using namespace Engine;
 
using namespace irr;
 
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
extern void CGame::initialize(int screenw, int screenh, bool fullscreen, irr::video::E_DRIVER_TYPE driverType)
{
    IrrlichtDevice *device = createDevice( driverType, core::dimension2d<u32>(screenw, screenh), 16, fullscreen, false, false, 0);
 
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();
}
extern void CGame::renderer()
{
    while(device->run())
    {
 
        driver->beginScene(true, true, SColor(255,100,101,140));
 
        smgr->drawAll();
        guienv->drawAll();
 
        driver->endScene();
    }
    device->drop();
}
 
Debug output:

Code: Select all

 
Building to ensure sources are up-to-date
Selecting target: 
Debug
Adding source dir: F:\Documents and Settings\lisac\My Documents\Projects\CPP\Game\
Adding source dir: F:\Documents and Settings\lisac\My Documents\Projects\CPP\Game\
Adding file: F:\Documents and Settings\lisac\My Documents\Projects\CPP\Misteries\Game.exe
Changing directory to: F:/DOCUME~1/lisac/MYDOCU~1/Projects/CPP/Game/.
Set variable: PATH=.;F:\Documents and Settings\lisac\My Documents\Projects\CPP\Misteries;F:\Program Files\CodeBlocks\MinGW\bin;F:\Program Files\CodeBlocks\MinGW;F:\WINDOWS\system32;F:\WINDOWS;F:\WINDOWS\system32\wbem
Starting debugger: F:\Program Files\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname  -quiet  -args F:/DOCUME~1/lisac/MYDOCU~1/Projects/CPP/MISTER~1/Game.exe
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 7.5
Child process PID: 3320
Program received signal SIGSEGV, Segmentation fault.
At F:\Documents and Settings\lisac\My Documents\Projects\CPP\Engine\engine.cpp:24
 
CuteAlien
Admin
Posts: 9933
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Sorry but code errors again :(

Post by CuteAlien »

A few problems. Let's start with the easy one. The functions Game::initialize and Game::renderer are member functions. You don't need the extern keyword for them. I must admit I don't even know what it would do in that case. The time you need extern usually is if you have c function (without a class) or static functions (in a class but marked static - those behave like c functions). Then put the extern in front of the declaration in the header file (not the cpp).

The next is that you should give your classes always constructors and initialize all member variables in there. Especially set all pointers to 0 (or NULL or nullptr - all more or less the same). That way you will find errors much faster. In this case the crash might have made more sense because you would have wondered why all variables in CGame::renderer are still 0. While because you have not initialized them they just have random values.

Which brings us to the last point - scopes. Variables in inner scopes always have precedence to variables in outer scopes. A class has a wider scope than a member-function because all variables in a class can also be accessed in all it's functions. So the class is the outer scope and the member-function is the inner scope. In this case that means when you create new variables in CGame::initialize which have the same names as those in the CGame class - then the function variables are used. The class variables are not touched at all because they are shadowed by the function variables. So you create and set for example a 'driver' variable which is only valid until the CGame::initialize function ends. While the CGame::driver variable is never even set - so it just contains a random value.

Solution: 1. initialize your variables (always do that!). 2. Don't create new variables with the same name as your class has inside functions. All you need is for example: driver = device->getVideoDriver(); Same for the other variables.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Sorry but code errors again :(

Post by thanhle »

Hi what CuteAlien means is this:

Code: Select all

 
 
void CGame::initialize(int screenw, int screenh, bool fullscreen, irr::video::E_DRIVER_TYPE driverType)
{
   device = createDevice( driverType, core::dimension2d<u32>(screenw, screenh), 16, fullscreen, false, false, 0);
 
 driver =  device->getVideoDriver();
    smgr = device->getSceneManager();
    //guienv = device->getGUIEnvironment();  //I don't know about gui. Let's comment it out
}
 
 
void CGame::renderer()
{
    while(device->run())
    {
 
        driver->beginScene(true, true, SColor(255,100,101,140));
 
        smgr->drawAll();
      //  guienv->drawAll();
 
        driver->endScene();
    }
    device->drop(); 
}
 
CuteAlien
Admin
Posts: 9933
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Sorry but code errors again :(

Post by CuteAlien »

And this:

Code: Select all

 
CGame::CGame()
: device(0)
, driver(0)
, smgr(0)
, guienv(0)
{
}
 
Yes, it works without that... but it's a good habit to get into doing that. And a very bad habit not doing that.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
lisacvuk
Posts: 21
Joined: Thu Apr 17, 2014 4:50 pm

Re: Sorry but code errors again :(

Post by lisacvuk »

err... you were really helpfull, but I still get the same error.
new engine.h

Code: Select all

#ifndef ENGINE_H_INCLUDED
#define ENGINE_H_INCLUDED
#include <irrlicht.h>
namespace Engine
{
    class CGame
    {
    public:
        CGame();
        irr::IrrlichtDevice *device;
        irr::video::IVideoDriver* driver;
        irr::scene::ISceneManager* smgr;
        irr::gui::IGUIEnvironment* guienv;
        void initialize(int screenw, int screenh, bool fullscreen, irr::video::E_DRIVER_TYPE driverType);
        void renderer();
    };
}
#endif // ENGINE_H_INCLUDED
 
new engine.cpp

Code: Select all

#include <irrlicht.h>
#include "engine.h"
 
using namespace Engine;
 
using namespace irr;
 
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
CGame::CGame()
: device(0)
, driver(0)
, smgr(0)
, guienv(0)
{
}
void CGame::initialize(int screenw, int screenh, bool fullscreen, irr::video::E_DRIVER_TYPE driverType)
{
    IrrlichtDevice *device = createDevice( driverType, core::dimension2d<u32>(screenw, screenh), 16, fullscreen, false, false, 0);
 
    driver = device->getVideoDriver();
    smgr = device->getSceneManager();
    guienv = device->getGUIEnvironment();
}
void CGame::renderer()
{
    while(device->run())
    {
 
        driver->beginScene(true, true, SColor(255,100,101,140));
 
        smgr->drawAll();
        guienv->drawAll();
 
        driver->endScene();
    }
    device->drop();
}
 
 
Havent changed main.cpp
CuteAlien
Admin
Posts: 9933
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Sorry but code errors again :(

Post by CuteAlien »

It should now crash in while (device->run()). And if you take a look in the debugger you'll see that the 'device' there is now 0 at that point. And the reason is the same as before - you create a new IrrlichtDevice variable called 'device' which hides the 'device' variable in your class. Just stop doing that :-)

Btw - now that you have initializied all your member-variables you can do nifty things to find your bugs like checking if a variable is find.
So for example:

Code: Select all

 
if (!device)
   return;
 
before using that variable would allow you to quit without a crash. Or print some error message etc.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Sorry but code errors again :(

Post by thanhle »

Compare carefully mine initialize function to yours.
You have the device as local variable.

Regards
Thanh
lisacvuk
Posts: 21
Joined: Thu Apr 17, 2014 4:50 pm

Re: Sorry but code errors again :(

Post by lisacvuk »

err... I'm really sorry if but I'm too stupid to understand that. Could you please give me fixed part of the code so I could understand it from that? Thanks in advance, lisacvuk.
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Sorry but code errors again :(

Post by thanhle »

Hi mate,
remove the red part below. Because that makes device object local.

void CGame::initialize(int screenw, int screenh, bool fullscreen, irr::video::E_DRIVER_TYPE driverType)
{
IrrlichtDevice * device = createDevice( driverType, core::dimension2d<u32>(screenw, screenh), 16, fullscreen, false, false, 0);

driver = device->getVideoDriver();
smgr = device->getSceneManager();
guienv = device->getGUIEnvironment();
}
Since you want to have it global, we would remove it.

Regards
thanh
lisacvuk
Posts: 21
Joined: Thu Apr 17, 2014 4:50 pm

Re: Sorry but code errors again :(

Post by lisacvuk »

Thanks! I finaly managed to run it without error!
lisacvuk
Posts: 21
Joined: Thu Apr 17, 2014 4:50 pm

Re: Sorry but code errors again :(

Post by lisacvuk »

I hope this is not a bump, but I made another error in the code. I tried to write a functon for loading map and for loading scene.
engine.cpp

Code: Select all

#include <irrlicht.h>
#include <iostream>
 
#include "engine.h"
 
#include "MastEventReciever.cpp"
 
using namespace std;
 
using namespace Engine;
 
using namespace irr;
 
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
void CGame::initialize(int screenw, int screenh, bool fullscreen, irr::video::E_DRIVER_TYPE driverType)
{
    event_rec.init();
    device = createDevice( driverType, core::dimension2d<u32>(screenw, screenh), 16, fullscreen, false, false, 0);
    device->setEventReceiver(&event_rec);
    if(!device)
    {
            cout << "ERROR: Could not locate device. Initialization of device has probably gone wrong." << endl;
            return;
    }
    cout << "LOG: Device initialized." << endl;
    driver = device->getVideoDriver();
    if(!driver)
    {
            cout << "ERROR: Could not locate driver. Initialization of driver has probably gone wrong." << endl;
            return;
    }
    smgr = device->getSceneManager();
    cout << "LOG: Driver initialized." << endl;
    if(!smgr)
    {
            cout << "ERROR: Could not locate screen manager. Initialization of screen manager has probably gone wrong." << endl;
            return;
    }
    cout << "LOG: Screen Manager initialized." << endl;
    guienv = device->getGUIEnvironment();
    if(!guienv)
    {
            cout << "ERROR: Could not locate GUI environment. Initialization of gui environment has probably gone wrong." << endl;
            return;
    }
    cout << "LOG: GUI Environment initialized." << endl;
    IMetaTriangleSelector * meta = smgr->createMetaTriangleSelector();
    if(!meta)
    {
            cout << "ERROR: Could not locate meta triangle selector. Initialization of meta triangle selector has probably gone wrong." << endl;
            return;
    }
    cout << "LOG: Meta triangle selector initialized." << endl;
}
void CGame::renderer()
{
    while(device->run())
    {
        event_rec.endEventProcess();
 
        if(event_rec.keyDown(KEY_ESCAPE))
        {
                cout << "LOG: Escape key pressed./nLOG: Shutting down..." << endl;
                return;
        }
        driver->beginScene(true, true, SColor(255,100,101,140));
 
        smgr->drawAll();
        guienv->drawAll();
 
        driver->endScene();
        event_rec.startEventProcess();
    }
    device->drop();
}
void CGame::loadMap(irr::io::path& mapFile)
{
    mapMesh = smgr->getMesh(mapFile);
}
void CGame::loadScene(irr::io::path& sceneFile)
{
    smgr->loadScene(sceneFile);
}
 
engine.h

Code: Select all

 
#ifndef ENGINE_H_INCLUDED
#define ENGINE_H_INCLUDED
#include <irrlicht.h>
#include "MastEventReciever.cpp"
 
namespace Engine
{
    class CGame
    {
    public:
        irr::IrrlichtDevice *device;
        irr::video::IVideoDriver* driver;
        irr::scene::ISceneManager* smgr;
        irr::gui::IGUIEnvironment* guienv;
        irr::scene::IMetaTriangleSelector* meta;
        irr::scene::IAnimatedMesh* mapMesh;
        MastEventReceiver event_rec;
 
        void initialize(int screenw, int screenh, bool fullscreen, irr::video::E_DRIVER_TYPE driverType);
        void renderer();
        void loadMap(irr::io::path& mapFile);
        void loadScene(irr::io::path& sceneFile);
    };
}
#endif // ENGINE_H_INCLUDED
 
main.cpp

Code: Select all

#include <engine.h>
#include <irrlicht.h>
 
using namespace irr;
using namespace video;
using namespace Engine;
 
int main()
{
    CGame Game;
    io::path filename = "./test.irr";
    Game.loadScene(filename);
    Game.initialize(640, 480, 0, EDT_OPENGL);
    Game.renderer();
    return 0;
}
 
debug log

Code: Select all

Building to ensure sources are up-to-date
Selecting target: 
Debug
Adding source dir: F:\Documents and Settings\lisac\My Documents\Projects\CPP\Game\
Adding source dir: F:\Documents and Settings\lisac\My Documents\Projects\CPP\Game\
Adding file: F:\Documents and Settings\lisac\My Documents\Projects\CPP\Misteries\Game.exe
Changing directory to: F:/DOCUME~1/lisac/MYDOCU~1/Projects/CPP/Game/.
Set variable: PATH=.;F:\Documents and Settings\lisac\My Documents\Projects\CPP\Misteries;F:\Program Files\CodeBlocks\MinGW\bin;F:\Program Files\CodeBlocks\MinGW;F:\WINDOWS\system32;F:\WINDOWS;F:\WINDOWS\system32\wbem;F:\Program Files\CMake\bin
Starting debugger: F:\Program Files\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname  -quiet  -args F:/DOCUME~1/lisac/MYDOCU~1/Projects/CPP/MISTER~1/Game.exe
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 7.5
Child process PID: 2820
Program received signal SIGSEGV, Segmentation fault.
At F:\Documents and Settings\lisac\My Documents\Projects\CPP\Engine\engine.cpp:87
 
CuteAlien
Admin
Posts: 9933
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Sorry but code errors again :(

Post by CuteAlien »

In which line does it crash?
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
lisacvuk
Posts: 21
Joined: Thu Apr 17, 2014 4:50 pm

Re: Sorry but code errors again :(

Post by lisacvuk »

at smgr->loadScene(sceneFile); in engine.h
CuteAlien
Admin
Posts: 9933
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Sorry but code errors again :(

Post by CuteAlien »

You try to load the scene before you initialize the engine. At that point smgr is still null (as you set it in intialize).

Note that you can see this things in a debugger. The moment it crashes you can still see the value all variables have in your IDE. So when you have a crash you can check them out directly (depending on IDE it might show the value already when you go over them with your mouse - in other IDE's you might have to create a "watch" for them).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
lisacvuk
Posts: 21
Joined: Thu Apr 17, 2014 4:50 pm

Re: Sorry but code errors again :(

Post by lisacvuk »

Oh! Thanks, that worked! I spent 2 days checking for solution with the engine code. It works now.
Post Reply