VC6/VC7自代STL实现的一个问题(vector与copy)
发现VC6/VC7中自代STL实现的一个问题.
对vector的对象调用过copy函数之后,此对象在析构中释放内存时,会出错!!!
我对同样的代码在FreeBSD中,用g++编译后运行不会出错.
程序代码:
//test.cpp---------------------------------start
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int fun()
{
std::cout <<"in fun" <<std::endl;
std::string buf;
buf = "jdsf;lasjdf;ajs";
std::vector<unsigned char> v;
v.resize(buf.size());
//如果对v调用这个拷贝调用,函数退出时就会出错.
std::copy( buf.begin(), buf.end(), v.end());
std::cout <<"out fun" <<std::endl;
return 0;
}
int main()
{
fun();
return 0;
}
//test.cpp---------------------------------end
函数调用栈:
_free_dbg(void * 0x00441d40, int 1) line 1033 + 60 bytes
free(void * 0x00441d40) line 926 + 11 bytes
operator delete(void * 0x00441d40) line 7 + 9 bytes
std::allocator<unsigned char>::deallocate(void * 0x00441d40, unsigned int 15) line 64 + 38 bytes
std::vector<unsigned char,std::allocator<unsigned char> >::~vector<unsigned char,std::allocator<unsigned char> >() line 60
fun() line 37 + 19 bytes
main() line 44
mainCRTStartup() line 206 + 25 bytes
KERNEL32! 77e889a5()
出错位置代码:
_free_dbg(void * 0x00441d40, int 1) line 1033 + 60 bytes
{
...
/* if we didn't already check entire heap, at least check this object */
if (!(_crtDbgFlag & _CRTDBG_CHECK_ALWAYS_DF))
{
/* check no-mans-land gaps */
if (!CheckBytes(pHead->gap, _bNoMansLandFill, nNoMansLandSize))
_RPT3(_CRT_ERROR, "DAMAGE: before %hs block (#%d) at 0x%08X.\n",
szBlockUseName[_BLOCK_TYPE(pHead->nBlockUse)],
pHead->lRequest,
(BYTE *) pbData(pHead));
if (!CheckBytes(pbData(pHead) + pHead->nDataSize, _bNoMansLandFill, nNoMansLandSize))
_RPT3(_CRT_ERROR, "DAMAGE: after %hs block (#%d) at 0x%08X.\n",
szBlockUseName[_BLOCK_TYPE(pHead->nBlockUse)],
pHead->lRequest,
XXX=> (BYTE *) pbData(pHead));
}
...
}