|
PowerBASIC Forums
![]() Frequently Asked Questions
![]() Win32: Detecting memory corruption problems with TRACE
|
| next newest topic | next oldest topic |
| Author | Topic: Win32: Detecting memory corruption problems with TRACE |
|
Lance Edmonds Member |
The cause of strange behavior and/or GPF errors (Page Faults) in a program can often be traced to memory corruption. A typical example of memory corruption might be the occasional failure of simple math expression, the content of a variable changing unexpectedly, right across the spectrum through to unpredictable crashes and/or DLL and API failures. The problem with memory corruption issues is that they can often be very difficult to diagnose, and very time consuming to track down... especially since they originates (most often) in completely unrelated sections of a program, and giving no hint as to the source of the problem at the point where the symptom becomes apparent. The most common source of such a problem can exist when a program writes to an array element that is beyond the boundaries of the array. Often this will cause a GPF, but if the memory address happens to be owned by the program, the effect will be one of writing data from (or reading data from) an area of memory that is actually being used by some other part of the program. The first step in tracking such problems is to use #DEBUG ERROR ON. This tells PowerBASIC to perform array bounds testing in the compiled code, and to flag an ERR=9 code if an array element access would breach an array boundary. However, unless the program actively tests for ERR or ERRCLEAR values, the run-time error report can be overlooked at the point the error is detected. What we need is an easy way to log run-time errors, so we can track down the cause quickly... and with PB/Win 7.0x and PB/CC 3.0x, the TRACE statement fills this need very nicely! First, just below the #COMPILE EXE metastatement (or at the very start of the source code if there is no #COMPILE metastatement present):
Then, at the very top of the PBMAIN/WINMAIN function, add the following lines:
Although it is not strictly necessary (as PowerBASIC will automatically close the log file when the app terminates), you can add two more statements just before PBMAIN/WINMAIN are terminated (either via an EXIT FUNCTION within the Function, or by the matching END FUNCTION statement):
Now compile and run the program right through to the end (or to the point where a suspicious crash or a suspected corruption occurs), and then close the app normally. Finally, open the new TRACE.TXT file in any file viewer than can cope with large text files, and search for "error"... every run-time error that occurred while the program was running will be logged in that file, along with Labels, Sub/Function names (and their parameters), etc, providing a useful context "map" that will show the trail of program execution up to the point where the error(s) are occurring. Naturally you will want to investigate any errors that are logged, but do pay special attention to any ERROR 9 reports, since these will definitely indicate an array bounds (or null-pointer) problem is occurring in the program. PS: Dont forget to switch off the trace file operation with #TOOLS OFF after you finish debugging the program code! ------------------ IP: Logged |
All times are EasternTime (US) | next newest topic | next oldest topic |
![]() |
|
Copyright © 1999-2007 PowerBASIC, Inc. All Rights Reserved.