Using CodeView to Diagnose R6001: Null Pointer Assignment

Last reviewed: July 17, 1997
Article ID: Q71252
2.20 3.00 3.11 3.14 4.00 4.01 4.05 4.10 | 2.20 3.00 3.11 3.12 3.50
MS-DOS                                  | OS/2
kbtool

The information in this article applies to:

  • Microsoft CodeView for MS-DOS, versions 2.2, 3.0, 3.11, 3.14, 4.0, 4.01, 4.05, and 4.1
  • Microsoft CodeView for OS/2, versions 2.2, 3.0, 3.11, 3.12, and 3.5

SUMMARY

When a program produces the error "R6001: null pointer assignment" at run time, the Microsoft CodeView Debugger may be used to track down the errant pointer. To locate the source of an R6001 error, bring the program up in CodeView and change focus to the command window. Next, type the following four commands into the command window (make sure you use the correct commands for the version of CodeView in use):

   With CodeView Versions              With CodeView Versions
   3.00 through 4.00                      2.35 and Earlier
   ----------------------              ----------------------

      g main                              g main
      n16                                 n16
      bp=DS:0x42                          tpb DS:0 DS:42
      g                                   g


   With CodeView Versions
   4.01 and Later
   ----------------------

      g main
      n16
      BP=DS:0 /R42
      g


The first command above executes to the beginning of the main() function. The second command, "n16", sets the radix for subsequent numeric entries to hexadecimal. The third command sets a breakpoint that will halt execution if any of the first 66 (0x42) bytes of the data segment change because this is the range of the null data segment. The fourth command begins execution of the program.

CodeView may run slowly because it must check the null segment for changes between every instruction. When the instruction that writes to the null segment is executed, CodeView will stop the program. When execution halts, the highlight will be on the line after the last statement to be executed. Thus, the line above the highlighted line will be the line where the bad pointer assignment occurred.

MORE INFORMATION

The location of the null segment can be observed in a link map. It starts at DS:0 and is 42H bytes long. The Microsoft copyright notice is written there at program startup and if this area is written to during the course of the program, the run-time error R6001 will be generated upon program termination.

The most common cause of this error is using a pointer that has not been initialized to point to a memory area. Pointers that have not had space allocated for them [for example, with malloc()] or that have not been assigned to point to a specific data element (for example, arrays or structures) are considered uninitialized. Writing to one of these pointers generally results in an overwrite of the null data segment.

With CodeView versions 1.0, 2.0, 2.1, 2.2, 2.3, and 2.35, the procedure described above should be used for locating the source of an R6001 error. With CodeView versions 3.0 and above, you may also use the following menu and keyboard commands to set the breakpoint for tracking down a null pointer assignment:

  1. Press the F8 or F10 key to step into main().

  2. Select Set Breakpoint from the Watch menu.

  3. Choose "Break When Expression Has Changed" from the Set Breakpoint dialog box.

  4. In the Expression field, enter DS:0.

  5. In the Length field, enter 0n66.

  6. Choose OK, or press ENTER.

  7. Press the F5 key to run the program.

As described above, the program will then break immediately after the errant pointer has written to the null segment.

Note that it may be necessary to clear any breakpoints on the null data segment and restart the program before entering the above commands in order for these procedures to work correctly.

In addition, any breakpoint on DS:0 may stop execution if DS changes. For example, if the above breakpoint is set and the program is restarted under CodeView, the program will break during the C start-up code [before reaching the main() function], since the C start-up sets DS to the default data segment. [This is also the reason you must step into main() before setting the breakpoint in the first place, otherwise you would not be setting the breakpoint on the correct segment.


Additional reference words: kbinf 3.00 4.00 4.10
KBCategory: kbtool
KBSubcategory: CvwIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.