Page 1 of 1

Program crashes on getFileName()

Posted: Wed Jul 30, 2008 10:24 pm
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?

Posted: Thu Jul 31, 2008 7:54 am
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 ;)

Posted: Thu Jul 31, 2008 11:59 pm
by CameroKid
no good. It reads file as true and crashes anyway

Posted: Fri Aug 01, 2008 12:20 am
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.

Posted: Fri Aug 01, 2008 1:13 am
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.