Programming Techniques to be Avoided: GUI Programming



October 30, 1998

Most COBOL programmers are not used to thinking in terms of dynamically allocating memory and then de-allocating it when finished with it. However, with the advent of ACUCOBOL-GT, this concept has become more common. Although you may not realize it, when you use W$MENU to display a menu, W$BITMAP to load a bitmap or W$FONT to load a font, you are dynamically allocating memory.

Normally, when an ACUCOBOL-GT runtime terminates, all dynamically allocated memory is released. There are two situations where this is not true:

CALL PROGRAM "programname"  using ......
and
CHAIN "programname" using .......

Both terminate the current runtime but do NOT release dynamically allocated memory. This has been found to cause various problems including "Illegal Operation" errors and other unexpected results. For this reason, if you are using CALL PROGRAM or CHAIN, be sure to DESTROY all bitmaps, fonts (except those accepted from "standard object") and menus which have been created by that run unit. The exception here is any of these whose handles you may be passing in the USING section which you want to use in the called program.

A second area which can cause problems is the situation where a subroutine is called several times, and each time it allocates memory by one or more of the above means. If prior to the EXIT PROGRAM you have not DESTROYED these items, then when the subroutine is called again, you end up reallocating this memory. Note that even if the subroutine is CANCELLED, the memory for these items is NOT freed. This causes a "memory leak" where the program starts using up memory. Again, we have found that sometimes this can cause "Illegal Operation" errors or other unexpected results.

These are important considerations when programming with ACUCOBOL-GT. One way of determining whether or not a "memory leak" is occurring is to use the "U" option in debugger. Place a breakpoint just before the EXIT PROGRAM in a subroutine and each time you reach it enter "U". The amount of memory being used should be approximately the same each time. If you see it increasing then you have a memory leak someplace in that routine and the code should be examined for the above mentioned causes.

If you follow the above suggestions you could dramatically reduce the number of "Illegal Operations" and other system type error which tend to show up in Windows applications. If you have any questions about this please feel free to contact Technical Support.

Your Session will expire in 90 minutes
Notification will be shown in:
600 seconds