调用实例方法时的C ++访问冲突

时间:2012-04-08 21:47:11

标签: c++ function methods directx access-violation

我正在创建一个看起来像这样的DirectX 11帮助程序类:

#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.
}

此类初始化如下:dxClass = new DXClass(); Initialize()函数在创建dxClass的类的另一个方法中调用。

运行应用程序时,我在setupDisplay()函数的开头出现访问冲突。但是,如果我将setupDisplay()中的代码放入Initialize()中,删除对setupDisplay()的调用,则不会发生访问冲突。此外,如果我从setupDisplay()删除代码以使其为空函数,然后在Initialize()中调用它,则不会发生访问冲突。

似乎没有指针为NULL,如果如上所述更改了应用程序,应用程序将启动。但是,另一方面,应用程序在退出时会收到另一个访问冲突。调试器指向Release()上的ID3D11ShaderResourceView*来电,我已在我的代码段中指出了这一点。该指针似乎也有效。

我也检查了类似的问题,但是类的this指针看起来是有效的,而且我没有创建任何可能溢出的缓冲区。也没有任何东西可以提前删除/释放对象。

我不知道可能导致错误的原因。 :/

谢谢:D

编辑: 这是一个孤立的测试,具有相同的错误: 我的主要功能是:

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

在我的App课程中,我删除了所有窗口功能和任何其他变量,因此它看起来像这样:

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

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

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

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

访问冲突仍然发生在同一个地方。此外,如果我用:

替换工厂实例变量,结果相同
IDXGIFactory *f;
HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void **)&f);

在其他应用程序中,这对我有用。

1 个答案:

答案 0 :(得分:1)

调用Release()时的访问冲突通常意味着该对象已经从其他地方收到了它的最终Release()(并且它已经破坏了自己)。将指针传递到DXClass

时,一种可能的解决方案是AddRef()
相关问题