[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

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?
0
StanChart
Asked:
StanChart
  • 5
  • 4
  • 2
  • +1
2 Solutions
 
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
 
AndyAinscowCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
< yes, which i would assume would throw an exception>

Why should it throw an exception?
0
 
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
 
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now