12

I can't seem to figure this one out. My program compiles and runs successfully, but during debugging only it pops up a message box saying "Invalid Pointer Operation" when shutting the program down. I have painstakingly checked all the FormCloseQuery and FormDestory events for any syntax or logical error. I found none and they execute as expected without any error.

在此处输入图像描述

When I do tell the compiler to break at Invalid Pointer Operation error, it doesn't do anything but hangs up the program. At which point, I had to terminate or kill the process.

How do you figure this one out?

Thanks in advance,

4

4 回答 4

33

An Invalid Pointer exception is thrown by the memory manager when it tries to free invalid memory. There are three ways this can happen.

The most common is because you're trying to free an object that you've already freed. If you turn on FastMM's FullDebugMode, it will detect this and point you directly to the problem. (But make sure to build a map file so it will have the information it needs to create useful stack traces from.)

The second way is if you're trying to free memory that was allocated somewhere other than the memory manager. I've seen this a few times when passing a string from a Delphi EXE to a Delphi DLL that wasn't using the shared memory manager feature.

第三种方式涉及直接弄乱指针,可能不适用于您。如果您尝试FreeMem使用Dispose不指向 FastMM 分配的实际内存块的错误指针,您将收到此错误。

这很可能是第一个。使用FullDebugMode,您会很容易找到问题的根源。

于 2012-04-11T20:30:43.610 回答
11

当您告诉 Delphi 内存管理器释放不属于它的内存时,会发生无效的指针操作。可能发生的方式有以下三种:

  • 释放已释放的指针或对象。
  • 用于释放由其他内存管理器FreeMem(例如GlobalAllocCoTaskMemAlloc)分配的内容。
  • 释放未初始化的指针。(这与释放空指针不同,后者是完全安全的。)

在你的程序的某个地方,你正在做这些事情之一。调试器检测到内存管理器抛出的异常,所以做一些调试。从堆栈跟踪中,您应该能够看到您要释放的变量。检查程序的其余部分以了解使用该变量的其他方式。

MadExcept 和 Eureka Log 等工具可以帮助您找到双重释放错误。他们可以跟踪相关指针的分配位置以及第一次释放的位置,这有时足以找出您的错误并停止多次释放。

于 2013-01-18T15:33:55.107 回答
2

可能发生无效指针操作的第四个原因。我有两个指针指向实数数组 [0..1000],第三个指针是实数数组 [1..200]。用 for i := 0 到 1000 初始化的所有 3 个指针都开始 ptr1^[i]:=0;ptr2^[i]:=0;ptr3^[i]:=0; 结尾; 虽然这种糟糕的编程并没有打扰 Delphi 中的 Pascal,但调用 Dispose 3 个指针中的任何一个都会导致无效的指针操作。修复只是正确初始化第三个指针。

于 2016-09-23T21:07:22.787 回答
0

在 Delphi 调试期间,我被这种类型的“指示错误”抓住了。

检查您是否有任何启用了“允许函数调用”的监视变量,或者尝试在同一单元(或全局)中显示可能未初始化的其他变量的监视。当在断点处停止时,这可能会导致 Delphi 的调试器尝试通过访问未初始化的指针或变量的函数调用来显示值。导致 AV 的实际变量甚至不在你的观察名单上。

于 2013-01-18T14:28:38.820 回答