Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC COM
Hello Everybody,
 
I have a situation where the Delete() statement in the following code segment causes an access violation:
 
try {
    MyOleDispatchDriver->InvalidFunction();
} catch (COleException* err) {
    TCHAR   szCause[255];
    err->GetErrorMessage(szCause, 255);
    LogString("Call failed because \"" + CString(szCause));
    err->Delete();
}
 
The MyOleDispatchDriver is an object of a class-wizard generated wrapper. The InvalidFunction is generated by this, but at the time the code is executed an older version of the automation server is used - which does not provide InvalidFunction. As a result, the exception handler is entered.
 
The err->Delete() statement does work - the rest of the err data seems valid. As for any CException-derived types I expected this to be the correct way of destructing the exception.
 
Has anybody run across a similar thing?
 
Many thanks!
Posted 18-Nov-12 21:20pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The LogString() call looks suspicious:
You are adding a CString object to a LPCTSTR. If LogString() expects a LPCTSTR, you are passing an invalid pointer: The address of the CString (by using the implicit LPCTSTR operator) is added to the address of your constant string.
  Permalink  
Comments
Doc Lobster at 19-Nov-12 6:21am
   
Hi, thanks for your answer. LogString takes const CString& as parameter and the string is concatenated properly. Anyway, I found the solution, I'm calling Delete twice ...
Jochen Arndt at 19-Nov-12 6:38am
   
It was a try and would explain the problem.
 
I had never such problems with COleExceptions but can't right now remember when one was thrown from code written by me (code is similar to yours).
 
Did you also catch COleDispatchExceptions? If not, you should do so. However, I don't think that this may help here.
 
You may also try catching all exceptions to see if this makes a difference. E.g.:
catch (COleDispatchException *de)
{
// show error message
de->Delete();
}
catch (CException *e)
{
// show error message
e->Delete();
}
Doc Lobster at 19-Nov-12 13:01pm
   
Yes, I'm aware of COleDispatchException*. It is derived from CException, so catching CException will do the job.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Sorry for asking the question, I'm calling err->Delete() twice (not visible in the question itself) . Understandably, I rather would like to draw back the question.
Anyway, when looking at the Delete() code:
 
void CException::Delete()
{
    // delete exception if it is auto-deleting
    if (m_bAutoDelete > 0)
    {
#ifdef _DEBUG
        m_bReadyForDelete = TRUE;
#endif
        delete this;
    }
}
 
m_bAutoDelete is just a random number after the first call to err->Delete(), so chances are 1:1 that the deletion is tried a second time if Delete() is to be called again.
 
On my development system, it was always a negative not causing the exception. On another system, m_bAutoDelete resolved to true and bang.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 392
1 OriginalGriff 370
2 CPallini 190
3 Abdul Samad KP 145
4 George Jonsson 119
0 OriginalGriff 6,329
1 Sergey Alexandrovich Kryukov 5,700
2 CPallini 4,940
3 George Jonsson 3,469
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100