Probeer niet te debuggen (Don’t TRY to DEBUG)

Tijdens het werken aan een project kan je snel “DEBUG” code tussenvoegen om feedback of een resultaat te kunnen volgen in de commandolijn onderaan…   Working on code, I encoutered an interesting problem using DEBUG …

DEBUG "test"
toont “test” in het zwarte venster, maar evengoed kan je een variabele laten zien:
DEBUG sSqlStatement
of nog iets duidelijker voor jezelf:
DEBUG "SQL statement is:" & sSqlStatement

Een andere techniek is het gebruik van TRY (code) / IF ERROR (code).

Mijn fout was de combinatie van de twee:

TRY DEBUG resultOfProcedure()
IF ERROR
(code)
ELSE
(code)
ENDIF

In de IDE werk dat perfect, maar het probleem steekt de kop op als je het project compileert; de DEBUG code wordt niet meer uitgevoerd (tenzij je compileert met behoud van DEBUG code), en de applicatie crasht.

Het behouden van DEBUG code kan je aanzetten in de IDE bij het compileren naar een uitvoerbaar programma:

Menu: Project, Make, Executable (ctrl-alt-m)

Open daar onderaan > Options en kies “Keep debugging information in executable”

Door dit verschil kon ik de fout opsporen: door de TRY DEBUG combinaties te vervangen door TRY PRINT werkte de uitvoerbare versie wel na “make executable”.

English version:(Don’t TRY to DEBUG)



Problem using DEBUG

If you code:
DEBUG "test"
it shows “test” in the black window underneath the Gambas IDE, but of course it is more interesting to show the content of a variable
DEBUG sSqlStatement
maybe with some explanation on the same line (in case the variable is empty):
DEBUG "SQL statement is:" & sSqlStatement

Another thing is the use of TRY (code) / IF ERROR (code).

My error was the combination of both:

TRY DEBUG resultOfProcedure()
IF ERROR
(code)
ELSE
(code)
ENDIF

In the IDE it does what you want, but the problem pops up if you run the compiled project.
The DEBUG code is not included any more* and your program crashes.
* Except if you compile with option to keep debug info.

In the IDE, you find the option to keep the DEBUG code here:

Menu: Project, Make, Executable (ctrl-alt-m)

Open > Options and check

“Keep debugging information in executable”.

If you run the program afterwards, the “TRY-DEBUG” crash will not happen. If you replace TRY DEBUG by TRY PRINT, the crash will not happen any more in the compiled program.
Problem identified!

This entry was posted in Gamblog. Bookmark the permalink.