CTD during renderloop

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
TheGolem

CTD during renderloop

Post by TheGolem »

Hi folks

I'm stumped again. I am working on a small space shooter. When things got too complicated (:roll:), I chopped up my code and turned it into a class structure. The result: bugger won't run anymore, the screen would turn black at start, then crashing back to desktop. I was commenting out and commenting out, and putting in some very badly written logfile, and after reducing the entire program to one class which does basically nothing except creating an irrlicht device and running it until somebody presses alt+f4, i found out that somehow, my rendering loop which is nothing out of the extraordinary is the reason. The shortened version of my code is 3 files, I am including those into this post, and also the generated log (you will see that it should be much longer if everything would work as planned). yes i know the logging is worst coding ever, i was just hacking it together in 2 minutes :P Oh yeah, working title of my game is "Beer" (dont ask).

// main.cpp
#include <windows.h>
#include <stdio.h>
#include "beer.h"

CBeer Beer;

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
if (!Beer.Init())
{
return 1;
}
Beer.Run();
return 0;
}

----------------------------------------------------------------------------------

// beer.h
#pragma once
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

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

class CBeer
{
private:
IrrlichtDevice* Device;
IVideoDriver* Driver;
ISceneManager* Smgr;
IGUIEnvironment* GuiEnv;
ICameraSceneNode* Camera;
public:
CBeer(void);
~CBeer(void);
bool Init(void);
bool Run(void);
bool HandleEvent(SEvent Event);
bool Render(void);
};

----------------------------------------------------------------------------------

// beer.cpp
#include <stdio.h>
#include <stdlib.h>
#include ".\beer.h"

extern CBeer Beer;
FILE* Logfile;

void Log(char* Message)
{
Logfile = fopen("log.txt", "a+t");
fprintf(Logfile, Message);
fclose(Logfile);
}

CBeer::CBeer(void)
{
}

CBeer::~CBeer(void)
{
}

class CMyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
return Beer.HandleEvent(event);
}
};

bool CBeer::Init(void)
{
if (Logfile == 0)
Logfile = fopen("log.txt", "w+t");
else
fprintf(Logfile, "WARNING: CBeer Instance already present at Logfile creation!\n");
if (Logfile == 0)
return false;
Log("Logfile created.\n");
Log("CBeer Instance created.\n");
Log("Initializing Event receiver...");
CMyEventReceiver Receiver;
Log("done\n");
Log("Creating Irrlicht Device...\n");
Device = createDevice(EDT_DIRECTX8, dimension2d<s32>(1024, 768), 32, true, true, &Receiver);
Log("...done\n");

Driver = Device->getVideoDriver();
Smgr = Device->getSceneManager();
GuiEnv = Device->getGUIEnvironment();
Log("Driver, Scene Manager and GUI Environment initialized.\n");
return true;
}

bool CBeer::HandleEvent(SEvent Event)
{
Log("Event Handler started.\n");
Log("Processing standard Event handling.\n");
if (Camera)
return Camera->OnEvent(Event);
return false;
}

bool CBeer::Run(void)
{
Log("Starting Game...\n");

Camera = Smgr->addCameraSceneNode();
Camera->setUpVector(vector3df(0,1,0));
Camera->setPosition(vector3df(0.0f, 0.0f, 0.0f));
Camera->setTarget(vector3df(0.0f, 0.0f, 1.0f));
Camera->setFOV(PI / 2);
Log("Camera initialized.\n");

Log("Starting Render Loop...\n");
while(Device->run() && Render());
Log("Render Loop left.\n");
Device->drop();
Log("Device dropped, shutting down.\n");
return true;
}


bool CBeer::Render(void)
{
Driver->beginScene(false, true, 0);
Log("Scene started.\n");
Smgr->drawAll();
GuiEnv->drawAll();
Log("Drawing complete.\n");
Driver->endScene();
Log("Scene complete.\n");
return true;
}

----------------------------------------------------------------------------------

// log.txt

Logfile created.
CBeer Instance created.
Initializing Event receiver...done
Creating Irrlicht Device...
Event Handler started.
Processing standard Event handling.
Event Handler started.
Processing standard Event handling.
...done
Driver, Scene Manager and GUI Environment initialized.
Starting Game...
Camera initialized.
Starting Render Loop...
Scene started.
Drawing complete.
Scene complete.



As you can see, there should be at least "Render Loop left." and "Device dropped, shutting down." after this, but it isnt. Please tell me it's just me being a n00b again :D
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

the reason is because you are creating Reciever inside of CBeer::Init()

this means it is being created locally within the function (on the 'stack' instead of the 'heap')

so, when the function goes away, all of its local variables including Reciever also go away, and the memory address of the reciever that you gave to createDevice() becomes invalid.

either create Reciver as a global as you did Beer, or give CBeer a Reciever pointer and set it to 'new Reciever()' then give that pointer to createDevice, and remember to 'delete' it when CBeer is destroyed.
a screen cap is worth 0x100000 DWORDS
Guest

Re: CTD during renderloop

Post by Guest »

TheGolem wrote:it's just me being a n00b again :D
its always the obvious one :roll:

thanks, that solved it. :)
Post Reply