Pointers passing problem [SOLVED]

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
Iyad
Posts: 140
Joined: Sat Mar 07, 2009 1:18 am
Location: Montreal, Canada

Pointers passing problem [SOLVED]

Post by Iyad »

Hi everyone.

I'm trying to make a kind of game framework fully OOP so i can use it in my future games without having any problem.

The major problem is when i try to pass pointers between each classes, not any pointer, the IrrlichtDevice only, I dont know why, it compiles perfectly but the application crashes instantly after execution.

Heres a bit of code :

Manager.h

Code: Select all

#ifndef _MANAGER_H
#define _MANAGER_H
 
#include <irrlicht.h>
#include <stdlib.h>
#include <stdio.h>
 
#include "CReceveur.h"
#include "CEtatdeJeu.h"
 
    #define RESX 800
    #define RESY 600
    #define BITS 032
    #define SCRN 000
    #define STBF 000
    #define VSNC 000
    #define ANTA 000
 
class CManager
{
    public :
 
        CManager();
        virtual ~CManager();
 
        irr::IrrlichtDevice* getDevice();
 
        irr::video::IVideoDriver* getVideo();
        irr::scene::ISceneManager* getSmgr();
        irr::gui::IGUIEnvironment* getGUIe();
 
        void InitialiserDevice(void);
        void LireConfig(void);
        bool RenduTR(void);
 
    private :
 
        irr::IrrlichtDevice* device;
        irr::SIrrlichtCreationParameters param;
 
        irr::video::IVideoDriver* driver;
        irr::scene::ISceneManager* smgr;
        irr::gui::IGUIEnvironment* gui;
 
        CReceveur Receveur;
        CEtatdeJeu* JeuRendu;
 
        FILE* Config;
};
 
#endif
 
Manager.cpp

Code: Select all

#include "CManager.h"
 
using namespace irr;
 
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
CManager::CManager()
{
    Config = NULL;
    LireConfig();
    JeuRendu = new CEtatdeJeu(device);
}
 
CManager::~CManager()
{
}
 
void CManager::InitialiserDevice(void)
{
    device = createDeviceEx(param);
    device->setWindowCaption(L"GAMEFRAMEWORK-IYAD, FPS-1");
    device->setEventReceiver(&Receveur);
 
    driver = device->getVideoDriver();
    smgr = device->getSceneManager();
    gui = device->getGUIEnvironment();
 
    RenduTR();
}
 
bool CManager::RenduTR(void)
{
    while(device->run())
    {
        driver->beginScene(true, true,
        SColor(0,200,200,200));
 
        JeuRendu->ToutModes();
 
        smgr->drawAll();
        gui->drawAll();
 
        driver->endScene();
    }
    device->drop();
    return false;
}
 
void CManager::LireConfig(void)
{
    Config = fopen("Configuration.txt", "r");
    if (Config != NULL)
    {
        //Load paramaters for the graphics
        fclose(Config);
    }
    else
    {
        param.DriverType = EDT_OPENGL;
        param.WindowSize = dimension2d<s32>(
                                            RESX,
                                            RESY);
        param.Bits =                        BITS;
        param.Fullscreen =                  SCRN;
        param.Stencilbuffer =               STBF;
        param.Vsync =                       VSNC;
        param.AntiAlias =                   ANTA;
    }
}
 
IrrlichtDevice* CManager::getDevice()
{return device;}
 
IVideoDriver* CManager::getVideo()
{return driver;}
 
ISceneManager* CManager::getSmgr()
{return smgr;}
 
IGUIEnvironment* CManager::getGUIe()
{return gui;}
 
EtatdeJeu.h (this chek the game state and calls the specefic functions)

Code: Select all

 
#ifndef ETATDEJEU_H
#define ETATDEJEU_H
 
#include <irrlicht.h>
 
#include "CMenu.h"
 
    #define MODE1 1
    #define MODE2 2
 
class CEtatdeJeu
{
    public :
 
        CEtatdeJeu(irr::IrrlichtDevice* deviceX);
        virtual ~CEtatdeJeu();
 
        void ToutModes(void);
        void Mode1Calc(void);
        void Mode2Calc(void);
 
        irr::IrrlichtDevice* retDev(void);
 
    private :
 
        int Mode;
 
        irr::IrrlichtDevice* deviceNEW;
 
        //CMenu* Menu;
};
 
#endif
 
EtatdeJeu.cpp

Code: Select all

 
#include "CEtatdeJeu.h"
 
using namespace irr;
 
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
CEtatdeJeu::CEtatdeJeu(IrrlichtDevice* deviceX)
{
    Mode      = MODE1;
    deviceNEW = deviceX;
 
    /*
    HERES THE PROBLEM : i try to call a function that uses the device and it crashes. Like that it open perfectly, but when i add for example 
deviceNEW->setWindowCaption(L"BUG!"); it makes me a fatal error
*/
    //Menu = new CMenu(deviceNEW);
}
 
CEtatdeJeu::~CEtatdeJeu()
{
}
 
void CEtatdeJeu::ToutModes(void)
{
    if (Mode == MODE1)
    {
    }
    if (Mode == MODE2)
    {
    }
}
 
void CEtatdeJeu::Mode1Calc(void)
{
}
 
void CEtatdeJeu::Mode2Calc(void)
{
}
 
IrrlichtDevice* CEtatdeJeu::retDev(void)
{return deviceNEW;}
 
Any help would be greatly appriciated, im still learning programming and i mostly learn by myself, by my errors and by reading a lot on this forum, but pointers are killing me, i had made tons of tests but it still dont work.

Thanks for your replies.
Last edited by Iyad on Sat Jul 16, 2011 9:28 pm, edited 3 times in total.
#include <Iyad.h>
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

Have you tried a debugger? If so, what line is it giving you an error on?
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

Code: Select all

CManager::CManager() 
{ 
    Config = NULL; 
    LireConfig(); 
    JeuRendu = new CEtatdeJeu(device); // is device valid here?
} 

maybe something like this?

Code: Select all

CManager::CManager() 
{ 
    Config = NULL; 
    LireConfig(); 
    InitialiserDevice();
    JeuRendu = new CEtatdeJeu(device); // is device valid here?
} 
Iyad
Posts: 140
Joined: Sat Mar 07, 2009 1:18 am
Location: Montreal, Canada

Post by Iyad »

@Lonesome Ducky
I get no error at any line, it crashes at execution. I know what is a debugger but i dont know how the hell i should use it, each time I press on debug it give tons of text that i dont understand. Anyways...
The problem is when i try to call a function like (device->setWindowCaption), using my device, it simply crashes when i open my app.

Thank you for trying to help me.
Last edited by Iyad on Sat Aug 22, 2009 2:38 am, edited 1 time in total.
#include <Iyad.h>
Iyad
Posts: 140
Joined: Sat Mar 07, 2009 1:18 am
Location: Montreal, Canada

Post by Iyad »

@Seven
the initialisation of the device if made in the main function. It taught it was the solution but i cheked my main()... anyways. Thx for trying to help.
#include <Iyad.h>
Iyad
Posts: 140
Joined: Sat Mar 07, 2009 1:18 am
Location: Montreal, Canada

Post by Iyad »

If it could help someone helping me :roll: , i tryed to debug the program and it says that i have a Segmentation fault SIGSEGV (what the heck?)

this is what he is trying to tell me.

Building to ensure sources are up-to-date
Build succeeded
Selecting target:
EXE
Adding source dir: C:\Documents and Settings\Amer\Desktop\FPS\
Adding source dir: C:\Documents and Settings\Amer\Desktop\FPS\
Changing directory to: C:/DOCUME~1/Amer/Desktop/C__~1/IRRLIC~1.5/bin/WIN32-~1
Adding file: EXE\FPS.exe
Starting debugger:
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
(no debugging symbols found)
Debugger name and version: GNU gdb 6.7.50.20071127
Child process PID: 948
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
Error while mapping shared library sections:
Program received signal SIGSEGV, Segmentation fault.
In ?? () ()

And i also had a message that told me if i wanted to look at the backtrace: it only shows some adresses in a window.
#include <Iyad.h>
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

Iyad wrote:@Seven
the initialisation of the device if made in the main function. It taught it was the solution but i cheked my main()... anyways. Thx for trying to help.
you are not initializing the device before the contructor is called. When you declare the variable, the constructor is called. you are then using an undefined device in your call

Code: Select all

JeuRendu = new CEtatdeJeu(device); // is device valid here? 

walk it backwards...

deviceX = whatever you pass to CEtatdeJeu::CEtatdeJeu(IrrlichtDevice* deviceX) throught his line

Code: Select all

deviceNEW = deviceX;
which is called by this line

Code: Select all

JeuRendu = new CEtatdeJeu(device); // is device valid here? 
and at this point, you have not called initialize() yet because it is in the constructor.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

this will work fine

Code: Select all

class myClass
{
   myClass()
   {
        Initialize();
        m_Device->DoSomething()
   }

    void Initialize()
    {
       m_Device = new Device();
    }
}


main()
{
   myClass a;
}

Code: Select all

class myClass
{
   myClass()
   {
        m_Device->DoSomething()
   }

    void Initialize()
    {
       m_Device = new Device();
    }
}


main()
{
   // this will crash because the constructor is called right here
   myClass a;

   // it is too late to call initialize() because the contructor already tried to use the device 
   a.Initialize();
}

Last edited by Seven on Sat Aug 22, 2009 3:06 am, edited 1 time in total.
Iyad
Posts: 140
Joined: Sat Mar 07, 2009 1:18 am
Location: Montreal, Canada

Post by Iyad »

Thanks alot seven
Last edited by Iyad on Fri Mar 04, 2011 10:19 pm, edited 1 time in total.
#include <Iyad.h>
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

if you dont mind me giving unsolicited advice :)

I recommend that you only set variables to a known value in the constructor and then create the device in a Create() function. the reason is that a constructor cannot return a value to you. for example :

Code: Select all

class myClass 
{ 
   myClass() 
   { 
       device = NULL;
       another variable = 0; 
       another variable2 = 0; 
       another variable3 = 0; 
       another variable4 = 0; 
   } 

   bool Initialize()
  {
      device = new device();
      if (device is ok and no error) return true; 
        else { print error message; return false; }
  }

} 


main() 
{ 
   myClass a; 
   if !(a.Initialize()) return -1;

   // use 'a.device' as much as you like, because you know that it is valid
} 

Post Reply