新建/删除导致访问冲突
好吧,这让我很困惑...下面的代码位于 DLL 中,当我的控制台应用程序调用此代码时,它突然在 delete[] lpBuffer
行引发访问冲突。我一整天都在使用这段代码,根本没有改变它。到目前为止,它一直运行良好。
访问违规消息
rhcopy.exe 中 0x6948b1a5 处未处理的异常:0xC0000005:读取位置 0x4de1c37f 时发生访问冲突。
图书馆代码
#define MAX_PACKET_SIZE 0x3FFF
DWORD MyClass::GetFile( LPCSTR lpszRemoteFile, LPCSTR lpszLocalFile )
{
LPBYTE lpBuffer = NULL;
// ...
lpBuffer = new BYTE[MAX_PACKET_SIZE];
// ...
if( NULL != lpBuffer )
delete[] lpBuffer;
// ...
}
我做错了什么吗?
附带说明:我一直在考虑将 lpBuffer 转换为向量。意见?
编辑
我要感谢你们的帮助!但显然……问题不是在这里。问题实际上是调用应用程序中的 printf()
语句,该语句在调用 GetFile(...)
之后立即发生。对于造成的混乱,我深表歉意。看起来微软的调试工具并没有指向导致错误的行,而是指向最后执行的行。我已投票结束该问题。
Ok, this is perplexing me... The code below is in a DLL, and when my console application calls this code it is suddenly throwing an access violation at the line delete[] lpBuffer
. I have been using this code all day and have not changed it at all. Until now, it had been working just fine.
Access Violation Message
Unhandled exception at 0x6948b1a5 in rhcopy.exe: 0xC0000005: Access violation reading location 0x4de1c37f.
Library Code
#define MAX_PACKET_SIZE 0x3FFF
DWORD MyClass::GetFile( LPCSTR lpszRemoteFile, LPCSTR lpszLocalFile )
{
LPBYTE lpBuffer = NULL;
// ...
lpBuffer = new BYTE[MAX_PACKET_SIZE];
// ...
if( NULL != lpBuffer )
delete[] lpBuffer;
// ...
}
Am I doing something wrong?
On a side note: I have been thinking about converting lpBuffer
into a vector. Opinions?
edit
I want to thank you guys for your help! But apparently... That's not where the problem is. problem is actually a printf()
statement in the calling application that occurs right after the call to GetFile(...)
. I apologize for the confusion. It would seem that Microsoft's Debugging tool is not pointing to the line that caused the error, but rather the last line that executed. I have voted to close the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在此处显示的代码没有任何问题。
我想到了两种可能性:
错误实际上并不在
delete
中,而是在两侧的代码中。您通过使用无效指针覆盖某些随机内存或多次删除指针来破坏堆。
通过在显示的代码中切换到 RAII(即
vector
),这些问题都不会被捕获,但如果您在代码的其余部分中一致地使用它们,则可能会有所改善。There is nothing wrong in the code you've shown here.
Two possibilities come to mind:
The error is not actually in the
delete
but in the code just on either side.You've done something to corrupt the heap, by using an invalid pointer that overwrites some random memory or deleting a pointer more than once.
Neither of these problems would be caught by switching to RAII (i.e.
vector
) in the displayed code, but might improve things if you used them consistently in the rest of your code.鉴于发布的代码,我不太确定为什么这里会导致访问冲突。
由于缓冲区仅在本地引用,您可以将其设为堆栈变量而不是动态分配吗?
如果它必须是一个指针,也许你可以使用 Boost 智能指针来代替:
在向量点上,如果使用向量很容易,我会选择它或任何其他 STL 容器。
I'm not really sure why an access violation is caused here given the code posted.
Since the buffer is only referenced locally could you make it a stack variable instead of allocated dynamically?
If it must be a pointer maybe you could use a Boost Smart Pointer instead:
On the vector point, if it's easy to work with a vector I'd opt for it or any other STL Container when you can.