堆分配/释放导致堆栈溢出

kre*_*tar 1 c++ stack-overflow memory-management visual-studio-2005

编辑:只是为了说清楚,这个问题是由我的代码中的拼写错误引起的

pointer = new BYTE(datasize);
Run Code Online (Sandbox Code Playgroud)

本来应该

pointer = new BYTE[datasize];
Run Code Online (Sandbox Code Playgroud)

一切都很好!

结束

嗨!

我在C++项目中的Visual Studio 2005中遇到了奇怪的堆栈溢出问题.

在我的代码中,我有一个

BYTE* pointer;
Run Code Online (Sandbox Code Playgroud)

此指针设置为NULL,然后分配一些内存,然后清除为0x00.像这样:

pointer = NULL;
pointer = new BYTE(dataSize);
memset(pointer,0x00,dataSize);
Run Code Online (Sandbox Code Playgroud)

现在,我已经运行了几次,得到了两个不同的结果.有时候(在程序的后期,当我用delete []删除指针时)它表示堆已损坏,并且在检查调用堆栈似乎_CrtIsValidHeapPointer断言它不是有效指针.但是我检查了这个指针,它似乎对我有效(它有一个内存地址).我错过了什么吗?

在另一种情况下,应用程序冻结了很短的时间,我得到一个堆栈溢出消息.当我检查调用堆栈时,它看起来像这样

 something.dll!_heap_alloc_base(unsigned int size=568)  Line 105 + 0x28 bytes   C
something.dll!_heap_alloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 411 + 0x9 bytes C++
something.dll!_nh_malloc_dbg(unsigned int nSize=532, int nhFlag=0, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 268 + 0x15 bytes   C++
something.dll!_malloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 191 + 0x1b bytes    C++
something.dll!_calloc_dbg(unsigned int nNum=1, unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 563 + 0x15 bytes   C++
something.dll!_getptd_noexit()  Line 608 + 0x18 bytes   C
something.dll!_errno()  Line 281 + 0x5 bytes    C
something.dll!_heap_alloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 415 + 0x5 bytes C++
something.dll!_nh_malloc_dbg(unsigned int nSize=532, int nhFlag=0, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 268 + 0x15 bytes   C++
something.dll!_malloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 191 + 0x1b bytes    C++
something.dll!_calloc_dbg(unsigned int nNum=1, unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 563 + 0x15 bytes   C++
something.dll!_getptd_noexit()  Line 608 + 0x18 bytes   C
something.dll!_errno()  Line 281 + 0x5 bytes    C
something.dll!_heap_alloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 415 + 0x5 bytes C++
something.dll!_nh_malloc_dbg(unsigned int nSize=532, int nhFlag=0, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 268 + 0x15 bytes   C++
something.dll!_malloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 191 + 0x1b bytes    C++
something.dll!_calloc_dbg(unsigned int nNum=1, unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 563 + 0x15 bytes   C++
something.dll!_getptd_noexit()  Line 608 + 0x18 bytes   C
something.dll!_errno()  Line 281 + 0x5 bytes    C
something.dll!_heap_alloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 415 + 0x5 bytes C++
something.dll!_nh_malloc_dbg(unsigned int nSize=532, int nhFlag=0, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 268 + 0x15 bytes   C++
something.dll!_malloc_dbg(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 191 + 0x1b bytes    C++
something.dll!_calloc_dbg(unsigned int nNum=1, unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x0627fec8, int nLine=608)  Line 563 + 0x15 bytes   C++
something.dll!_getptd_noexit()  Line 608 + 0x18 bytes   C
something.dll!_errno()  Line 281 + 0x5 bytes    C
Run Code Online (Sandbox Code Playgroud)

有很多这些调用,我很确定这会导致堆栈溢出.任何想法可能是什么问题?我试过看看我是否分配/解除分配错误,但我没有写入我在memset中分配的内存块.

我分配/取消分配内存的方式有问题吗?

我正在使用此代码取消分配:

if (pointer != NULL){
    delete[] pointer;
    pointer = NULL;
}
Run Code Online (Sandbox Code Playgroud)

这应该确保我没有释放已经被释放的内存吗?

谢谢..

编辑:在输出窗口,我也得到了这些TON:

First-chance exception at 0x76df0839 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e2871f in app.exe: 0xC0000005: Access violation reading location 0x00000004.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
First-chance exception at 0x76e00a65 in app.exe: 0xC0000005: Access violation reading location 0xfffffff8.
Run Code Online (Sandbox Code Playgroud)

And*_*rsK 6

我想你的意思是:

pointer = new BYTE[size];
Run Code Online (Sandbox Code Playgroud)