Click here to Skip to main content
12,815,616 members (42,509 online)
Rate this:
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 {
} catch (COleException* err) {
    TCHAR   szCause[255];
    err->GetErrorMessage(szCause, 255);
    LogString("Call failed because \"" + CString(szCause));

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 22:20pm
Rate this: bad
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.
Doc Lobster 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 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
catch (CException *e)
// show error message
Doc Lobster 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
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;
        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.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2017
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