Application disappearing instead of crashing

Hi,

I have the following lines of code:

if (!m_cmMyMap.Lookup(msgType, (CObject*&)pInfo))
{
pInfo->DoSomething
}

This is obviously a bug in that pInfo is always NULL if the condition is true. The "!" should not be there.  Anyway, when my app gets to pInfo->DoSomething, it just disappears instead of giving me an exception error.  If I step through the code, VS give me a access violation error. Anyone know why it would just disappear without throwing an application error message?
StanChartAsked:
Who is Participating?
 
DanRollinsCommented:
If pInfo is a random value, then
    pInfo->DoSomething()
is an address in memory taken from a randomly-generated vtable.  Executing code there would typically (eventually, more likley sooner rather than later) cause some sort of exception.  But imagine that it (by pure chance) pointed to the shut-down code of the running C++ module :-)  

The results of executing random code are, ....well, random (though usually fatal).  Thus the advice to initialize all pointers to NULL should be taken to heart.  That way, any use of it (inluding to execute it), will come up as a well-defined, easily fixed exception.

-- Dan
0
 
AlexFMCommented:
This just happens sometimes, so you are not alone. I don't think that Lookup method sets NULL to pInfo if search is unsuccessful. Is pInfo NULL before this line? Setting it to NULL can make program behavour more predictable.
0
 
StanChartAuthor Commented:
It is initialized to NULL before the call to Lookup().
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
AndyAinscowFreelance programmer / ConsultantCommented:
How about
pInfo = NULL;
m_cmMyMap.Lookup(msgType, (CObject*&)pInfo)
if(pInfo)
  pInfo->DoSomething();


ps.
I find the casting (CObject*&) rather unusual - are you actually saving the correct information into the map in the first place.
0
 
StanChartAuthor Commented:
Hi Andy,

The map was empty.  That was the problem.  My question was why would it disappear instead of throwing an application error.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Lookup return true if an entry is found so if the map is empty
if (!m_cmMyMap.Lookup(msgType, (CObject*&)pInfo))  
{
pInfo->DoSomething();  //This line will execute if no match found, pInfo is pointing to ???
}


Disappear/Error  -  well your app is attempting to run the DoSomething function at some indeterminate memory.  What happens may vary on what is at that memory location.
0
 
StanChartAuthor Commented:

pInfo->DoSomething();  //This line will execute if no match found, pInfo is pointing to ???

This was a bug which was fixed.  The ! was not supposed to be there.


Disappear/Error  -  well your app is attempting to run the DoSomething function at some indeterminate memory.  What happens may vary on what is at that memory location.

pInfo was initialized to NULL
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
So you had
(CObject*&)NULL->DoSomething()
being executed.
0
 
StanChartAuthor Commented:
yes, which i would assume would throw an exception
0
 
DanRollinsCommented:
Seeing so little of your code, we don't know if you have an outer try/catch exception handler somewhere.  There might be something that is "handling" the exception and the "smart thing" it does is shut down immediately.

Also, there is a setting under
   Project -> Settings -> C/C++ > C++ Language
that might be relevant.

Also, there might be an issue of how the OS is handling critical errors.  For instance, I think that on XP, it will do special stuff... such as ask if you want to email to Microsoft.  A similar possibility -- when the old DrWatson was active, *it* intercepted and handled crashes differently than when it was not running.  That indicates a possible machine-specific debugging-settings dependancy.  
To see if that is a possibility, you should make a minimal version of the program that crashes always when you click a certain button, then then run it on several different computers.

-- Dan
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
< yes, which i would assume would throw an exception>

Why should it throw an exception?
0
 
StanChartAuthor Commented:
Thanks a lot for that gents.  I'm splitting up the points because I've learned something from both of you.

Cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.