调试访问冲突错误? [英] Debugging Access Violation errors?

查看:19
本文介绍了调试访问冲突错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Delphi 编写应用程序时,您可以分享哪些技巧来帮助定位和修复访问冲突?

What tips can you share to help locate and fix access violations when writing applications in Delphi?

我认为访问冲突通常是由于尝试访问内存中尚未创建的内容(例如对象等)引起的?

I believe access violations are usually caused by trying to access something in memory that has not yet been created such as an Object etc?

我发现很难确定是什么触发了访问冲突,然后在何处进行必要的更改以尝试停止/修复它们.

I find it hard to identify what triggers the access violations and then where to make the required changes to try and stop/fix them.

一个例子是我现在正在做的一个个人项目.我在 TTreeView Node.Data 属性中存储了每个节点的一些数据.节点可以被多个选择和导出(导出遍历每个选定的节点并将特定数据保存到文本文件 - 保存到文本文件的信息是存储在 nodes.data 中的信息).也可以将文件导入到Treeview中(将文本文件的内容保存到node.data中).

A example is a personal project I am working on now. I am storing in TTreeView Node.Data property some data for each node. Nodes can be multiple selected and exported (the export iterates through each selected node and saves specific data to a text file - the information saved to the text file is what is stored in the nodes.data). Files can also be imported into the Treeview (saving the contents of the text files into the node.data).

该示例中的问题是,如果我将文件导入到 Treeview 中,然后将它们导出,则效果很好.但是,如果我在运行时添加一个节点并导出它们,我会得到:

The issue in that example is if I import files into the Treeview and then export them, it works perfect. However if I add a node at runtime and export them I get:

模块'Project1.exe'中地址00405772的访问冲突.读取地址00000388."

"Access Violation at address 00405772 in module 'Project1.exe'. Read of address 00000388."

我对此的想法一定是我将数据分配给创建的节点的方式,可能与我在导入时分配数据的方式不同,但对我来说这一切都很好.访问冲突仅在导出时出现,导入的文件不会发生这种情况.

My thoughts on that must be the way I am assigning the data to created nodes, maybe differently to the way I assign it when they are imported, but it all looks ok to me. The access violation only shows up when exporting, and this never happens with imported files.

我不是在寻找对上述示例的修复,但主要是建议/提示如何查找和修复此类错误.我不经常遇到访问违规的情况,但是当我遇到这些违规情况时,真的很难找到并修复.

I am NOT looking for a fix to the above example, but mainly advice/tips how to find and fix such type of errors. I don't often get access violations, but when I do they are really hard to track down and fix.

所以建议和技巧会非常有用.

So advice and tips would be very useful.

推荐答案

这意味着您的代码正在访问它不允许访问的内存的某些部分.这通常意味着您有一个指向错误内存的指针或对象引用.可能是因为它没有初始化或者已经被释放了.

It means your code is accessing some part of the memory it isn't allowed to. That usually means you have a pointer or object reference pointing to the wrong memory. Maybe because it is not initialized or is already released.

使用调试器,如 Delphi.它会告诉您 AV 发生在哪一行代码.从那里通过查看调用堆栈和局部变量等找出您的问题.有时,如果您使用调试 DCU 进行编译也会有所帮助.

Use a debugger, like Delphi. It will tell you on what line of code the AV occurred. From there figure out your problem by looking at the callstack and local variables etc. Sometimes it also helps if you compile with Debug DCUs.

如果您没有调试器,因为它只发生在客户端,您可能需要使用 MadExcept 或 JclDebug 来记录带有调用堆栈的异常并将其发送给您.它提供的细节较少,但可能会为您指明正确的方向.

If you don't have a debugger because it only happens on a client side, you might want to use MadExcept or JclDebug to log the exception with callstack and have it send to you. It gives you less details but might point you in the right direction.

有些工具可以通过更积极地检查来更早地发现此类问题.FastMM 内存管理器有这样的选项.

There are some tools that might be able to find these kind of problems earlier by checking more aggressively. The FastMM memory manager has such options.

编辑

"地址 00405772 处的访问冲突在模块Project1.exe"中.阅读地址 00000388."

"Access Violation at address 00405772 in module 'Project1.exe'. Read of address 00000388."

因此,您的问题导致模块Project1.exe"中地址为 00405772 的 AV.Delphi 调试器会将您带到正确的代码行(或使用 Find Error).

So your problem results in a AV at addresss 00405772 in module 'Project1.exe'. The Delphi debugger will bring you to the right line of code (or use Find Error).

它正在尝试读取地址 00000388 处的内存.这非常接近于 00000000 (nil),因此这可能意味着访问一些指向数组或 nil 的动态数组的指针/引用.如果它是一个字节数组,它将是项目 388.或者它可能是一个相当大的对象的字段或具有很多字段的记录.对象或记录指针/引用将为 nil.

It is trying to read memory at address 00000388. That is pretty close to 00000000 (nil), so that would probably mean accessing some pointer/reference to an array or dynamic array that is nil. If it was an array of bytes, it would be item 388. Or it could be a field of a rather large object or record with lots of fields. The object or record pointer/reference would be nil.

这篇关于调试访问冲突错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆