Using CodeView to Track Down an R6001: Null Pointer Assignment

ID Number: Q71252

1.x 2.x 3.00 3.10 3.11 3.14 | 2.x 3.00 3.10 3.11 3.12 3.50

MS-DOS | OS/2

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 and Later 2.35 and Earlier

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

g main g main

n16 n16

bp=DS:0#42 tpb DS:0 DS:42

g 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 [e.g., with malloc()] or that have not been

assigned to point to a specific data element (e.g., 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.00, 2.00, 2.10, 2.20, 2.30, and 2.35, the

procedure described above should be used for locating the source of an

R6001 error. With CodeView versions 3.00 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 66.

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: s_c s_quickc