Analysis of Delphi Access violation error

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/delphi7456/archive/2010/11/13/1876180.html
Delphi Access Violation Error Analysis Delphi What are the common run-time Access violation errors? How to prevent? Any software development will encounter a situation where you write the program and test it and send it everywhere, and the user tells you that it failed. You might consider compiling your program with the compile instruction {$D}--delphi to create an image file that helps locate the source code for an access violation error. Project Options dialog box (project| options| Linker & Compiler) lets you specify everything you need. For the unit file, the debug information and the unit's object code are recorded in the unit file. When compiling a program that uses this unit, the debug information increases the size of the unit file and adds additional memory overhead, but it does not affect the size and speed of the final executable file. Contains debug information and image files (project| options|   Linker) option does not complete the line information until the {$D +} compilation directive. Access violation usually manifests itself only in one aspect of the program. When the problem first occurs, it is important to consider what the user is doing, and then find a breakthrough here. From the user's point of view, your program stops their work, and they tell you that the problem appears to have you postpone solving the problem.   However, communicating with users is the only effective way to identify problems and improve procedures.   Now you will know how to easily find the exact path, the source code file, the line where the access violation error occurred, in case of only giving you the conflicting address: "Search-find error ...". When a run-time Access violation appears, your users get an error message similar to the following: Access violation at address < hexadecimal value > in module < application name > Read of AD Dress < hex value > If your program contains debug information compilation in the Delphi IDE, you can navigate to the line that causes this error source code. One of the most common causes of Access violation errors in Delphi programs is the use of an object that has not been created. If the second address < hexadecimal value > is FFFFFFF or 0000000, is the likelihood that you visit? An object that has not been built. For example, you invoke an event for a form, but the form is not created automatically,There is no code instantiation. Procedure Tfrmain.oncreate (Sender:tobject); var badform:tbadform; Begin//The access violation Badform.refresh will be generated here;  End Suppose Badform is in the list of "Available Forms" windows of the Project option-this window needs to be created and released manually.   Calling the Refresh method of the Badform window in the code above will cause access violation. If you make "Stop on Delphi Exceptions" effective in the Debugger Options window, the following message pops up: The message states that the eaccessviolation have occurred.   The eaccessviolation is the exception class for invalid memory access errors.   This is the information you will see when you design the program, the next message box will appear, and then the program fails: Access violation at address 0043f193 in module ' Project1.exe ' Read of address 000000. The first hexadecimal number 0043f193 is the address of the run-time error of the compiled code (Project1.exe) that occurs with access violation. Select the menu item "search|" in the IDE. Find error ... ", click the" OK "button after entering the address (0043f193) in the dialog box.   Delphi will recompile your project file and then display the line of code where the run-time error occurred, which is the Badform.refresh line. The following is a list of the most common causes of access violation errors in the Delphi environment. This list is not and is not likely to overwrite all possible access violation scenarios. Please send your access violation information on the forum, and you can try to solve the problem together-real real-world cases are generally more obscure than listed errors. 1. Calling a nonexistent object as mentioned above, most access violation are justified by using objects that have not been created or have been disposed of. To prevent this type of access violation from happening, make sureAny objects you visit are created first.   For example, when a table is positioned in a data module that is not created (removed from the Auto-crete window), you may open the table in the OnCreate event of the form. In the following code, after invoking an object that has been deleted (B:tbitmap) event, an access violation appears: Var B:tbitmap; Begin B:=tbitmap.create; Try//perform some operations on the B object finally b.free; End ...//Because B has been released, an access violation error will appear b.canvas.textout (0,0, ' This is an access violation '); End;2. Non-existent API parameter if you try to pass a non-existent parameter to the WIN API function, an access violation error will appear. The best way to resolve this type of access violation error is to consult the win API help to see the parameter information and parameter types of this API function call. For example, it is always guaranteed not to pass an invalid pointer to a buffer parameter. 3. Let Delphi release when an object has another object, let it do the deletion work for you. Because by default, all forms (automatically created) belong to the Application object. When an application finishes, it frees the Application object and releases all the forms. For example, if you automatically create two forms (Form1/unit1 and Form2/unit2) at the beginning of a program, the following code causes access to violation errors: unit Unit1; ..... uses unit2; ... procedure tform1.call_form2 begin form2.showmodal; Form2.free; Access violation error will appear form2.showmodal; End;4. Killing an exception never destroys a temporary exception object (E), and handling an exception automatically frees the exception object. If you manually release the exception object, the program tries to release it again, and then an access violation error occurs: zero:=0; Try dummy:= 10/zero; Except on E:ezerodivide do Messagedlg (' Can't divide by 0! '), Mterror, [Mbok], 0); E.free.////access violation error will appear end;5. Retrieving an empty string an empty string is without any data. That is, retrieving an empty string is equivalent to accessing an object that does not exist, which causes access violation error: Var s:string; Begin s:= '; S[1]:= ' a '; Access violation error will appear end; the Access violation error I encountered today is due to retrieving an empty string caused by the if amodalarray[i]. helpkeyword= ' background ' thencbbNewParent.Items.Add (Amodalarray[i]. Caption); when Amodalarray[i]. Helpkeyword= "is the first way to avoid the exception of Amodalarray[i]. Whether the HelpKeyword is empty is judged by 6. Direct reference pointers you must refer to pointers indirectly, otherwise you will change the pointer address and may break other storage units: procedure Tform1.button1click (Sender:tobject); var p1:pointer; P2:pointer; Begin Getmem (P1, 128); Getmem (p2, 128); The next line causes access to violation error Move (P1, p2, 128); The next line of methods is the correct Move (p1^, p2^, 128); Freemem (P1, 128); Freemem (p2, 128);   End   These are all my suggestions for the run-time access violation error, and I hope you can also make some comments about the access violation errors that appear in your program. Believe that all readers have encountered the "Access violation" error, if not their own program, we have a lot of people to blame on Bill Gates. If your own program has this embarrassing error, how can we explain it in the face of user inquiries? This article is the best answer.

Analysis of Delphi Access violation error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.