Program crashes on getFileName()

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
CameroKid
Posts: 71
Joined: Tue Jul 11, 2006 8:54 pm

Program crashes on getFileName()

Post by CameroKid »

Here's a shortened version of my source:

Code: Select all

class MyEventReceiver : public IEventReceiver 
{
public:


   bool OnEvent(const SEvent& event)
   {

		if (event.EventType == irr::EET_GUI_EVENT)
		{
			s32 id = event.GUIEvent.Caller->getID();
			gui::IGUIEnvironment* env = device->getGUIEnvironment();
			switch(event.GUIEvent.EventType)
			{

			case gui::EGET_BUTTON_CLICKED:

				if (id == 3) // load game
				{
				
							gui::IGUIWindow* window = env->addWindow(
								core::rect<s32>(screen.Width/2 - 100, screen.Height - 150, 
								screen.Width/2 + 100 , screen.Height - 10 ),
								false, // modal?
								L"How many teams?");

							file = env->addFileOpenDialog(L"Please choose a file.");

				

							input = env->addEditBox(L"", core::rect<s32>(50, 60, 150, 80), true, window);

							env->addButton(core::rect<s32>(
								80, 
								90,
								120, 
								120 ),
								window, 
								30,
								L"Play", 
								L"");
						

					return true;
				}
				else if (id == 30) // load a board
				{

					core::stringw text = input->getText();

					ifstream book;


					core::stringc charFile = file->getFileName();


					book.open( core::stringc(file->getFileName()).c_str() );

more code here....

							return true;
						}


				break;
			default:
				break;
			}


		}


		return false;
   }

private:
	
	gui::IGUIEditBox* input;
	gui::IGUIEditBox* input2;

	
	gui::IGUIFileOpenDialog* file;

};
When I debug the program always crashes here:
core::stringc charFile = file->getFileName();
I imagine 'file' is getting corrupted, but I don't know how.

A side question: if I try to make 'file' a child of 'window' the gui freezes and I can't see the open file dialogue. What's up with that?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Before using a pointer you should always check it's valid;

core::stringc charFile;
if (file) charFile = file->getFileName();

can't believe that's the second time i've said that in about as many minutes! :lol:

Obviously that doesn't explain why file is apparently an invalid pointer, but it would stop your app crashing ;)
Image Image Image
CameroKid
Posts: 71
Joined: Tue Jul 11, 2006 8:54 pm

Post by CameroKid »

no good. It reads file as true and crashes anyway
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

Add a constructor which initializes file to 0.

Code: Select all

MyEventReceiver()
{
file=0;
}
My guess is that id 30 is happening before id 3, so your file pointer is just pointing to garbage.
CameroKid
Posts: 71
Joined: Tue Jul 11, 2006 8:54 pm

Post by CameroKid »

I fixed it. I figured out how Open Dialogue works. I needed to access the data on the EGET_FILE_SELECTED event handler rather than the Button clicked handler. Thanks for replying though.
Post Reply