【发布时间】:2011-05-10 06:09:09
【问题描述】:
我正在尝试使用 C++ 编写一个简单的代码,但是当我删除指针时出现Debug Assertion Failed _BLOCK_TYPE_IS_VALID 错误。我不知道我做错了什么。这是我的代码。
hash_map<string,string> m_hashDetails;
m_hashDetails.insert(hash_map<string,string>::value_type("test",*(new string("test123"))));
hash_map<string,string>::iterator myIterator;
myIterator = m_hashDetails.find("test");
if(myIterator == m_hashDetails.end())
{
printf("not found");
}
else
{
printf(myIterator->second.c_str());
//this is where I get Debug Assertion Failed _BLOCK_TYPE_IS_VALID
delete &(myIterator->second);
}
当我删除hash_map 的second 字段时,我得到Debug Assertion Failed _BLOCK_TYPE_IS_VALID 错误。我做错了什么?我已经使用new 运算符分配了second 字段?我注意到的一件事是,如果我将 hash_map 定义更改为
hash_map<string,string *> m_hashDetails; 并插入类似的值
m_hashDetails.insert(hash_map<string,string>::value_type("test",new string("test123")));
然后delete 没有给出错误.. 并且工作正常?这个错误的真正原因是什么?
【问题讨论】:
标签: c++ new-operator assertions delete-operator