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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
StanChartAuthor Commented:
It is initialized to NULL before the call to Lookup().
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.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

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.
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.
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
AndyAinscowFreelance programmer / ConsultantCommented:
So you had
(CObject*&)NULL->DoSomething()
being executed.
StanChartAuthor Commented:
yes, which i would assume would throw an exception
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
AndyAinscowFreelance programmer / ConsultantCommented:
< yes, which i would assume would throw an exception>

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
StanChartAuthor Commented:
Thanks a lot for that gents.  I'm splitting up the points because I've learned something from both of you.

Cheers
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.