【发布时间】:2015-03-08 01:14:30
【问题描述】:
这是一个从文件中读取二进制数据然后返回指向对象的指针的方法。
Database* Database::open(const char *path)
{
ifstream ifs;
ifs.open(path, ios::in | ios::binary);
if(!ifs)
{
cerr << "Failed to open database." << endl;
return NULL;
}
config_vars cfg;
ifs.read((char*)&cfg, sizeof(config_vars));
if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 )
{
cerr << "Failed to read database file." << endl;
return NULL;
}
ifs.close();
Database *db = new Database();
db->config = cfg;
db->db_path = string(path);
return db;
};
调用栈显示是通过销毁config_vars结构体的字符串成员来表示的,其定义如下:
struct config_vars
{
string name;
string author;
int date;
};
我无法真正理解导致访问冲突的原因。如果重要的话,该方法也是静态的。
调用栈:
msvcp100d.dll!std::_Container_base12::_Orphan_all() Line 201 + 0x12 bytes C++
NoDB.exe!std::_String_val<char,std::allocator<char> >::~_String_val<char,std::allocator<char> >() Line 478 + 0xb bytes C++
NoDB.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> >() Line 754 + 0xf bytes C++
NoDB.exe!config_vars::~config_vars() + 0x54 bytes C++
> NoDB.exe!Database::open(const char * path) Line 24 + 0x1b bytes C++
【问题讨论】:
-
什么访问冲突?你没有在这里显示任何东西。
Database是什么?错误是什么?你如何运行这段代码? -
@Mat:这与序列化向量有什么关系?请不要滥用你的欺骗锤。
-
@Selenir:在继续之前请阅读stackoverflow.com/help/mcve。
-
@Mat:但这不是完全相同的问题。你不能只是欺骗关闭所有可能有相似答案的东西。
标签: c++ string memory struct destructor