簡體   English   中英

C ++我的程序因奇怪的錯誤而中斷

[英]c++ my program breaks with weird error

當我運行我的程序時,它可以正常編譯,但是當我運行時,我得到了一個停止我程序的框。 框顯示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;
}

任何想法? 更新這是類

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

您試圖訪問您無權訪問的內存。 最常見的原因是因為您使用的是尚未初始化的指針,並且該指針指向不在進程的內存空間中的地址。

在使用->運算符取消引用m_animController應確保已m_animController進行初始化(並指向有效對象)。

0xC0000005:訪問沖突讀取位置0xcdcdcdcd。

好吧,這很簡單:

  1. 訪問沖突是您嘗試訪問允許訪問的某些內存的錯誤。 例如,您可能正在嘗試使用空指針。
  2. 這里的0xcdcdcdcd部分是Visual Studio在刪除指針(或更確切地說,指針所指向的對象被銷毀並將指針設置為該值)或未初始化的>(編輯:我不確定,必須檢查VS文檔)。 只有在我的內存正確的情況下,才在“調試”模式下正確。

所以在這里,m_animController處於錯誤狀態(因為它似乎是崩潰的表達式的唯一指針)。

首先,我建議您確保之前未在此指針上調用過delete。 可能很明顯,也可能是您在對象析構函數中調用了delete,但沒有看到此對象已被銷毀或復制。 如果您的對象(CXFileEntity)一定不能被復制,請確保它不能復制(通過繼承boost :: noncopiable或將其復制構造函數和復制操作符私有化而不實現)。

接下來,如果您確實必須使用原始指針(而不是引用或智能指針),則最好在每個函數開始時使用斷言(在google和stackoverflow中查找斷言,對此有很多討論)。 更好的是:每次從函數獲取指針時都要檢查指針是否有效。 當一個指針處於您未曾預料到的狀態時,這將有助於您盡早跟蹤。

您的某個地方指針不好。 根據斷點所在的行,我猜測m_animController的值不正確。 檢查m_animController是否已初始化為默認值。 並確保您沒有在使用它之前刪除它。

注意:0xcdcdcdcd是一些調試器將使用其初始化無效指針的公共值 ,但不能保證。

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

以下是有關MSVS填充值的更多信息: 在Visual Studio C ++中,內存分配表示形式是什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM