c++ - ( vector ?)析构函数中的读取访问冲突

标签 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” vector 时。 这是来自 visual studio 的调用堆栈。

enter image description here

我想知道我是否需要在这里描述一个析构函数,或者默认值是否应该正确处理 vector 和映射成员?

最佳答案

这里有很好的引用:http://www.cplusplus.com/reference/memory/shared_ptr/ 您必须在 CacheRegStream 中包含一个析构函数,它释放共享指针 vector 的所有权,而不是尝试删除该 vector 。 (默认析构函数试图破坏仍由其他对象拥有的共享指针,本质上是试图删除它们的成员,这是越界的。)

析构函数要处理的一个特殊情况是最后拥有的对象在销毁时负责销毁共享指针 vector 。通常共享指针会默认处理这个问题,但由于它是一个 vector , vector 析构函数也会被调用(它也会破坏所有 vector 元素),但没有权限破坏其他对象共同拥有的东西.

关于c++ - ( vector ?)析构函数中的读取访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37006543/

相关文章:

C++11,仅将一个字段复制到 vector 中

c++ - 内存分配不够?

visual-studio - 为什么在 Silverlight 4/VS 2010 的命名空间内使用 using 指令?

c++ - Microsoft Visual Studio 2008 正在与其所依赖的项目并行构建依赖项目

c++ - 用于 cpp 文件的 emacs flycheck 语法检查程序

java - 简单快速的方法来计算二进制整数到0的汉明距离?

mysql - MySQL for Visual Studio 1.2.8 安装失败

c# - WebForm.ReportViewer 无限循环问题

javascript - VS 2015-typescript 不生成 javascript 文件

c++ - C++ 中是否有链接哈希集?