C++读取访问冲突为什么?
我正在开发该应用程序,它将使用 C++/Winsock 通过 pop/imap 协议从服务器获取邮件。
因为,代码太大,无法粘贴到这里,我给你了pastebin上的链接:
http://pastebin.com/uCKcTQsj
它没有任何编译器错误,因此可以很好地编译。
我得到了不同的工作结果,有时一切正常,但通常我得到的结果是:
alex.exe 中 0x773f15de (ntdll.dll) 处出现未处理的异常:0xC0000005:
读取位置 0x00648000 时出现访问冲突。
为什么我会得到不同的工作结果(经常发生访问冲突,但有时它有效)?
有没有什么方法可以捕获和处理违规异常(可能来自较低的安全环,据我所知,这些异常是在本机模式下从 ntdll.dll 抛出的)来解决这个问题?
谢谢,最诚挚的问候!
I'm developing the application, which will get mails from servers via pop/imap protocols using C++/Winsock.
Cause, the code is large to paste here , I give you the link on pastebin:
http://pastebin.com/uCKcTQsj
It doesn't have any compiler errors, so it can be copmpiled well.
I'm getting different result of its work, sometimes all working ok, but often I get the result:
Unhandled exception at 0x773f15de (ntdll.dll) in alex.exe: 0xC0000005:
Access violation reading location 0x00648000.
Why do I get different result of its work ( often cathing access violation, but sometimes it works )?
Are there any ways to catch and handle violation exceptions ( may be from lower secrity rings, as I understand , these exceptions are throwing from ntdll.dll in native mode ) to fix this problem?
Thanks, best regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
访问冲突意味着您正在访问尚未分配给应用程序的内存。
您会得到随机的成功/失败,因为有时您尝试访问的内存会分配给您的应用程序,有时则不会。
无论哪种方式,问题都是您试图访问尚未进行malloc(或new)的内存。即,您要么尝试取消引用/写入/读取“悬空”或“野生”指针,要么尝试读取/写入超出正确分配的指针指向的内存块的边界。
Access violation means that you're accessing memory that hasn't been allocated to your application.
You get random successes/failures because sometimes memory that you're trying to access will be allocated to your application, sometimes it won't be.
Either way, the problem is that you're trying to access memory that hasn't been
malloc
ed (ornew
ed). I.e. you're either trying to dereference/write to/read from a "dangling" or "wild" pointer, or you're trying to read/write beyond the boundaries of a memory block a properly allocated pointer points to.您的标头结构包含此数组:
但是您在多个地方将其视为以 null 结尾的 C 字符串,而没有确保它实际上是以 null 结尾的。
例如,在
Inet::GetResponse()
中:我认为
strlen(buffer)
不会返回任何有意义的内容。您可能应该使用sizeof(buffer)
。但是,即使您正在处理接收到的数据,也不能保证数据将以空终止 - 即使协议确实如此以空值终止记录。使用 TCP,
recv()
只能返回已发送记录的一部分,因此即使在发送 null 的情况下,也可能尚未收到该记录。特别是,POP3 和 IMAP 协议不会以空八位字节终止响应。因此,除非您确保'\0'
存在,否则无法使用strlen()
及其友元。您需要仔细重新检查如何处理缓冲区大小和代码中接收的数据量。
Your header struct contains this array:
But you treat it as a null terminated C string in several places without making sure that it is actually null terminated.
For example, in
Inet::GetResponse()
:I don't think that
strlen(buffer)
will return anything meaningful.You probably should usesizeof(buffer)
.But even when you're dealing with received data, there's no guarantee that the data will be null terminated - even if the protocol does terminate records with nulls. With TCP a
recv()
can return only part of a sent record, so even in situations where a null is sent, it might not be received yet. In particular, the POP3 and IMAP protocols do not terminate responses with a null octet. So unless you make sure the'\0'
is there,strlen()
and friends cannot be used.You need to carefully reexamine how you're handling the buffer size and the amount of data received in that code.