Access violation at address Problem and Solution

Source: Internet
Author: User

Windows users may often see errors similar to the following: "error: accessviolation at address 836556f8. Read of address 836556f8 ". As a Delphi program developer, there are more opportunities for such errors than other users (pai_^ ).

What is "accessviolation "? How can we avoid it during the design period?

Accessviolation (unauthorized access), generalprotection fault (general protective error), or invalidpage fault (invalid page error). Although they are different, they are always caused by the same error. Access violation is often encountered when a program running by a computer user attempts to access an unspecified bucket.

Access violation at address

In module <Application name>

Read of address

Once windows writes data information outside the allocated storage area, it overwrites commands or data of other programs or even operating systems. Once this happens, the operating system will be paralyzed or shut down in some form. You must restart the computer. For example, when the next program in Windows NT/2000 encounters this error. watson appeared and stopped the program, captured some fast details, and recorded them in text format. Access violation is one of the most annoying windows program errors. The purpose of this article is to help you find the solution to accessviolation in Delphi. First, declare that accessviolation has nothing to do with microsoftaccess.

When developing programs using Delphi, we can divide access violation into two categories: runtime and design.

I. accessviolation during the design period

1. Hardware reasons

Access violation during the design period may occur when delphiide is started or disabled and a Delphi project is compiled. The access violation information displayed on your computer may be caused by various reasons, including the system bios, operating system, or hardware driver line, some sound cards, video cards, and NICs may actually cause such errors. Why? Each card in a computer has its device driver. Different manufacturers, different versions of Windows, or different versions of Delphi may encounter different problems. The following steps may help you solve these problems:

1. Follow the necessary steps to verify that there is no conflict between the drivers you have installed.

2. Sometimes lowering the display resolution may make some weird graphics driver stable.

3. If you use a dual-processor motherboard, make sure that the modification steps for each processor are the same.

4. Use the latest driver for all hardware on the computer.

2. Software reasons

Although Windows is the most popular operating system on Intel computers, due to inherent Windows vulnerabilities and bugs, misoperations of applications may result in the rapid paralysis of the Operating System (sometimes the operating system itself will be inexplicably paralyzed ). Selecting a more stable development environment is the solution. The following steps can help you prevent access violation:

(1) Despite the popularity of Windows 9x, Windows NT/2000 has been proven to be a much more stable environment in many aspects, almost all windows code platforms.

(2) ensure that the latest service pack is installed for Windows NT/2000. After installing the new service pack, you will find that the machine becomes stable.

(3) install the Current updates or patches (BDE, ADO…) for your various versions of Delphi ......), This is a good way to prevent errors in advance. Try to use the latest Delphi patch-the number of access violation errors, especially the number of errors during the design period, will be greatly reduced.

(4) If you often encounter access violation errors randomly in IDE, it is likely that you have installed a bad control, package, or wizard, it is not written or compiled by the Delphi version you are using. Try to uninstall the custom control (or package) one by one until the problem is solved, and then contact the control vendor to follow up the problem.

(5) Check whether something useless in the computer conflicts with the program. Strange software programs and beta products often cause access violation errors.

(6) If the system is set incorrectly, access violation errors may also occur frequently. If you constantly encounter an access violation with the same error message, record these details and notify the software manufacturer that may cause this error.

These are all my suggestions for incorrect accessviolation during the design period.

Ii. accessviolation during runtime

What are common access violation errors in Delphi during runtime? How to prevent it?

Any software development may encounter such a situation: you write the program and test it, and then send it everywhere. The user tells you that it has failed.

You may consider using the compilation command {$ d} to compile your program -- Delphi can create an image file that helps locate the source code of access violation errors. The project | options | linker & compiler dialog box allows you to specify everything you need. For the unit file, the debug information and the unit object code are recorded in the unit file together. When compiling a program that uses this unit, the debug information increases the size of the unit file and increases the memory overhead, but it does not affect the size and running speed of the final executable file. Products that contain the debug information and image file (Project | options | linker) options are only available at {$ D +}
Compile the command to complete the line information.

Accessviolation is usually expressed only in one aspect of the program. When a problem occurs for the first time, it is very important to consider what operations the user has performed, and then find a breakthrough here. From the user's point of view, your program has terminated their work, and they will tell you that the problem seems to have delayed you to solve the problem. However, communicating with users is the only effective way for you to discover problems and improve your programs.

Now you can know how to easily find accurate paths, source code files, and access violation errors when only conflicting addresses are given to you:

"Search-finderror ...".

When an accessviolation occurs during the runtime, the error message you get is similar to the following:

Access violation at address

In module <Application name>

Read of address

If your program contains debug information compilation in delphiide, you can locate the line that causes this error source code.

In the Delphi Program, the most common cause of access violation error is that an uncreated object is used. If the second address

? Procedure tfrmain. oncreate (Sender: tobject );

VaR badform: tbadform;

Begin

// Access violation will be generated here

Badform. Refresh;

End;

Assume that badform is in the "Available Forms" window list of the project option-this window needs to be created and released manually. In the above Code, calling the refresh method of the badform window will lead to access violation.

If you enable "Stop on Delphi exceptions" in the debugger option window, the following information will pop up:

The message states that the eaccessviolationhas occurred. The eaccessviolation is the exception class for invalid memoryaccess errors.

This is the information you will see when designing the program. The next information box will appear, and the program will fail:

Access violation at address 0043f193

In module 'project1.exe'

Read of address 000000.

The first hexadecimal number 0043f193 is the address of the access violationcompilation code (project1.exe) during the runtime error. In IDE, select the menu item "Search | find error ...", Enter the error address (0043f193) in the dialog box and click "OK. Delphi will recompile your project file and display the line of code that has encountered a runtime error. Here is the line badform. Refresh.

The following lists most common causes of access violation errors in the Delphi environment. This list does not and cannot overwrite all possible accessviolation cases. Please send your accessviolation information on the Forum. You can try to solve this problem together-the actual case is generally much more obscure than the listed errors.

1. Call a nonexistent object

As mentioned above, the most reasonable reason for accessviolation is that an object is not created or released. To prevent this type of access violation, make sure that any object you access is created first. For example, if a table is located in an uncreated datamodule (removed from the auto-crete window), you may open the table in the oncreate event of the form.

In the following code, after calling a deleted object (B: tbitmap) event, an access violation occurs:

VaR B: tbitmap;

Begin

B: = tbitmap. Create;

Try

// Perform some operations on object B

Finally

B. Free;

End;

...

// Because B has been released, an accessviolation error will occur.

B. Canvas. textout (0, 0, 'This is an access violation ');

End;

2. nonexistent API Parameters

If you try to pass a non-existent parameter to the winapi function, an access violation error will occur. The best way to solve this access violation error is to check the win api help to see the parameter information and parameter type of this API function call. For example, always do not pass an invalid pointer to a buffer parameter.

3. Release Delphi

When an object has another object, let it delete it for you. By default, all forms (automatically created) belong to the application object. When an application ends, it releases the application object and releases all forms. For example, if you automatically create two forms (form1/unit1 and form2/unit2) at the beginning of the program, the following code will cause access 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 destroy the temporary exception object (e). When an exception is handled, the exception object is automatically released. If you manually release the exception object, the program will try to release it again, and the access violation error will occur:

Zero: = 0;

Try

Dummy: = 10/zero;

Except

On E: ezerodivide do

Messagedlg ('Do not use 0 as the divisor! ', Mterror, [mbok], 0 );

E. Free. // access violation error will appear

End;

5. Retrieve an empty string

A Null String does not have any data. That is to say, retrieving an empty string is equivalent to accessing a non-existent object, which will lead to an access violation error:

VaR S: string;

Begin

S: = '';

S [1]: = 'a ';

// Access violation error will appear

End;

6. Direct Pointer Reference

You must indirectly reference the pointer. Otherwise, you will change the pointer address and possibly destroy other storage units:

Procedure tform1.button1click (Sender: tobject );

VaR

P1: pointer;

P2: pointer;

Begin

Getmem (P1, 128 );

Getmem (P2, 128 );

// Access violation error caused by the next row

Move (P1, P2, 128 );

// The method for the next row is correct

Move (P1 ^, P2 ^, 128 );

Freemem (P1, 128 );

Freemem (P2, 128 );

End;

These are all my suggestions on access violation errors during runtime. I hope you can also give some comments on access violation errors in your program.

 

 

Solution

Right-click my computer ". Click properties ".

Click Advanced in system properties ".

Click set in performance ".

In performance options, click data protection ".

Click Add ". Select the program to run.

OK. That's simple.

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.