Access violations is the most troublesome error that occurs when you are programming Windows?

Source: Internet
Author: User

Access violations<?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

Access violation

A. Introduction

B. AVs during design

A. Hardware reasons

B. Software reasons

C. Library errors

D. Upgrade C++builder

C. AVs during Operation

A. AVs occurs when program exits

B. Set your pointer to a null pointer !

C. Use the IDE to manage!

D. using cafree! in a form

E. Random AVs (non-exit)

D. Additional recommendations by users

Brief introduction

Access violation (AVs) is one of the most troublesome errors that occur when Windows is programming. Although it is difficult to use an article to explain all the possible causes of AVS, I will explain as much as I can about all the reasons I know. If you have an AVS solution not mentioned in this article , please email the author. Your experience will be added to this article.

There are two main forms of AVS occurring in C++builder. AVS during design and during run time . Let's start the discussion.

during the design period AVs

AVS is the easiest to capture during design time , but it's hard to really get rid of it by yourself. They are usually generated at compile time,when the builder starts and closes, or almost randomly. Let's discuss these known reasons first.

Hardware reasons

Some video cards, dual processor motherboards, and sound devices can cause AVs in C++buider . Why? Each board card in your machine is equipped with a device driver. There are compatibility issues with the manufacturer, theversion of Windows, and the version of C++builder you are using, which can cause AVS problems. The steps to resolve this situation are as follows:

o Always use the latest drivers for your system parts. If you use a driver that comes with windows, you should get the latest upgrade from the manufacturer.

O visit the newsgroups on borland.com and dejanews.com to find topics about your hardware device. Some graphics cards are known to have compatibility issues. You may need to replace the hardware. It's a good idea to use hardware from a well-known stable and mature vendor. Matrox is a good example of a graphics card.

o Check that there is a conflict between the devices you have installed is a necessary step.

O for some wacky graphics drivers , sometimes lowering the resolution helps stabilize.

o If you are using a dual processor, make sure that the two-processor step revision is the same, with exactly the same two chips.

Software reasons

Although windows is the most widely used operating system in Intel's system, its history is full of bugs and instability. There are many ways to help you have a more stable programming workstation. Follow these steps to help prevent this type of AVS from happening.

o Disable Active Desktop on a Windows workstation that has Internet Explorer (IE) 4.x or later . While this feature allows you to customize your desktop, it also causes many applications to create problems.

o Although Windows 9X is more popular,NT4 (NT5) provides the most stable environment in almost all Windows platforms. I want to emphasize that this should be the environment that C++builder programmers choose.

o Ensure that the latest NT system patches (SPx) are installed and that each release of the patch will make your NT system more stable.

o Reinstall the latest SPx after upgrading the main package . Some SPX-updated files are often overwritten when the driver is installed, including MS Office, IE, or even after C++builder installation. If SPX asks whether to replace the new version with the old version, answer No.

O Our experience is that when you find a newly installed system, and after a period of time, more and more problems begin to arise (including AVs), the reload system solves most of the problems and improves the overall performance of the system. This can be time-consuming, but absolutely effective.

Library errors

After you have installed the new libraries and components, you should track down and see if there are corrections to AVS during the design period . If a new AVs occurs , you may want to uninstall the most recently installed component. If Avs also disappears, seek the support of the supplier.

Also should pay attention to the Readme file and installation introduction. If you upgrade a library, this may require you to change your include directory settings, or even modify your make file so that the old and new versions do not conflict. If possible and the upgrade program allows, you should always uninstall the old version before upgrading.

Upgrade C++builder

I can assure you that I do not work for inprise, nor do I get any benefit. I can no longer emphasize the importance of using the new version of C++builder. The number of AVS, especially during the design period , was greatly reduced after I upgraded from CB3 to CB4 (now CB5). At the same time, performance is improved and more resources are available. If you want to run a long distance, it is worthwhile to upgrade.

during the run AVs

Although tracking is a nightmare, AVS during runtime can be resolved, and they are not usually bugs described in C + + Builder . Before I begin to help you solve the problem in your code, you must read and understand the AVS message during the design . The recommendations in this section only work for AVS during run time . Pay particular attention to whether your include directory contains the latest upgraded libraries, which is often the culprit. If none of this solves your problem, let's talk about programming, and we should be able to solve your problem and get you back to work.

occurs when the program exits AVs

If you have already seen your program exiting, the Avs dialog pops up , then congratulations on the frustration you now share as many C++builder programmers (including me). This type of AVS is the hardest to track. Because Debuger will usually introduce you to the unfathomable VCL interior or simply point to the post brackets of the project CPP file. But don't be afraid, the following things will help you through the worst part of your AV experience.

set your pointer to a null pointer !

One of the biggest causes of AV is trying to delete an illegal pointer. The reason for the occurrence may be using a pointer that is not initialized or trying to delete something two times. If you follow the instructions below, you can reduce the AVS in 50% to occur in your program. For all pointers, do the following:

1. After the pointer is declared, set it to NULL. If you do not do so, you should not immediately use the new action on this pointer . Otherwise, when the program exits and executes the delete action, the address of the pointer becomes meaningless. Then you get an AV.

2. after deleting a pointer, set it to NULL. Although the delete action has cleared the memory, it does not clear the pointer address. If you delete the pointer again later, it will cause an AV.

Remember to delete a NULL null pointers are not wrong and do not cause side effects.

Use IDE Management !

If you create a category (owned by) objects of other objects, let owner to delete this object. Are you confused? Please allow me to give an example to explain. If you dynamically create a panel object and new "en-us" owner is set to a form ( tpanel mypanel=new Tpanel (this). So when owner (form) is removed, he will attempt to delete your panel. If you have deleted the av. So, any time when you new an object and in the constructor (owner, do not manually delete this object, let Builder to do. If you have to do this, make sure you set it to null.

in the Form used in cafree!

If you can, do not manually delete the dynamically created form instance, and use Cafree in its Exit event . Although doing so does not necessarily resolve your access violation (AVs) issue, you can isolate this cause. Because av will occur in an event instead of when the program exits.


Random AVs ( non-exit )

Creating a list of program issues will not only take a lot of time, but the problems that you have encountered are probably not included. But there is still a small part of the most common av code issues:

o Attempt to access a location other than the length of the string. For example: The string is null ("") and an attempt is made to access the first character of the string mystr[1].

o reference a null pointer. Possible causes are: The pointer should be new without new, the pointer is deleted before it is accessed, the local and global pointers have the same name, a global or local pointer is new, but the other is accessed.

More suggestions from users

One way to prevent access to a null pointer problem is to always check all pointers before deciding to do anything with the pointer. There are many ways to achieve this. The best way to do this is to use assert, in fact if (myptr!=null) {...} The form is also good. It is worth pointing out that for multi-layer pointers (multi-level), theif method can also work well. This is thanks to the C language strongly supportsjumping at the first false value of an "if" predicate (a Boolean assignment short-circuit). such as:if (myptr!=null && myptr->itsptr!=null && myptr->itsptr->ptr2!=null) {...}

In the following example
int *parray = new Int[2];
Parray[0] = 1;
PARRAY[1] = 2;
PARRAY[2] = 2; Overflow!! The array only applied for 8 bytes ...
The AV dialog box (the one with the red X) does not pop up normally. Instead, a dialog box with no icon pops up, and the CPU window pops up. So, when you see a similar situation, you know there's an array overflow ....

4190

Access violations is the most troublesome error that occurs when you are programming Windows?

Related Article

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.