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

pointer = new BYTE(datasize);

应该是

pointer = new BYTE[datasize];

一切都很好!

<强> END

嗨!

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

在我的代码中,我有一个

BYTE* pointer;

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

pointer = NULL;
pointer = new BYTE(dataSize);
memset(pointer,0x00,dataSize);

现在,我已经运行了几次,得到了两个不同的结果。有时候(在程序的后期部分,当我用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

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

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

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

if (pointer != NULL){
    delete[] pointer;
    pointer = NULL;
}

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

谢谢..

编辑:在输出窗口中,我也得到了这些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.
有帮助吗?

解决方案

我认为你的意思是:

pointer = new BYTE[size];

其他提示

您的取消分配代码应为:

if (pointer != NULL){
    delete[] pointer;
    pointer = NULL;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top