C++初心者です。
ヒープ破壊のエラーに、かれこれ2週間ほど苦しめられております。
何かわかる方いましたら回答をお願いします...助けてください...(>_<)汗
【発生手順】
「CreateBuffer」というFunctionで、pA1~pA16のバッファを作成します。
そして、pA1~pA16が作られた状態で「DeleteBuffer」が呼ばれるとpA1~pA16をdeleteしていきます。
pA15~pA16辺りをdeleteするときに以下のエラーが発生します。(たいていはpA16でエラーが発生する)
Windows によって HogeHoge.exe でブレークポイントが発生しました。
ヒープが壊れていることが原因として考えられます。HogeHoge.exe または読み込まれた DLL にバグがあります。
あるいは、HogeHoge.exe がフォーカスを持っているときに、ユーザーが F12 キーを押したことが原因として考えられます。
可能であれば、出力ウィンドウに詳細な診断情報が表示されます。
「CreateBuffer」でpA1~pA16をnewした直後に「DeleteBuffer」を呼んでみても同様のエラーが発生するので、構造体の作り方が悪かったりするのかなと考えているところです。
説明が不足している点は聞いていただければと思います。
皆さんのお知恵をお貸しください。よろしくお願いします。
また、ソースですがお見せすることができない場所もありますので、これがソースのすべてではありません。限られた情報しか提示できませんがご協力お願いします(ToT)
以下、問題のソースです。
typedef struct AData{ BYTE *pA1; //A1ポインタ BYTE *pA2; //A2ポインタ BYTE *pA3; //A3ポインタ BYTE *pA4; //A4ポインタ BYTE *pA5; //A5ポインタ BYTE *pA6; //A6ポインタ BYTE *pA7; //A7ポインタ BYTE *pA8; //A8ポインタ BYTE *pA9; //A9ポインタ BYTE *pA10; //A10ポインタ BYTE *pA11; //A11ポインタ BYTE *pA12; //A12ポインタ BYTE *pA13; //A13ポインタ BYTE *pA14; //A14ポインタ BYTE *pA15; //A15ポインタ BYTE *pA16; //A16ポインタ }
void HogeHoge::CreateBuffer( Int32 bufferSize ) { this->DeleteBuffer(); this->data->pA1 = new BYTE[ bufferSize ]; this->data->pA2 = new BYTE[ bufferSize ]; this->data->pA3 = new BYTE[ bufferSize ]; this->data->pA4 = new BYTE[ bufferSize ]; this->data->pA5 = new BYTE[ bufferSize ]; this->data->pA6 = new BYTE[ bufferSize ]; this->data->pA7 = new BYTE[ bufferSize ]; this->data->pA8 = new BYTE[ bufferSize ]; this->data->pA9 = new BYTE[ bufferSize ]; this->data->pA10 = new BYTE[ bufferSize ]; this->data->pA11 = new BYTE[ bufferSize ]; this->data->pA12 = new BYTE[ bufferSize ]; this->data->pA13 = new BYTE[ bufferSize ]; this->data->pA14 = new BYTE[ bufferSize ]; this->data->pA15 = new BYTE[ bufferSize ]; this->data->pA16 = new BYTE[ bufferSize ]; this->DeleteBuffer(); //テスト用に追加 }
void HogeHoge::DeleteBuffer() { if( this->data->pA1 != NULL ) { delete[] this->data->pA1; this->data->pA1 = NULL; } if( this->data->pA2 != NULL ) { delete[] this->data->pA2; this->data->pA2 = NULL; } if( this->data->pA3 != NULL ) { delete[] this->data->pA3; this->data->pA3 = NULL; } if( this->data->pA4 != NULL ) { delete[] this->data->pA4; this->data->pA4 = NULL; } if( this->data->pA5 != NULL ) { delete[] this->data->pA5; this->data->pA5 = NULL; } if( this->data->pA6 != NULL ) { delete[] this->data->pA6; this->data->pA6 = NULL; } if( this->data->pA7 != NULL ) { delete[] this->data->pA7; this->data->pA7 = NULL; } if( this->data->pA8 != NULL ) { delete[] this->data->pA8; this->data->pA8 = NULL; } if( this->data->pA9 != NULL ) { delete[] this->data->pA9; this->data->pA9 = NULL; } if( this->data->pA10 != NULL ) { delete[] this->data->pA10; this->data->pA10 = NULL; } if( this->data->pA11 != NULL ) { delete[] this->data->pA11; this->data->pA11 = NULL; } if( this->data->pA12 != NULL ) { delete [] this->data->pA12; this->data->pA12 = NULL; } if( this->data->pA13 != NULL ) { delete [] this->data->pA13; this->data->pA13 = NULL; } if( this->data->pA14 != NULL ) { delete [] this->data->pA14; this->data->pA14 = NULL; } if( this->data->pA15 != NULL ) { delete [] this->data->pA15; this->data->pA15 = NULL; } if( this->data->pA16 != NULL ) { delete [] this->data->pA16; this->data->pA16 = NULL; } }
↓以下追加のソースです。
HogeHoge.h
public ref class HogeHoge // データ取得バッファ(アンマネージ)のラッパ { private: FUGA_AREA* data; // データ取得バッファ public: int BufferSize; // バッファのサイズ HogeHoge(void); // コンストラクタ ~HogeHoge(void); // デストラクタ !HogeHoge(void); // ファイナライザ FUGA_AREA* GetReference(); // データ取得バッファのポインタを取得する void CreateBuffer( Int32 bufferSize ); // バッファを作成 void DeleteBuffer(); // バッファを削除 public: /// /// プロパティ /// property String^ IdName // ID名(S1~S100,R1~R100,M1~M100)(Read Only) { String^ get() { std::string s( this->data->aIdName ); return Utility::ToUnicode( s ); } }; property UInt32 AMaxSize // データ最大数(AデータのMAX値)(Read Only) { UInt32 get(){ return this->data->nMax; } }; property UInt32 Count // データ個数(property) { UInt32 get(){ return this->data->nCnt; } void set( UInt32 value ){ this->data->nCnt = value; } }; property Int32 CountReference // データ個数のポインタ(Read Only) { Int32 get(){ return (int)( &(this->data->nCnt) ); } }; property UInt32 StartPosition // データ開始位置(Read Only) { UInt32 get(){ return this->data->nStp; } }; property UInt32 EndPosition // データ終了位置(Read Only) { UInt32 get(){ return this->data->nEnp; } }; property String^ DataType // データ型(Read Only) { String^ get() { std::string s( this->data->aDataType ); return Utility::ToUnicode( s ); } }; property Int32 DataLength // データ長(Read Only) { Int32 get(){ return this->data->nDataLength; } }; property Int32 Status // 状態(Read Only) { Int32 get(){ return this->data->nStatus; } }; property Int32 A1Reference //A1データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA1[0] ) ); } }; property Int32 A2Reference //A2データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA2[0] ) ); } }; property Int32 A3Reference //A3データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA3[0] ) ); } }; property Int32 A4Reference //A4データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA4[0] ) ); } }; property Int32 A5Reference //A5データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA5[0] ) ); } }; property Int32 A6Reference //A6データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA6[0] ) ); } }; property Int32 A7Reference //A7データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA7[0] ) ); } }; property Int32 A8Reference //A8データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA8[0] ) ); } }; property Int32 A9Reference //A9データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA9[0] ) ); } }; property Int32 A10Reference //A10データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA10[0] ) ); } }; property Int32 A11Reference //A11データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA11[0] ) ); } }; property Int32 A12Reference //A12データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA12[0] ) ); } }; property Int32 A13Reference //A13データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA13[0] ) ); } }; property Int32 A14Reference //A14データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA14[0] ) ); } }; property Int32 A15Reference //A15データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA15[0] ) ); } }; property Int32 A16Reference //A16データのポインタ(Read Only) { Int32 get() { return (int)( &( this->data->pA16[0] ) ); } }; }
HogeHoge.cpp
/// /// 機能 : コンストラクタ /// HogeHoge::HogeHoge(void) { this->data = new FUGA_AREA; memset( this->data, 0, sizeof( FUGA_AREA ) ); this->BufferSize = 0; strcpy_s( this->data->aIdName, sizeof(this->data->aIdName), "R1" ); } /// /// 機能 : デストラクタ /// HogeHoge::~HogeHoge(void) { this->!HogeHoge(); } /// /// 機能 : ファイナライザ /// HogeHoge::!HogeHoge() { this->DeleteBuffer(); if( this->data != NULL ) { delete this->data; this->data = NULL; } }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/16 12:01
2016/04/16 13:31