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数据库
|