Segmentation fault (bug in irrlicht?)

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
limvot
Posts: 20
Joined: Sun Sep 09, 2007 2:41 am

Segmentation fault (bug in irrlicht?)

Post by limvot »

I have been getting a segmentation fault with this program, after it compiles and i try to run it I get a segmentation fault. (it compiles fine)

Here is my code.

The error is in gamemanager.cpp, when i do GameMngrCMngr->AddCollisionCam()

(around line 30)

http://limvot.x10hosting.com/tests/uploads/newoo.zip

Please help, i have been expermienting for 3 days! :shock: :!:

EDIT: there are some other bugs in the code or something too, + i have changed some things. I still come back to the same problem though, and would love to have some help on it. (when creating an instance of Collision Maker you must specify the irrlicht device as a peramater)
Last edited by limvot on Sat May 31, 2008 11:19 pm, edited 1 time in total.
Hi me noob so help me.
xDan
Competition winner
Posts: 673
Joined: Thu Mar 30, 2006 1:23 pm
Location: UK
Contact:

Post by xDan »

If you wanted help really badly you would also post relevant code snippets... SURELY!

Also your website does not work for me.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Use a debugger to find which pointer is a null pointer (or even just uninitialized).
limvot
Posts: 20
Joined: Sun Sep 09, 2007 2:41 am

Post by limvot »

Sorry everybody! I messed up. Here are the relevent code snippits.
(my websight went down as soon as i uploaded)

It crashes as soon as you call a function to collisionmanager (from gamemanager.cpp). Highlighted in bold
**Sorry ther're are bold tags that don;t work
In gamemanager.cpp - Contains the main game loop

Code: Select all

#include <iostream>
#include "gamemanager.h"
#include "core.h"

GameManager::GameManager()
{
}

//! Main game loop
bool GameManager::run()
{
   IrrlichtDevice* pDevice = 0;
   IVideoDriver* pDrv = 0;
   ISceneManager* pSceneMngr = 0;
   IGUIEnvironment* pGUIEnv = 0;
   [b]CollisionMaker* GameMngrCMngr = 0;[/b]

// Name of game, displayed in window if windowed
gCore.getDevice()->setWindowCaption(L"Power Paintbal - 0.3 - ALPHA");
   
int lastFPS = -1; //<-----doesnt work because there is no code to display   
                        // fps in this example but needed to compile sucessfuly

    //load scene using our pointer we made for the


 [b]  std::cout<<"   Setup!\n";
GameMngrCMngr = gCore.getCollisionMngr();
std::cout<<"   Done! Loading Cam...\n";
GameMngrCMngr->AddCollisionCam(); [b]//crash here![/b]
std::cout<<"   Loading scene...\n";
GameMngrCMngr->LoadScene("scenes/Scene.irr");
   std::cout<<" Scene 1 Loaded......\n";[/b]

 
// Keep running game loop if device exists
   while(gCore.getDevice()->run())
      {
      gCore.getVideo()->beginScene(true,true, SColor(255, 100, 101, 140));
      gCore.getSceneMngr()->drawAll();
      gCore.getGUIEnv()->drawAll();
      gCore.getVideo()->endScene();
    }
   
   {
   gCore.getDevice()->drop();
}
   
   return 0;
} 
core.cpp (entire)-

Code: Select all

#include "core.h"
#include <iostream>



//constructor
CGameCore::CGameCore()
{
   InitalizeDevice();
   std::cout<<"Game Manager Created !\n";
}

//Default Constructor
CGameCore::~CGameCore()
{
   std::cout<<"Game Manager Shut Down!!\n";
}

/* in thefuntion below we have 2 device types one that uses a directx 
    device with decent resolution settings and one using OpenGL with
    minimal settings set up in an if statment. one cool thing about
    doing ot this way is that if you have another app running that
    wont release the directx driver, the game manager object
    will automagicly use the openGL device instead and tell you
    that its using it if you have a console app or a drop down style
    console in your app ( quake style ) */
 
void CGameCore::InitalizeDevice()
{

      pDevice = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(800,600),32,false,false,false);
  std::cout<<"Creation Sucsess !!\n";


   //Init engine functions and store the pointers to them
   pSceneMngr = pDevice->getSceneManager();
   pDrv = pDevice->getVideoDriver();
   pGUIEnv = pDevice->getGUIEnvironment();
   [b]CollisionMngr->Init(pDevice);[/b]

}


      //return a funtion to engine core fuctions here
      IrrlichtDevice* CGameCore::getDevice()
   {
      return pDevice;
   }
 
      // GUI
   IGUIEnvironment* CGameCore::getGUIEnv()
   {
      return pGUIEnv;
   }

   //Video
   IVideoDriver* CGameCore::getVideo()
   {
      return pDrv;
   }

   //Scene Manager
   ISceneManager* CGameCore::getSceneMngr()
   {
      return pSceneMngr;
   }
[b]
   CollisionMaker* CGameCore::getCollisionMngr()
   {
      return CollisionMngr;
   }[/b]

core.h(entire)

Code: Select all

#ifndef _CORE_H_
#define _CORE_H_

#include <irrlicht.h>
#include "collision.h"

//engine namespaces
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;


class CGameCore   //here we define the class and create our poiners
{
public:
   //functions providing acess to engine componants.
 
[b]   //Colision Maker
   CollisionMaker* getCollisionMngr();[/b]

   //GUI Environment
    IGUIEnvironment* getGUIEnv();

   //Scene Manager
   ISceneManager* getSceneMngr();

   //video driver
   IVideoDriver* getVideo();

   //Engine core device
   IrrlichtDevice* getDevice();
   


   //constructor
   CGameCore();

   //destructor
   virtual ~CGameCore();


   //used to insanianate device
   void InitalizeDevice();

   //corisponding pointers to engine core functions
   IrrlichtDevice* pDevice;
   IVideoDriver* pDrv;
   ISceneManager* pSceneMngr;
   IGUIEnvironment* pGUIEnv;
[b]   CollisionMaker* CollisionMngr;[/b]

};

#endif 
collision.cpp

Code: Select all

#include "collision.h"

#include <iostream>











//Default Constructor

CollisionMaker::~CollisionMaker()

{

   std::cout<<"Collision Manager Shut Down!!\n";

}





CollisionMaker::CollisionMaker()

{

std::cout<<"Collision Manager Started Up!!\n";

}
void CollisionMaker::Init(irr::IrrlichtDevice* pDevice)
{
   std::cout<<"Collision Manager Initulizd!!\n";

   mDevice = pDevice;

   mSelector = mDevice->getSceneManager()->createMetaTriangleSelector();
}



irr::scene::IMetaTriangleSelector* CollisionMaker::getSelector()

{

   return mSelector;

}





irr::io::IAttributes* CollisionMaker::createUserData (irr::scene::ISceneNode *forSceneNode)

{

   //Sending 0 caus' we don't care

   return (irr::io::IAttributes*)0;

}





void CollisionMaker::OnReadUserData (irr::scene::ISceneNode *forSceneNode, irr::io::IAttributes *userData)

{

   //if the attribute is not set, it's considered false. If it's false, we don't do anything.

   if(userData->getAttributeAsBool("Collidee"))

   {

      //Detecting node type

      if(forSceneNode->getType()==irr::scene::ESNT_OCT_TREE)

      {

         //Node is an octree, let's get mesh info

         irr::io::IAttributes *attributes = mDevice->getFileSystem()->createEmptyAttributes();

         forSceneNode->serializeAttributes(attributes);

         irr::scene::IAnimatedMesh* mesh = mDevice->getSceneManager()->getMesh(attributes->getAttributeAsString("Mesh").c_str());

         attributes->drop();

         //Creating selector based on mesh

         irr::scene::ITriangleSelector* thisSelector = mDevice->getSceneManager()->createOctTreeTriangleSelector(mesh->getMesh(0), forSceneNode, 128);

         //Adding the selector to the meta selector

         mSelector->addTriangleSelector(thisSelector);

         thisSelector->drop();

      } else {

if (forSceneNode->getType() == scene::ESNT_ANIMATED_MESH)

  {

    scene::IAnimatedMeshSceneNode* msn = (scene::IAnimatedMeshSceneNode*)forSceneNode;



         irr::scene::ITriangleSelector* thisSelector =

              mDevice->getSceneManager()->createTriangleSelector(msn->getMesh()->getMesh(0), forSceneNode);

         //And we add it to the metaSelector

         mSelector->addTriangleSelector(thisSelector);

         thisSelector->drop();







      } else {



         //Node isn't an octree or animated mesh, so we build a triangle selector based on bounding box

         irr::scene::ITriangleSelector* thisSelector = mDevice->getSceneManager()->createTriangleSelectorFromBoundingBox(forSceneNode);

         //And we add it to the metaSelector

         mSelector->addTriangleSelector(thisSelector);

         thisSelector->drop();

         std::cout<< "DRAT! Bounding box!";

      }

      }

   }

   if(userData->getAttributeAsBool("Collider"))

   {

      //Now we animate nodes that are supposed to respond to collisions based on the metaselector

      //create an animator using the metaselector. Collision values are arbitrary. Elipsoid is made according to mesh bounding box

      irr::scene::ISceneNodeAnimatorCollisionResponse* anim = mDevice->getSceneManager()->createCollisionResponseAnimator(

         mSelector, forSceneNode, forSceneNode->getBoundingBox().MaxEdge,

         irr::core::vector3df(0,-1,0), irr::core::vector3df(0,0,0));

      //Adding the animator to the node

      forSceneNode->addAnimator(anim);



      anim->drop();

      std::cout<< "here at collider";

   }

}





void CollisionMaker::LoadScene(const c8 *filename)

{

   mDevice->getSceneManager()->loadScene(filename, this);

}



void CollisionMaker::AddCollisionCam ()

{

//This is my code! I adapted the collision camera code from the collision tut

//Basically I added a new function that creates a camera that collides with stuff

//to the class. I did it in the class for neatness and because all of the stuff was declared here

//any way. As to the changes to the Collision tut code i replaces smgr with

//mDevice->getSceneManager() this gets the secen manager from mDevice. Also selcector (one mesh)

//is replaced with the mSlector (definged eirler, contains sub slectors in one big slector)







	/*

	We add a first person shooter camera to the scene for being able to move in the quake 3

	level like in tutorial 2. But this, time, we add a special animator to the

	camera: A Collision Response animator. This thing modifies the scene node to which

	it is attached to in that way, that it may no more move through walls and is affected

	by gravity. The only thing we have to tell the animator is how the world looks like,

	how big the scene node is, how gravity and so on. After the collision response animator

	is attached to the camera, we do not have to do anything more for collision detection,

	anything is done automaticly, all other collision detection code below is for picking.

	And please note another cool feature: The collsion response animator can be attached

	also to all other scene nodes, not only to cameras. And it can be mixed with other

	scene node animators. In this way, collision detection and response in the Irrlicht

	engine is really, really easy.

	Now we'll take a closer look on the parameters of createCollisionResponseAnimator().

	The first parameter is the TriangleSelector, which specifies how the world, against

	collision detection is done, looks like. The second parameter is the scene node, which

	is the object, which is affected by collision detection, in our case it is the camera.

	The third defines how big the object is, it is the radius of an ellipsoid. Try it out

	and change the radius to smaller values, the camera will be able to move closer to walls

	after this. The next parameter is the direction and speed of gravity. You could

	set it to (0,0,0) to disable gravity. And the last value is just a translation: Without

	this, the ellipsoid with which collision detection is done would be around the camera,

	and the camera would be in the middle of the ellipsoid. But as human beings, we are

	used to have our eyes on top of the body, with which we collide with our world, not

	in the middle of it. So we place the scene node 50 units over the center of the

	ellipsoid with this parameter. And that's it, collision detection works now.

	*/







		scene::ICameraSceneNode* camera =

		mDevice->getSceneManager()->addCameraSceneNodeFPS(0, 100.0f, 300.0f, -1, 0, 0, true);

	camera->setPosition(core::vector3df(0,1300,0));





	if (mSelector)

	{

		scene::ISceneNodeAnimator* animcam = mDevice->getSceneManager()->createCollisionResponseAnimator(

			mSelector, camera, core::vector3df(10,50,10),

			core::vector3df(0,-0.5,0),

			core::vector3df(0,50,0));

		mSelector->drop();

		camera->addAnimator(animcam);

		animcam->drop();

	}

}


collision.h

Code: Select all





#include <irrlicht.h>



//engine namespaces

using namespace irr;





class CollisionMaker : public irr::scene::ISceneUserDataSerializer

{

   irr::IrrlichtDevice* mDevice;

   irr::scene::IMetaTriangleSelector* mSelector;

public:

   CollisionMaker();

   irr::scene::IMetaTriangleSelector* getSelector();

   irr::io::IAttributes* createUserData (irr::scene::ISceneNode *forSceneNode); //override from

   void OnReadUserData (irr::scene::ISceneNode *forSceneNode, irr::io::IAttributes *userData);

   void LoadScene(const c8 *filename);

   void AddCollisionCam();
   void Init(irr::IrrlichtDevice* pDevice);

   //destructor

   virtual ~CollisionMaker();



};


main.cpp

Code: Select all

#include <irrlicht.h>
#include <iostream>

//new classes
#include "core.h"
#include "gamemanager.h"



using namespace irr;

#pragma comment(lib, "Irrlicht.lib")

//this will disable the console window on startup when implemented
//#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")



int main()
{

   GameManager game;
   game.run();

}
Hi me noob so help me.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I don't see any code that allocates a CollisionMaker object, and I don't see any code that assigns CGameCore::CollisionMngr. You call getCollisionMngr() and you are going to get a bad pointer. A total of about 5 seconds in the debugger [maybe 60 if you're new to debuggers] would have told you exactly what the problem was.

Travis
limvot
Posts: 20
Joined: Sun Sep 09, 2007 2:41 am

Post by limvot »

I am sorry I don't know what you mean. I have been using a debugger,
and the program seams to crash when i assign mDevice = pDevice in collisiion.cpp. as for allocateing a CollisionMaker object, isn't that done in core.h? and it is initulized in core.cpp?

I am very sorry, i am somewhat new to c++ programming.

What would you sugest i change it too? am i missing something?

I really appreciete your help.
Hi me noob so help me.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

In core.h you have a CollisionMaker pointer, this does NOT create a CollisionMaker object for you. To do that you shouldn't use a pointer or you should (in the GameCore constructor) actually create the object and assign it to the pointer; e.g collision_maker = new CollisionMaker(). Before you do that you just have a pointer which points to an arbitrary piece of memory so when you use it you'll get the segmentation fault.

If you don't understand pointers then you really shouldn't try and use them because you will run into problems that will be very hard for you to fix yourself and it's not good for your learning of language if you get other people to fix it for you all the time because you'll never learn yourself. So i'd suggest googling some info on C++ pointers and really trying to understand how they work, why they're used and how they're used.

Don't take this the wrong way, i struggled with pointers myself when i started C++!
Image Image Image
Post Reply