IXAudio2 - 0xC0000005:访问冲突写入位置 0x00000000

Posted

技术标签:

【中文标题】IXAudio2 - 0xC0000005:访问冲突写入位置 0x00000000【英文标题】:IXAudio2 - 0xC0000005: Access violation writing location 0x00000000 【发布时间】:2014-01-11 21:07:52 【问题描述】:

我的 DirectX XAudio2 程序有一个奇怪的问题。一切都应该正常工作,但我得到 0xC0000005:访问冲突错误:

if(FAILED(hr = xAudio2Engine->CreateSourceVoice(&pAudioTrack->mAudiosource, lWaveFileFormat)))

这里是 XAudio2Manager.cpp:

#include "XAudio2Manager.h"
#include <exception>

class XAudio2InitException: public std::exception

    virtual const char* what() const throw()
    
        return "XAudio2 Initialiation failed";
    
 XAudio2ex;

template <class T> void SafeRelease(T **ppT)

    if(*ppT)
    
        (*ppT)->Release();
        *ppT = NULL;
    


cAudioTrack::cAudioTrack()

    mIsPlaying = false;


//cAudioTrack::~cAudioTrack();
//

//

void cAudioTrack::Play()

    if(!mIsPlaying)
    
        mAudioSource->Start();
        mIsPlaying = true;
    


void cAudioTrack::Pause()

    if(mIsPlaying)
    
        mAudioSource->Stop();
        mIsPlaying = false;
    


void cAudioTrack::Reset()

    mAudioSource->FlushSourceBuffers();
    mAudioSource->SubmitSourceBuffer(&buffer);
    mIsPlaying = false;


cXAudio2Manager::cXAudio2Manager()

    xAudio2Engine = NULL;
    masterVoice = NULL;

    HRESULT hr = S_OK;

    // Create audio engine
    if(SUCCEEDED(hr))
    
        hr = XAudio2Create(&xAudio2Engine);
    

    // Create mastering voice
    if(SUCCEEDED(hr))
    
        hr = xAudio2Engine->CreateMasteringVoice(&masterVoice,
            XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE,
            0,0, NULL);
    

    // If failed, throw exception
    if(!SUCCEEDED(hr))
    
        throw XAudio2ex;
    


bool cXAudio2Manager::LoadPCMFile(LPWSTR filename, cAudioTrack *pAudioTrack, bool pLooping)

    HRESULT hr = S_OK;

    // Read wave file
    CWaveFile lWaveFile;

    if(FAILED(hr = lWaveFile.Open(const_cast<LPWSTR>(filename), nullptr, WAVEFILE_READ)))
    
        return false;
    

    // Get file format
    WAVEFORMATEX* lWaveFileFormat = lWaveFile.GetFormat();

    // Calculate bytes and samples 
    DWORD lWaveFileSize = lWaveFile.GetSize();

    // Read sample data from wave file in to memory
    BYTE* lWaveData = new BYTE[lWaveFileSize];
    if(FAILED(hr = lWaveFile.Read(lWaveData, lWaveFileSize, &lWaveFileSize)))
    
        if (lWaveData)
            delete[] lWaveData;
        return false;
      

    if(FAILED(hr = xAudio2Engine->CreateSourceVoice(&pAudioTrack->mAudioSource, lWaveFileFormat)))
    
        if(lWaveData)
            delete [] lWaveData;
        return false;
    

    XAUDIO2_BUFFER buffer = 0;
    pAudioTrack->buffer = buffer;
    pAudioTrack->buffer.pAudioData = lWaveData;
    pAudioTrack->buffer.Flags = XAUDIO2_END_OF_STREAM;
    pAudioTrack->buffer.AudioBytes = lWaveFileSize;
    if(pLooping)
    
        pAudioTrack->buffer.LoopCount = XAUDIO2_LOOP_INFINITE;
    

    if(FAILED(hr = pAudioTrack->mAudioSource->SubmitSourceBuffer(&pAudioTrack->buffer)))
    
        pAudioTrack->mAudioSource->DestroyVoice();
        if(lWaveData)
            delete [] lWaveData;
        return false;
    

    return true;

Game.cpp中调用Wave文件加载函数:

AudMa->LoadPCMFile(L"running.wav", mpCharacter->runningSound, true); 

SDKwavefile 正在使用中。

请帮忙!

【问题讨论】:

【参考方案1】:

这闻起来非常像空指针。有问题的行是:

xAudio2Engine->CreateSourceVoice(&pAudioTrack->mAudioSource, lWaveFileFormat)

因此,为 null 的明显可能性是 xAudio2EnginepAudioTrack。添加一些诊断以检查是否为空。如果你发现 null 然后深入挖掘以了解它为什么会这样。

【讨论】:

谢谢,看来 pAudioTrack 是 NULL。现在它像梦一样运作。

以上是关于IXAudio2 - 0xC0000005:访问冲突写入位置 0x00000000的主要内容,如果未能解决你的问题,请参考以下文章

C++:0xC0000005:访问冲突写入位置0x00000000

处有未经处理的异常:0xC0000005:写入位置0x00EFA000时发生访问冲突.

没有躲过的坑--0xC0000005: 读取位置 xxx时发生访问冲突

引发0xC0000005内存违例几种可能原因分析

无法在 C# 中捕获 0xC0000005 异常

0xC0000005: 写入位置 0x0000000002450040 时发生访问冲突