2016-05-03 53 views
1

私は、少数の入力ファイルを取り、クラスメンバに格納されている要求のストリームを生成するメンバ関数を持つクラスを記述しています。(Vector?)デストラクタでの読み取りアクセス違反

ただし、テストが終了すると、デフォルトのデストラクタが実行されると読み取りアクセス違反が発生します。私はこれの理由を理解することはできません。

ここにクラスを説明する.hppファイルがあります。誰かがここで基本的な欠陥を指摘できますか?デストラクタは「reqStream」ベクトルをクリアしている間

#include <iostream> 
#include <fstream> 
#include <vector> 
#include <memory> 
#include <string> 
#include <sstream> 
#include <map> 
#include "SimDataTypes.h" 

using namespace std; 

#define dataSize 64 
#define SEARCH_RANGE 40 
#define DEBUG_STREAM 1 
#define BLOCKSIZE 256 //256 Byte alignment 



class CacheReqStream 
{ 
    struct CacheReq 
    { 
     CC_UINT8 data[256]; //at most 256 bytes 
     vector<CC_UINT32> size; 
     vector<CC_UINT64> addr; 
     vector<CC_UINT64> timeStamp; 
     CC_UINT64 baseAddr; 
     string surf; 
     string tile; 
     access_type access; 
     vector<string> client; 
     CC_UINT32 dataMask; //Which addr to write the 32 bytes to. (Alahiry Revise description) 
     CC_UINT32 addrMask; //Which 64 Byte chunk of the 256 Byte aligned Addr (4 bit mask that says which addresses are valid) 
    }; 

struct AddrList 
{ 
    vector<string> data; 
    vector<CC_UINT64> addr; 
    vector<CC_UINT64> timeStamp; 
    vector<access_type> access; 
    vector<CC_UINT32> size ; 
    vector<CC_UINT64> Index; //Index in the file for that addr. 
    vector<string> surf; 
    vector<string> tile; 
    vector<string> client; 
    bool initialized; 
    bool anyWrites; 
}; 

map<CC_UINT64, AddrList> AddrMap; //store base address and list of all accesses in that range 
vector< shared_ptr<CacheReq> > reqStream; //store processed addresses 


//Check if the address for that timeStamp and Client is already processed for the same access type. 
bool isProcessed(CC_UINT64 address, string Client , access_type access, CC_UINT64 timeStamp); 

//void CheckNeighbors(CC_UINT64 addrIn[], vector< shared_ptr<CC_UINT8> > dataIn, access_type accessIn[], int start, access_type type, CC_UINT64 startAddr, CacheReq* req); 
void CheckNeighbors(string dataIn, int start, access_type type, CC_UINT64 startAddr, shared_ptr<CacheReq> req); 

//void findAndStore(CC_UINT64 addrIn[], vector< shared_ptr<CC_UINT8> > dataIn, access_type accessIn[], int start, access_type type); 
void findAndStore(string dataIn, int start, CC_UINT64 addr, access_type type,string surf, string tile, string client, CC_UINT64 time); 

//total data requested must equal the total data in the created stream 
void verifyStream(void); 

//Create an access from the addresses stored in the AddrMap at this point in time. 
void createReadAccess(string dataIn, int start, CC_UINT64 addr, string surf, string tile, string client, CC_UINT64 time); 

//Create an access from the addresses stored in the AddrMap at this point in time. 
void createWriteAccess(string dataIn, int start, CC_UINT64 addr, string surf, string tile, string client, CC_UINT64 time); 

//Get the index to store the addr based on the bottom two bits(0x00 = index 0 ; 0x40 == index 1 0x80 == index 2 0xC0 == index 3) 
//This assumes 64 byte stream with 256 byte access stream 
CC_UINT32 getAddrIndex(CC_UINT64 addr); 

//Get the index to the 32 byte chunk in the data segment 
CC_UINT32 getDataSegIndex(CC_UINT64 addr); 

//Get Index of access currently requested 
CC_UINT32 getAccessIndex(CC_UINT64 addr, CC_UINT64 time, string client); 
//Align addresses to the cache block size 
CC_UINT64 alignDown(CC_UINT64 address); 

tiling_formats convertTile(string tile); 
surface_formats convertFormat(string format); 

//CC_UINT64 partialMasks; 


public: 

    CC_UINT64 getAddr(int index); 
    CC_UINT32 getSize(int index); 
    tiling_formats getTiling(int index); 
    surface_formats getSurfFormat(int index); 
    access_type getAccess(int index); 

//Create a stream of accesses clubbed together. 
void ProcessData(void); 
//accumulate data structure per aligned addr. 
void accumulateAddr(void); 
}; 

問題が発生します。 ここにVisual Studioのコールスタックがあります。

enter image description here

私はここにデストラクタを記述する必要がありますまたはデフォルトが正しく、ベクターおよびマップメンバーを処理する場合、私は疑問に思って?

+0

大変申し訳ありませんが、言葉の悪い選択。私はreqStreamを削除していません。デフォルトのデストラクタはshared_ptrsをクリアしようとしていますか?読み取りアクセス違反は、デフォルトのデストラクタで発生します。 – akslah

+1

あなたは[mcve](http://stackoverflow.com/help/mcve)を使いこなそうとしましたか? – BeyelerStudios

+0

まだありません。今晩後半に作成しようとします。クラスに記述されている特定のコンストラクタ/デストラクタの不足のために問題が明らかになったのかどうか疑問に思っていました。 – akslah

答えて

1

ここに良い参考文献があります:http://www.cplusplus.com/reference/memory/shared_ptr/ ベクトルを削除しようとするのではなく、共有ポインタのベクトルの所有権を解放するDestructorをCacheRegStreamに含める必要があります。 (デフォルトのデストラクタは他のオブジェクトが所有している共有ポインタを破壊しようとしています。境界を越えてメンバを削除しようとしています)。

扱うデストラクターの特別なケースは、それが破壊されたとき共有ポインタベクトルを破壊する原因となる。通常は共有ポインタはこれをデフォルトで扱いますが、それはベクトルなので、ベクトルデストラクタも呼び出されています(ベクトル要素もすべて破棄されます)が、他のオブジェクトが共有しているものを破壊する権限はありません。

+0

ありがとう、これは根本的な原因ではありませんが、私が修正したコードの潜在的なバグでした。 – akslah

関連する問題