在(Vector?)析构函数中读取访问冲突

时间:2016-05-03 14:08:38

标签: c++ visual-studio

我正在编写一个具有成员函数的类,这些函数接受一些输入文件并生成存储在类成员中的请求流。

但是一旦测试结束,我会在默认析构函数执行时获得读取访问冲突。我无法弄清楚原因。

这是描述该类的.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

我想知道我是否需要在这里描述析构函数,或者默认情况下是否应该正确处理向量和映射成员?

1 个答案:

答案 0 :(得分:1)

这里有很好的参考:http://www.cplusplus.com/reference/memory/shared_ptr/ 你必须在CacheRegStream中包含一个析构函数,它释放共享指针向量的所有权,而不是试图删除向量。 (默认的析构函数试图破坏仍然由其他对象拥有的共享指针,实质上是试图删除它们的成员,这是超出范围的。)

析构函数要处理的一个特例是最后一个拥有对象负责在销毁时销毁共享指针向量。默认情况下,通常共享指针会处理这个,但由于它是一个向量,因此也会调用向量析构函数(它也会销毁所有向量元素),但是没有权限销毁其他对象共有的内容。