VB "Out of Stack Space" with LoadPicture in Form_Paint Event

ID Number: Q72675

1.00

WINDOWS

Summary:

An "Out of stack space" error can occur when you use a LoadPicture

method within a Form_Paint event.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

The Visual Basic stack can be exhausted when the LoadPicture method is

executed within a [control/form]_Paint event. The LoadPicture method

generates a [control/form]_Paint event itself, and when performed

within a _Paint event, the program will repeat the cycle until the

stack is exhausted.

The following code example demonstrates that the Form_Paint event is a

recursive procedure when a LoadPicture method is included in the

_Paint event code.

After you add the code to your program, run the program and notice how

many times the message "Form_Paint Count :" is displayed within the

Immediate Window before you receive the "Out of stack space" error

message.

Sub Form_Paint ()

Static Count

Count = Count + 1

Debug.Print "Form_Paint Count : "; Count

Form1.picture = LoadPicture("c:\windows\chess.bmp")

End Sub

To remedy the situation, move the LoadPicture to another event

handler, such as the Form_Load event. Since these bitmaps are

automatically refreshed when needed, you don't have to maintain the

picture within a Paint event.

The Visual Basic stack is limited to 16K bytes, and cannot be changed.