Microsoft KB Archive/51597

From BetaArchive Wiki

Article ID: 51597

Article Last Modified on 11/21/2006

This article was previously published under Q51597

SYMPTOMS

The PAINT function can corrupt variables that are passed to a SUB when the program is compiled using BC.EXE 4.00, 4.00b, or 4.50. The PAINT function works correctly in the QB.EXE environment, and if compiled using BC.EXE with the /X switch.

STATUS

Microsoft has confirmed this to be a bug in QuickBasic versions 4.00, 4.00b, and 4.50 and in Microsoft Basic Compiler versions 6.00 and 6.00b (buglist6.00 buglist6.00b) for MS-DOS. This problem was corrected in Microsoft Basic Compiler version 7.00 (fixlist7.00).

MORE INFORMATION

The following program demonstrates this problem. The values of x and y will be invalid following the PAINT(x, y) statement; however, the values of x and y are unaffected at the module level code following the call to "Sub1":

Compile with either: BC Paint; or BC Paint /O;
Link with:           Link Paint;
                

Code Example: PAINT.BAS

DECLARE SUB Sub1(x,y)
x = 300                'Initialize x
y = 100                'Initialize y
CALL Sub1(x,y)
PRINT x; y             'These values will be correct
END
SUB Sub1 (x, y)
SCREEN 12              'Any graphics mode
PRINT x; y             'These values will be correct
CIRCLE (x, y), 50
PAINT (x, y)            'Paint the circle
PRINT x; y             '** These values will contain garbage
END SUB
                

Specifying a definite type for x and y (such as INTEGER, LONG, SINGLE, or DOUBLE) does not have any effect on the output.

Four possible workarounds for the PAINT problem are as follows:

  1. Compile with BC /X.
  2. Copy x and y to variables that are local to the SUB.
  3. PAINT at the module level instead of at the SUB level.
  4. Make x and y SHARED or COMMON SHARED.

This problem was corrected in Microsoft Basic Compiler 7.00.

Example 2

The following program also demonstrates the problem in an EXE program, where PAINT corrupts the SUB argument used as a color value. This problem is caused by compiler optimizations, and can be worked around as follows:

  1. Disable optimizations by using the /X compiler option. -or-


  1. Disable optimizations by including the ON ERROR GOTO ... RESUME combination in the program source code. -or-


  1. To avoid the problem without disabling compiler optimizations, assign the SUB's argument to a local variable that is then passed to the PAINT statement's color parameter.
DECLARE SUB box (z%)
SCREEN 9                'Note: Also occurs in other graphics modes.
FOR count% = 1 TO 10    'Run through the test with ten colors.
CALL box(count%)        'Do the test.
NEXT count%
SLEEP                   'A chance to display the values.
END

SUB box (z%)
'This sub uses z% for positioning as well as the color variable.
 x% = 200 + z% * 20
 y% = 50 + z% * 20                       'Just a location for the box.
 LINE (x%, y%)-(x% + 10, y% + 10), z%, B 'The box to paint in.
 PRINT z%;                               'This value will be correct.
 PAINT (x% + 5, y% + 5), z%              'Do the paint.
 PRINT z%                     'This value will have been corrupted.
END SUB
                


Additional query words: QuickBas BasicCom B_BasicCom buglist4.00 buglist4.00b buglist4.50

Keywords: KB51597