错误 Delphi XE2 - 异常类 $C00000005 [英] Error Delphi XE2 - Exception class $C00000005

查看:42
本文介绍了错误 Delphi XE2 - 异常类 $C00000005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调试一个项目时遇到此错误,该项目以前在 Delphi 7 中,我一直在升级到 Delphi XE2,在几种方法中都会发生相同的错误.

I am getting this error will debugging a project, which used to be in Delphi 7 and I have been upgrading to Delphi XE2, the same error happens in several methods.

First chance exception at $006DC660. Exception class $C0000005 with message 'access violation at 0x006dc660 read of address 0xffffffff' 

这是其中一种方法:

PFI = ^TFI;           
TFI = record
Id         : TToken;
Name       : TName;
Parameters : string;
end;

function TListFI.IsIn(S: PChar): PFI;

  function SearchName2(Item: PFI):Boolean;
  var N1, N2: PChar;
  begin
    N1:= StrNew(Item^.Name);
    N2:= StrNew(S); //Here is the issue
    SearchName2:= (StrComp(StrUpper(N1), StrUpper(N2)) = 0);
    StrDispose(N1);
    StrDispose(N2);
  end;

begin
  IsIn:= PFI(FirstThat(@SearchName2));
end;

我用谷歌搜索,我发现有人描述了类似的问题,他确认当增量链接器被禁用时它可以工作,有人可以告诉我它是什么以及它在哪里或提供一些建议来解决这种情况.

I have googled and I found someone describing a similar problem, and he affirms that when the incremental linker is disabled it works, can someone tell me what and where is it or give some advice to solve this situation.

现在删除 @ 在 IsIn 中给我以下错误:= PFI(FirstThat(SearchName2));

Removing the @ now gives me the following error in IsIn:= PFI(FirstThat(SearchName2));

E2010 Incompatible types: 'TObject' and 'PFI'

我正在添加 FirstThat 程序,看看它是否有帮助.

I am adding the FirstThat procedure to see if it may help.

TFuncionColeccion = function (Elemento: TObject): Boolean;

function TColeccion.FirstThat (Rutina: TFuncionColeccion): TObject;
var
  i: Integer;
begin
  For i:=0 to Count-1 do
    if Rutina(Items[i]) then
    begin
      FirstThat:=Items[i];
      exit;
    end;
  FirstThat:=nil;
end;

推荐答案

通过指针调用本地(嵌套)过程是(并且一直是)一个错误,这显然是您的FirstThat功能确实.编译器必须对堆栈做一些特殊的事情来调用局部函数并让它们访问父作用域的变量(代码中的 S),但是编译器只能在本地函数被直接调用.编译器无法知道 FirstThat 的参数将是一个本地函数,因此当 FirstThat 调用指向的函数时,它不包含特殊代码.

It is (and always has been) an error to call local (nested) procedures by pointer, which is clearly what your FirstThat function does. The compiler has to do special things with the stack to call local functions and give them access to the parent scope's variables (S in your code), but the compiler can only know to do those special things when the local function is called directly. The compiler cannot know that the argument to FirstThat will be a local function, so it doesn't include the special code when FirstThat invokes the pointed-to function.

最重要的是,函数内部的堆栈没有按照预期的方式设置,这意味着可能会出现任何数量的奇怪症状.您将不得不使用其他方式.也许让 SearchName2 成为一个双参数函数,然后编写 FirstThat 来接受 S 作为参数,它可以转发给函数参数.

The bottom line is that the stack inside the function doesn't get set up the way it's supposed to, and that means any number of strange symptoms may appear. You'll have to use some other way. Maybe make SearchName2 be a two-argument function, and then write FirstThat to accept S as a parameter that it can forward to the function argument.

在构造函数指针时,您不应该需要使用 @ 运算符.当您这样做时,编译器往往会跳过类型检查,这首先允许您将本地函数指针传递给 FirstThat.当您传递的函数确实与所需的原型匹配时,编译器将允许您在不使用 @ 运算符的情况下传递它.

You shouldn't need to use the @ operator when constructing a function pointer. When you do, the compiler tends to skip type checking, which is what allowed you to pass a local function pointer to FirstThat in the first place. When the function you're passing really matches the required prototype, the compiler will allow you to pass it without the @ operator.

这篇关于错误 Delphi XE2 - 异常类 $C00000005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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