Eliminate the program error in the germination

zhaozj2021-02-16  37

-------------------------------------------------- ------------------------------

This article contributes from Alex REST, translation: Bugfree / 9CBS platform: VC6, VC7, Windows 9X / NT / 2000 / XP

-------------------------------------------------- ------------------------------

I have contacted a lot of students with many students. I understand their mistakes. And, I also know my mistake. There is a relaxation that can make your program credibility. It is a trap for the application AssertionG macro. The usual C technology and VC / MFC special functions are described below.

1. Why must initialize the pointer to null if you have

Class a {public: a (); ~ a (); // some declaratives.private: sometype * m_ppointer; // some declarations.

A :: ~ a () {delete m_ppoint;}

If you give a pointer outside the constructor, you must always set it to null. For example:

A :: A (): m_ppointer (null) {}

- Why? - because:

1). If your program executed logic does not create an object for your pointer, the delete operation will perform unclear (according to C language standard). If you forget to initialize the ByPass Pointer, it will have a random value, delete this address Will be your program crash. 2). By using breakpoints and tracking, you can easily find a pointer without initialization. 3). You can use "if" to test a valid pointer, such as: if (m_ppointer) {M_ppointer-> dosomething ();} else {AFXMessageBox ("Unexpected Error # 1234. Send Me Letter Please to Aaa@bbb.com");} Maybe your program does not work correctly, but here it will not collapse. This It is very important. Imagine that you have already entered the two hours of text and there is no stock. Do you want the text editor to crash? Or give some warnings?

4). You can use Assert Macro to debug traps (Debug Trap)

2. For the trap set (traps)

Insert breakpoint is a good technology, but if the problem is in the long loop inside, it is not efficient. For example, after 10,000 cycles, some conditions have problems. In order to seize such problems, VC / MFC Programmers apply Assert Macro. ANSI macro is often applied. Which is not a problem, in the following example I use the MFC Assert Macro.

- How is it used? - This:

Assert (condition); example:

Assert (nwatertemperature> 0 && nwatertemperature <100); // Break if Wrong Valueassert (psomepointer); // Break if Null PointeraSsert (0); // BREAK IF Here

- How do it work? - This:

Assertion executes with the disclaimed information (program, module, the Assertion line) dialog box. The dialog has 3 buttons: "Break", "debug"), and "continued" ("ignore") "BREAK" end program, "Continue" ignores the assertion, the most useful "Repeat" button. Press it to open the source code editor in the assertion. Here you can test all the variable values ​​and understand where the problem What is it used? - Most of this:

In order to control the passing pointer: void Somefun (SomeType * ppoint) {assert (ppoint); // some instructions.

You can capture strange values ​​in "Switch" and "IF" operations, for example:

Switch (nrgbcolors) {copy nred: {// Some INSTRUCTIONS.} Break; Case NBlue: {// Some INSTRUE: {// Some Instructions.} Break; default: assert (0); //// We Should Have Never Come Here!}

IF (nwatertemp> = 0 && nwatertemp <50) {// some instructions.} else if (nwatertemp> = 50 && nwatertemp <= 100) {// Some Instructions.} else {assert (0); // We Should Have Never come here!}

Assert (nsomeValue> = minvalue and nsomevalue <= maxValue); assert (NOTHERVALUE! = 0);

Always Use this technique and you will be greatly surprised how offen such traps will work! This technology is always applied, you will be shocked by HOW OFTEN these traps.

3. Cute Assert Error Assert (m_mywnd.create ());

Vomitation! This is a terrible error! The program works normally in the debug version and does not work in the release. Remember: This is a macro that will be removed in the release. In this way your window will be forever Will not be created. If you use MFC, do this:

Verify (M_MyWnd.create ());

It performs m_mywnd.create in the debug version and executes m_mywnd.create in the release.

4. Object Verification and MFC Macro Assert_Valid Utilization Verify members are technically known to verify objects. If you have a clear object of verification, you can create and use the Verify class member. For example:

Class time {public: void set (int h, int m); bool verify () {return m_h> = 0 && m_h <24 and m_m> = 0 && m_m <60;} // Some Instructions.

Void Time :: Set (INT H, INT M) {m_H = H; M_M = m; assert (Verify ());}

Most of the MFC classes are subclasses of COBJECT. It is useful to verify the AssertValid virtual virtual function if a class implements this function, he is called by the Assert_Valid macro. For example: assert_valid (pView);

It checks the pointer to a CVIEWND object. If the object is invalid (empty pointer or the wrong window handle), the assertion will be executed.

5. MFC TRACE Macro

The appendix of the MFC macro without the Trace Macro will be uncomfortable. It is no problem with the value of the stream output variable in the main control desk mode. From the other hand, Windows programming the tracking variable is not a trivial task. In fact, when we track something, many windows can be turned on and off. It is not necessary to write the tracking output on a number of windows. A window is enough. Because this purpose, VC IDE applies "Output" window (View-Output " Menu Point) To debug output, you can use the Trace operator, which has the same format as the PrintF stdio function.

E.g:

"/ Nthis is a trace of int variable% d.", Nsomeint); trace ("/ nfunction onInitialupdate is starting);

Link: My other articles, Demo Project, and many interesting C links can be found here: http://www.brigsoft.com/edu

My software is here: http://www.brigsoft.com.

转载请注明原文地址:https://www.9cbs.com/read-24466.html

New Post(0)