c ++我的程序打破了奇怪的错误

时间:2010-11-10 00:42:42

标签: c++ winapi visual-c++

当我运行我的程序时它编译得很好,但是当它运行时,我得到这个盒子,它会停止我的程序。该框在Mon.exe中的0x0039e9a7处显示此未处理的异常:0xC0000005:访问冲突读取位置0xcdcdcdcd。我甚至都不知道这意味着什么。当它突破它时指向这个功能。

void CXFileEntity::SetAnimationSet(unsigned int index)
{
    if (index==m_currentAnimationSet)
        return;

    if (index>=m_numAnimationSets)
        index=0;

    // Remember current animation
    m_currentAnimationSet=index;

    // Get the animation set from the controller
    LPD3DXANIMATIONSET set;
    IT BREAKS HERE>>>>m_animController->GetAnimationSet(m_currentAnimationSet, &set );  

    // Note: for a smooth transition between animation sets we can use two tracks and assign the new set to the track
    // not currently playing then insert Keys into the KeyTrack to do the transition between the tracks
    // tracks can be mixed together so we can gradually change into the new animation

    // Alternate tracks
    DWORD newTrack = ( m_currentTrack == 0 ? 1 : 0 );

    // Assign to our track
    m_animController->SetTrackAnimationSet( newTrack, set );
    set->Release(); 

    // Clear any track events currently assigned to our two tracks
    m_animController->UnkeyAllTrackEvents( m_currentTrack );
    m_animController->UnkeyAllTrackEvents( newTrack );

    // Add an event key to disable the currently playing track kMoveTransitionTime seconds in the future
    m_animController->KeyTrackEnable( m_currentTrack, FALSE, m_currentTime + kMoveTransitionTime );
    // Add an event key to change the speed right away so the animation completes in kMoveTransitionTime seconds
    m_animController->KeyTrackSpeed( m_currentTrack, 0.0f, m_currentTime, kMoveTransitionTime, D3DXTRANSITION_LINEAR );
    // Add an event to change the weighting of the current track (the effect it has blended with the secon track)
    m_animController->KeyTrackWeight( m_currentTrack, 0.0f, m_currentTime, kMoveTransitionTime, D3DXTRANSITION_LINEAR );

    // Enable the new track
    m_animController->SetTrackEnable( newTrack, TRUE );
    // Add an event key to set the speed of the track
    m_animController->KeyTrackSpeed( newTrack, 1.0f, m_currentTime, kMoveTransitionTime, D3DXTRANSITION_LINEAR );
    // Add an event to change the weighting of the current track (the effect it has blended with the first track)
    // As you can see this will go from 0 effect to total effect(1.0f) in kMoveTransitionTime seconds and the first track goes from 
    // total to 0.0f in the same time.
    m_animController->KeyTrackWeight( newTrack, 1.0f, m_currentTime, kMoveTransitionTime, D3DXTRANSITION_LINEAR );

    // Remember current track
    m_currentTrack = newTrack;
}

任何想法? UPDATE 这是班级

class CXFileEntity
{
private:
    LPDIRECT3DDEVICE9 m_d3dDevice; // note: a pointer copy (not a good idea but for simplicities sake)

    // Direct3D objects required for animation
    LPD3DXFRAME                 m_frameRoot;
    LPD3DXANIMATIONCONTROLLER   m_animController;
    D3DXMESHCONTAINER_EXTENDED* m_firstMesh;

    // Bone data
    D3DXMATRIX *m_boneMatrices;
    UINT m_maxBones;

    // Animation variables
    unsigned int m_currentAnimationSet; 
    unsigned int m_numAnimationSets;
    unsigned int m_currentTrack;
    float m_currentTime;
    float m_speedAdjust;

    // Bounding sphere (for camera placement)
    D3DXVECTOR3 m_sphereCentre;
    float m_sphereRadius;

    std::string m_filename;

    void UpdateFrameMatrices(const D3DXFRAME *frameBase, const D3DXMATRIX *parentMatrix);
    void UpdateSkinnedMesh(const D3DXFRAME *frameBase);
    void DrawFrame(LPD3DXFRAME frame) const;
    void DrawMeshContainer(LPD3DXMESHCONTAINER meshContainerBase, LPD3DXFRAME frameBase) const;
    void SetupBoneMatrices(D3DXFRAME_EXTENDED *pFrame/*, LPD3DXMATRIX pParentMatrix*/); 
public:
    CXFileEntity(LPDIRECT3DDEVICE9 d3dDevice);
    ~CXFileEntity(void);

    bool Load(const std::string &filename);
    void FrameMove(float elapsedTime,const D3DXMATRIX *matWorld);

    void Render() const;
    void SetAnimationSet(unsigned int index);

    void NextAnimation();
    void AnimateFaster();
    void AnimateSlower();

    D3DXVECTOR3 GetInitialCameraPosition() const;
    unsigned int GetCurrentAnimationSet() const {return m_currentAnimationSet;}
    std::string GetAnimationSetName(unsigned int index);
    std::string GetFilename() const {return m_filename;}
};

我在析构函数中释放m_animController

4 个答案:

答案 0 :(得分:0)

您试图访问您无权访问的内存。最常见的是,这是因为您使用的指针尚未初始化,并且指向的地址不在进程的内存空间中。

在使用m_animController运算符取消引用之前,应确保->已初始化(并指向有效对象)。

答案 1 :(得分:0)

  

0xC0000005:访问冲突读数   位置0xcdcdcdcd。

嗯,这很简单:

  1. 访问冲突是一个错误,您尝试访问某些允许访问的内存。例如,您可能正在尝试使用空指针。
  2. 这里的0xcdcdcdcd部分是一个特殊的地址,当一个指针被删除时(或者更确切地说,指针所指向的对象被破坏,指针被设置为这个值)或者未被初始化的> (编辑:我不再确定,将不得不检查VS文档)。如果我的内存是正确的,那只在调试模式下才是真的。
  3. 所以在这里,m_animController处于错误状态(因为它似乎是崩溃时表达式的唯一指针)。

    首先,我建议你确保在此指针之前没有调用删除。可能很明显,或者可能是您在对象析构函数中调用delete,并且您没有看到此对象已被销毁或复制。如果不能复制您的对象(CXFileEntity),请确保它不能被复制(通过继承boost :: noncopiable或将其复制构造函数和复制操作符放在私有而不实现)。

    接下来,如果你真的必须使用原始指针(而不是引用或智能指针),你最好在每个函数的开头检查所有指针(和其他上下文值),使用断言(在google中查找断言)和stackoverflow,有很多讨论)。更好的是:每次从函数中获取指针时检查指针是否有效。这将有助于您在指针进入您没想到的状态时提前跟踪。

答案 2 :(得分:0)

你的某个地方有一个糟糕的指针。基于它破坏的行,我猜m_animController有一个坏的值。检查m_animController是否已初始化为默认值。并确保在使用之前不要将其删除。

注意:0xcdcdcdcd是a common value,有些调试器会初始化无效指针,但不能保证。

答案 3 :(得分:0)

0xCDCDCDCD是MSVS放入堆的未分配部分的值。

以下是有关MSVS填充值的更多信息: In Visual Studio C++, what are the memory allocation representations?