user974967
user974967

Reputation: 2946

CXX0030: Error: expression cannot be evaluated

The short question:

Why am I see this in the Locals window for my IWICImagingFactory object while in my destructor?

The long explanation:

I am creating a IWICImagingFactory object in my CreateDeviceIndependentResources() function:

if (SUCCEEDED(hr))
{
    hr = CoCreateInstance(
        CLSID_WICImagingFactory,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&mpWICFactory)
        );
}

I have checked hr after this and it is always S_OK.

Then in my CreateDeviceResources() function I pass a pointer to the IWICImagingFactory object in a call to LoadBitmapFromFile():

    if(SUCCEEDED(hr))
    {
        hr = LoadBitmapFromFile(
            mpRenderTarget,
            mpWICFactory,
            L".\\background.png",
            0,
            0,
            &mpBackgroundBitmap);
    }

The function LoadBitmapFromFile is exactly as it appears in the MSDN sample. You can see most of the code here: http://msdn.microsoft.com/en-us/library/dd756686%28v=VS.85%29.aspx

The return code from LoadBitmapFromFile() is S_OK. I now have a ID2D1Bitmap object which works fine.

I don't use the WIC factory for anything else. The problem occurs in my destructor when I try to SafeRelease() the WIC factory:

SafeRelease(&mpWICFactory);

SafeRelease() is defined as:

template<class Interface>
inline void SafeRelease(Interface** ppInterfaceToRelease)
{
if(*ppInterfaceToRelease != NULL)
{
    (*ppInterfaceToRelease)->Release();
    (*ppInterfaceToRelease) = NULL;
}

}

While in the destructor, if I expand the mpWICFactory object in the Locals window, I see the "CXX0030: Error: expression cannot be evaluated" errors. The screenshot below shows the Locals window right before calling SafeRelease() on the WICFactory object.

http://img21.imageshack.us/img21/9820/localsy.jpg

I then get an error: Unhandled exception at 0x00d22395 in Program.exe: 0xC0000005: Access violation reading location 0x6df128f0.

What is the cause of this problem?

EDIT: Here is a full simple program demonstrating the problem:

Test.h

#ifndef TEST_H
#define TEST_H

#include <Windows.h>
#include <wincodec.h>
#include <d2d1.h>

class Test
{
public:

IWICImagingFactory *mpWICFactory;

Test();
~Test();

HRESULT Init();
};

template<class Interface>
inline void SafeRelease(Interface** ppInterfaceToRelease)
{
if(*ppInterfaceToRelease != NULL)
{
    (*ppInterfaceToRelease)->Release();
    (*ppInterfaceToRelease) = NULL;
}

}

#endif

Test.cpp

#include "Test.h"

Test::Test() : mpWICFactory(NULL)
{
}

Test::~Test()
{
SafeRelease(&mpWICFactory);
}

HRESULT Test::Init()
{
   HRESULT hr = CoCreateInstance(
        CLSID_WICImagingFactory,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&mpWICFactory)
        );

   return hr;
}

Main.cpp

#include <Windows.h>
#include "Test.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
               PSTR cmdLine, int showCmd)
{
if(SUCCEEDED(CoInitialize(NULL)))
{
    Test app;

    app.Init();

    CoUninitialize();
}

return 0;
}

Upvotes: 1

Views: 17555

Answers (2)

Ateş G&#246;ral
Ateş G&#246;ral

Reputation: 140032

Try calling SafeRelease() right after initialization and see if it works.

Also, try invoking the clean up explicitly (with some sort of uninitialize function) instead of doing this at the destructor. When you leave clean up to destructors, sometimes you don't have full control over exactly what is being cleaned up when.

It could also be that you're destroying some dependent objects in the wrong order.

Upvotes: 0

Bo Persson
Bo Persson

Reputation: 92211

The short answer is that the virtual function pointer 0x6df128e8 is invalid, so the debugger cannot dereference it. That's what it means by "expression cannot be evaluated".

So when the program slightly later tries to call one of the virtual functions, the pointer still doesn't work and you get an access violation.

The long and hard part if finding out exactly where your object is overwritten with an invalid value. Unfortunately it can be pretty much anywhere else in the program...

Upvotes: 3

Related Questions