1

I'm creating a DirectX 11 helper class that looks kind of like this:

#import "DXClass.h" // I have declared the constructor and the other methods here
// All of the DirectX libraries are imported in the header as well 

DXClass::DXClass()
{

    // Pointers created, etc.
}

DXClass:~DXClass()
{
    // Other DirectX objects released   

    // With an if (bbSRView) {}, the exception still occurs, so bbSRView is not NULL
    // bbSRView is a ID3D11ShaderResourceView*
    // When the other violation does not occur, one does here:
    bbSRView->Release();
    bbSRView = NULL;

    // More releases

void DXClass::Initialize()
{
    SetupDisplay();

    // Other initialization that works fine
}

void DXClass::SetupDisplay()
{
    // This is where the debugger shows the access violation.
    // factory is declared as DXGIFactory*
    HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void **)&factory);

    // Loop through adapters and outputs, etc.
}

This class is initialized like this: dxClass = new DXClass(); The Initialize() function is called in another method of the class that created dxClass.

When the application is run, I get an access violation at the beginning of the setupDisplay() function. However, if I take the code in setupDisplay() and put it in Initialize(), removing the call to setupDisplay(), no access violation occurs. Also, if I remove the code from setupDisplay() so that it is an empty function, and then call it in Initialize(), no access violation occurs.

It appears that no pointers are NULL, and the application will start fine if it is changed as described above. However, on another note, the application receives another access violation when quitting. The debugger points to a Release() call on an ID3D11ShaderResourceView*, which I have pointed out in my code snippet. This pointer also appears to be valid.

I have also checked the similar questions, but the this pointer of the class appears to be valid, and I am not creating any buffers that could be overflowing. There also isn't anything that could be deleting/freeing the object early.

I have no idea what could be causing the errors. :/

Thanks :D

EDIT: Here's an isolated test, with the same errors: I have in my main function:

INT APIENTRY wWinMain(HINSTANCE, HINSTANCE, LPWSTR, INT)
{
    App *app = new App();
    app->Run();
    app->Release();
 }

In my App class, I have removed all window functionality and any other variables so that it looks like this:

App::App()
{
    dxClass = new DXClass();
}

App::~App()
{
    delete dxClass;
}

void App::Run()
{
    dxClass->Initialize();

    while (true) {} // Never reaches here
}

The access violation still occurs at the same place. Also, same results if I replace the factory instance variable with:

IDXGIFactory *f;
HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void **)&f);

Which has worked for me in other applications.

4

1 に答える 1

1

Release() を呼び出すときのアクセス違反は、通常、オブジェクトが別の場所から最終的な Release() を既に受け取っている (そしてそれ自体が破棄されている) ことを意味します。考えられる解決策の 1 つは、ポインターを DXClass に渡すときに AddRef() を使用することです。

于 2012-04-08T22:37:46.250 に答える