[fpc-pascal] ansistrings, exceptions, pascal call stack and access violations

David Emerson dle3ab at angelbase.com
Tue Nov 2 23:41:04 CET 2004


Hi all,

We've got a sophisticated system which relies heavily on the power of ansistrings. I'm now introducing exception handling into the system, and am running into problems with the pascal call stack.

Without exception handling, I get a nice pascal stack, such as the following (produced by a test program, source below):


Runtime error 200 at $004010A8
  $004010A8  TEST_DIVISION_BY_ZERO,  line 13 of e:/c/test/t.pas
  $00401179  PLAY_WITH_STRINGS,  line 23 of e:/c/test/t.pas
  $004011B1  main,  line 28 of e:/c/test/t.pas


However, if I include the sysutils unit, and catch (and reraise) all exceptions, I get the following -- which not only lacks the complete call stack, but includes lots of junk:


An unhandled exception occurred at 0x004010A8 :
EDivByZero : Division by zero
  $004010A8  TEST_DIVISION_BY_ZERO,  line 13 of e:/c/test/t.pas
  $666F2033
An unhandled exception occurred at 0x004088FB :
EAccessViolation : Access violation
  $004088FB
  $00406278
  $005CFDA0
  $666F2033
An unhandled exception occurred at 0x004088FB :
EAccessViolation : Access violation
  $004088FB
  $00406278
  $005CFB30
  $00406278
  $005CFDA0
  $666F2033
An unhandled exception occurred at 0x004088FB :
EAccessViolation : Access violation
  $004088FB
  $00406278
  $005CF8C0
  $00406278
  $005CFB30
  $00406278
  $005CFDA0
  $666F2033
An unhandled exception occurred at 0x004088FB :
EAccessViolation : Access violation
  $004088FB
  $00406278
  $005CF650
  $00406278
  $005CF8C0
  $00406278
  $005CFB30
  $00406278
  $005CFDA0
  $666F2033



I noticed in my test program that if I change the ansistring_param to a normal string I get the call stack back:


An unhandled exception occurred at 0x00401078 :
EDivByZero : Division by zero
  $00401078  TEST_DIVISION_BY_ZERO,  line 13 of e:/c/test/t.pas
  $004010F3  PLAY_WITH_STRINGS,  line 23 of e:/c/test/t.pas
  $0040116B  main,  line 30 of e:/c/test/t.pas


(compared to the first: notice exception handling is now working, and the third line has changed from "main, line 28" to line 30 -- where raise is called, rather than where play_with_strings is called)


Here's the test source:


	{$mode objfpc}   // (line 1)

	uses sysutils;

	procedure do_nothing_with_an_ansistring (ansistring_param : string);
	   begin   end;

	procedure test_division_by_zero;
	   var
	      zero : integer;
	   begin
	      zero := 0;
	      write (5 div zero);
	   end;

	procedure play_with_strings;
	   var
	      short_string : string;
	   begin
	      short_string := 'HELLO';
	      if (lowercase(short_string)='hello')
	         then do_nothing_with_an_ansistring (short_string);
	      test_division_by_zero;
	   end;

	begin
	   try
	      play_with_strings;   // (line 28)
	   except
	      on exception do raise;   // (line 30)
	      end; // try
	end.



The only change I've made to fpc.cfg is uncommenting the -gl switch.

Perhaps there's another switch that I need to set in order to make ansistrings and exceptions work harmoniously?

Thanks,
David







More information about the fpc-pascal mailing list