2016-05-03 29 views
1

我正在寫一個具有成員函數的類,它接收幾個輸入文件並生成存儲在類成員中的請求流。在(Vector?)析構函數中讀取訪問衝突

但是,一旦測試結束,當默認的析構函數執行時,會出現讀訪問衝突。我無法弄清楚這個原因。

下面是描述類的.hpp文件,有人能指出基礎知識中的任何缺陷嗎?

#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); 
}; 

當析構函數正在清除'reqStream'向量時,會發生該問題。 這是來自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/ 你必須包括CacheRegStream析構函數,釋放共享指針的向量的所有權,而不是試圖刪除向量。 (默認的析構函數試圖銷燬仍然由其他對象擁有的共享指針,實質上是試圖刪除它們的成員,這是超出範圍的。)

析構函數處理的一個特例是最後一個擁有對象負責銷燬共享指針向量。通常共享指針會默認處理這個,但由於它是一個向量,所以也會調用向量析構函數(它也銷燬所有向量元素),但沒有權限銷燬其他對象共同擁有的內容。

+0

謝謝,雖然這不是根本原因,但這是我修復的代碼中的潛在錯誤。 – akslah