转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Delphi Access violations 问题的解决之道         ★★★★

Delphi Access violations 问题的解决之道

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2373 更新时间:2009/4/23 18:44:43
indow than the following message pops up:

The message states that the EAccessViolation has occurred. The EAccessViolation is the exception class for invalid memory access errors. This is what you will see while developing your application. The next message box will see the user - and the program will die:

Access violation at address 0043F193
in module ''''Project1.exe''''
Read of address 000000.

The first hex number (''''0043F193'''') is the address of the runtime error in the compiled code (Project1.exe) were the AV occurred. In the IDE choose menu option ''''Search|Find Error...'''', enter the address at which the error occurred (''''0043F193'''') in the dialog and click OK. Delphi will now recompile your project and show you the line of source code were the runtime error occurred, that is, the BadForm.Refresh.

What follows is a list of the most common causes of AVs in Delphi development. This list is not, and it can''''t be, intended to cover all the situations in which an AV can occur. Please post your AV to the Forum - we''''ll try to solve them together - real life examples are normally more obscure than those that follow.

Calling a non-existing object
As stated above the most probable cause of AVs is the use of an object that has not yet been created or has already been destroyed. To prevent this kind of AV, be sure than any objects you refer to, have first been created. For example, you might be opening a Table in the form''''s OnCreate event when the Table component is located on a data module which has not been created yet (was removed from the auto-crete forms list).
In the next code an AV occurs after calling a method on an object (b:TBitmap) that has already been destroyed.

var b:TBitmap;
begin
 b:=TBitmap.Create;
 try
  //do something with b
 finally
  b.free;
 end; 
 ...
 //this will cause an AV - b does not longer exist
 b.Canvas.TextOut(0,0,''''this is an Access Violation'''');

end;

Invalid API parameter
If you attempt to pass an invalid parameter to a Win API procedure an AV might occur. The best way to solve this kind of AV is to refer to the Win API help for information on the particular API call and the parameters and parameter types it expects. For example, always be sure not to pass an invalid pointer to a buffer parameter.

Let Delphi Free
When an object ownes another object, let it do the deleting. Since, by default, all forms (autocreated) are owned by the Application object, when the application terminates, it frees the Application object, which frees all forms. For example, if you have two forms (Form1/Unit1 and Form2/Unit2) both autocreated when the application starts, the next code will result in an AV:

unit Unit1;
...
uses unit2;
...
procedure TForm1.Call_Form2
begin
 Form2.ShowModal;
 Form2.Free;
 Form2.ShowModal; //AV

end;

Killing the exception
Never destroy the temporary exception object (E). Handling an exception automatically destroys the exception object. If you destroy the object yourself, the application attempts to destroy the object again, generating an access violation.

Zero:=0;
try
 dummy:= 10 / Zero;
except
 on E: EZeroDivide do
   MessageDlg(''''Can not divide by zero!'''',
               mtError, [mbOK], 0);
 E.free. // causes an access violation

end;

Indexing an empty string
An empty string has no valid data. Therefore, trying to index an empty string is like trying to access nil and will result in an access violation:

var s: string;
begin
 s:='''''''';
 s[1]:=''''a''''; //AV

end;

Dereferencing pointers
You must dereference the pointers, otherwise you are moving the address of the pointers and possibly corrupting other memory locations.

procedure TForm1.Button1Click(Sender: TObject);
var
  p1 : pointer;
  p2 : pointer;
begin
  GetMem(p1, 128);
  GetMem(p2, 128);
 {This line may cause an access violation}
  Move(p1, p2, 128);
 {This line is correct}
  Move(p1^, p2^, 128);
  FreeMem(p1, 128);
  FreeMem(p2, 128);

end;

   That''''s all folks
I hope that now, your users will scream less about how your application crashed.

 

 

 

 

Random access violations in the IDE

Delphi used to work fine but now I get random access violations in the IDE! Why?

 

You most likely installed a bad component or package that was not written/compiled for Delphi 4. Try uninstalling custom components one by one (or package by package) until the problem goes away, then get in touch with the component vendor regarding this issue. 2/0 / 998 2:59:06 PM

 

 

 

上一页  [1] [2] 


[其他]手工升级ACCESS到SQLSERVER方法详解  [Web开发]把ACCESS的数据导入到Mysql中的方法详解
[系统软件]InstallShield Express for delphi制作安装程序定…  [常用软件]InstallShield Express制作Delphi数据库安装程序
[C语言系列]SQL Server连接ACCESS数据库的实现  [C语言系列]应用 SQLServer 链接服务器访问远程 Access 数据库
[VB.NET程序]WindowsForm登陆窗体的制作(Vb.net+Access)  [VB.NET程序]在VB.NET中使用MS Access存储过程 — 第二部份
[VB.NET程序]在VB.NET中使用MS Access存储过程 — 第一部份  [VB.NET程序]如何在Visual Basic 6.0 中连接加密的Access数据库
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台