Member function call causing unhandled exception

Started by
4 comments, last by AcidInjury 20 years ago
Alright, doing a map editor here using C++ and MFC. The following code is causing me a problem. BTW I am still pretty new to C++.

void CNZViewMapProperties::SetActiveMap(MAP* mapptr) 
{
	if(mapptr != NULL)
        pMap = mapptr;

	if(mapptr->GetMapName() == NULL)
		::MessageBox(NULL, "I get it", "Error", MB_OK);
	
	::MessageBox(NULL, mapptr->GetMapName(), "Map Name", MB_OK);

}
The above code doesnt do much. It prints out a stupid message if GetMapName is NULL otherwise prints out the map name. Its a test. What surprises me is that the following code doesn''t error out.

void CNZViewMapProperties::SetActiveMap(MAP* mapptr) 
{
	if(mapptr != NULL)
        pMap = mapptr;

	if(mapptr->GetMapName() == NULL)
		::MessageBox(NULL, "I get it", "Error", MB_OK);
/*	
	::MessageBox(NULL, mapptr->GetMapName(), "Map Name", MB_OK);
*/
}

And Finally, here is the runtime error I am getting.

Unhandled exception at 0x77d5d231 in NZEdit.exe: 0xC0000005: Access violation reading location 0xfdfdfd00.

Sounds like a memory access violation to me, but I cannot figgure out why.  The pointer that is passed to the function SetActiveMap is passed from and used in a CDocument class which manages the map.  The GetMapName() function also works as predicted in teh CDocument class.

Will Reynard 

Advertisement
Sound like a pointer was not assigned. If you are checking pointers for NULL make sure you actually assign it null at some point. Try stepping through with your debugger to see the exact line it crashes and make sure that the pointer is valid.

[edited by - acraig on March 30, 2004 1:13:25 PM]
This is pretty simple you need to add an else statement

The reason it is crashing is that the second messagebox gets called even if the pointer is NULL

One other thing is that you dont do anything if mapptr is NULL which would be bad too

void CNZViewMapProperties::SetActiveMap(MAP* mapptr) {	if(mapptr != NULL)           pMap = mapptr;        else return;  //dont do anything if the pointer is invalid	if(mapptr->GetMapName() == NULL)		::MessageBox(NULL, "I get it", "Error", MB_OK);	else //need this statement	   ::MessageBox(NULL, mapptr->GetMapName(), "Map Name", MB_OK);}


"Give a man a fish and he will eat for a day, drown a man in the water and the fish will eat for a week!

[edited by - cmptrgear on March 30, 2004 1:26:44 PM]
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson
Or even it like this

void CNZViewMapProperties::SetActiveMap(MAP* mapptr) {  if (mapptr == NULL)    return;  pMap = mapptr;  if(mapptr->GetMapName() == NULL)    ::MessageBox(NULL, "I get it", "Error", MB_OK);  else //need this statement    ::MessageBox(NULL, mapptr->GetMapName(), "Map Name", MB_OK);}
EDIT: Well ok, I did another test and realized that the problem must be located somewhere else. I need to dig into the code some more to find the problem. Thanks for your suggestions guys.

Well I agree that that else should have been there, i've been changing a lot of things trying to figgure out the problem. It was there at one point.

Regardless, with the else added I still have the same problem. VS says that the problem occurs when I call.

MapPropDlg.DoModal();

This makes the problem make even less sense. I will try to explain.

Here is the function that calls the dialog box. It occurs when the user selects View Map Properties from the toolbar.

void CNZEditDoc::OnViewMapProps(){	CNZViewMapProperties MapPropDlg;	MapPropDlg.SetActiveMap(pMap);	MapPropDlg.DoModal();}And here is the updates version of SetActiveMap   void CNZViewMapProperties::SetActiveMap(MAP* mapptr) {	if(mapptr != NULL)        pMap = mapptr;	if(pMap->GetMapName() == NULL)		::MessageBox(NULL, "I get it", "Error", MB_OK);	else		::MessageBox(NULL, pMap->GetMapName(), "Map Name", MB_OK);}


As you can see from the first method, the SetActiveMap method is called before DoModal(). Yet VS tells me the problem occurs on the call to DoModal, which should mean SetActiveMap completed successfully. When I run the program however, no message box appears telling me anything. The SetActiveMap method has to generate at least one MessageBox yet I get nothing.

Now I am thourally confused.

Will Reynard

[edited by - AcidInjury on March 30, 2004 1:45:05 PM]
Even though the debugger says the error occured on some line it usually happened at a point before that. Since no messageboxes are being created it supports this thought. Now what is causing the problem is anyones guess.

"Give a man a fish and he will eat for a day, drown a man in the water and the fish will eat for a week!
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson

This topic is closed to new replies.

Advertisement