2017-12-08 48 views
-2

"例外がスローされずにstd :: mapでclear()メソッドを呼び出そうとしています:読み取りアクセス違反._Pノードが0xDDDDDDDDでした。 "。std :: map.clear()が読み取りアクセス違反をスローしています

//I have narrowed down the error to this group of code 
#include "stdafx.h" 
#include <map> 
#include <iostream> 

class Input 
{ 
    std::map<int, bool> pressedKeys; 
    std::map<int, bool> heldKeys; 
    std::map<int, bool> releasedKeys; 

public: 
    void Update() 
    { 
     heldKeys.clear(); 
     pressedKeys.clear(); 
     releasedKeys.clear(); 
    } 
}; 

class Window 
{ 
private: 
    Input * input; 
    void Update() 
    { 
     input->Update(); 
    } 
public: 
    Window() 
    { 
     input = &Input(); 

     while (true) 
     { 
      this->Update(); 
     } 
    } 
}; 

int main() 
{ 
    Window w = Window(); 
} 

例外は常に上起こる "heldKeys.clear();" Visual Studioのデバッガで「xtree」というページが表示されます。次のコードは、例外が発生した場所で周りのコードである「のXtree:」

void _Erase(_Nodeptr _Rootnode) 
    { // free entire subtree, recursively 
    for (_Nodeptr _Pnode = _Rootnode; !_Pnode->_Isnil; _Rootnode = _Pnode) //The error occurs here 
     { // free subtrees, then node 
     _Erase(_Pnode->_Right); 
     _Pnode = _Pnode->_Left; 
     _Alnode& _Al = this->_Getal(); 
     _Alnode_traits::destroy(_Al, _STD addressof(_Rootnode->_Myval)); 
     _Node::_Freenode0(_Al, _Rootnode); 
     } 
    } 

私は例外を期待していません。 が、私は例外を取得しています「スロー例外:アクセス違反を読ん _Pnodeが0xDDDDDDDDた。。」これ以上明確化をコメントしてください必要な場合 。

+1

[mcve]を投稿してください。 – PaulMcKenzie

+0

問題はあなたが示したものではありません。あなたが非常に不運でなければ、問題はマイクロソフトのコードではありません。 – chris

+0

また、このコードは実際のプログラムからコピーして貼り付けたのではなく、質問に入力しました。あなたのクラスでは 'heldkeys'が宣言されていますが、' update() '関数では' heldKeys'(資本** K **)を使用します。 – PaulMcKenzie

答えて

2

コンパイラで警告がすべて表示されていますか?かなり最近の高品質なC++コンパイラは、これをキャッチする必要があります。例えば。 http://rextester.com/l/cpp_online_compiler_clangに移動して、コードを入力し、とにかく必要とされていないstdafx.hをヘッダを除去し、あなたが表示されます:

source_file.cpp:31:17: error: taking the address of a temporary object of type 'Input' [-Waddress-of-temporary] 
     input = &Input(); 
       ^~~~~~~~ 

(コンパイラとして打ち鳴らす3.8を使用する。)

これが証明していますスタックオーバーフローと比較して大規模なクラスのプログラミングエラーを見つけるためのはるかに効率的な方法です。他の種類のエラーには、clangのアドレスサニタイザ、メモリサニタイザ、または未定義ビヘイビアサニタイザーなどの追加の解析パスまたはサニタイザを有効にすることが含まれます。

関連する問題