Newsgroups : Borland : borland.public.delphi.rtl.win32 : 2005 Apr : Weird stack overflow exception

www.cryer.info
Managed Newsgroup Archive

Weird stack overflow exception

Subject:Weird stack overflow exception
Posted by:"Adrien Reboisson" (adrien-reboissonatastasedotcom@nospam.com)
Date:12 Apr 2005 09:14:13

Hi,

I'm asking here since I'm completely puzzled by a weird "Stack
Overflow" exception. I wrote a DLL and a small console app to
test it and this mysterious exception appears. Usually stack
overflows are caused by an infinite (or too big level of)
recursion, but here, I don't see anything.

I have 2 functions, the first returns a list of "TThUserData" and
the second a list of "TThServerData" (classical Delphi records).
Internally, the code is virtually the same (excepted the record's
type which obviously change) :

{ First function, OK }

procedure CheckServers;
var
  LCount: Integer;
  LServerList: array [0..254] of TThServerData;
  I: Integer;
begin
  LCount := thbGetServersList(@LServerList[0], Length(LServerList));
  WriteLn('Liste des serveurs trouvés : ');
  for I := 0 to Pred(LCount) do
    WriteLn(Format('  %s (%s:%d)', [LServerList[I].ServerName,
     LServerList[I].ServerHost, LServerList[I].ServerPort]));
end;

{ Function which raises the stack overflow exception }

procedure CheckUsers;
var
  LCount: Integer;
  LList: array [0..254] of TThUserData;
  I: Integer;
begin
  LCount := thbGetUsersList(@LList[0], Length(LList));
  WriteLn('Liste des utilisateurs trouvés : ');
  for I := 0 to Pred(LCount) do
    WriteLn(Format('  %s (%s)', [LList[I].Identity.UserName, LList[I].Identity.UserFullName]));
end;

Well, when I write :

    try
      CheckServers;
      CheckUsers; //<- exception raised here
    except
      on E: exception do
        writeln(e.message);
    end;

...the exception is raised just when entering in CheckUsers. More
precisely, when the "red cursor" (excuse my approximative English
!) reachs the first line (the "begin" keyword) of CheckUsers I
get the Stack Overflow error.

More interessting: when I move the call of "thbGetUsersList" in
the CheckServer (which works perfectly), then CheckUsers fails
and the same exception is raised on the "begin" keyword too. It's
very strange since the function seems never be reached. The
exception is raised only if the call is compiled.

thbGetUserslist is not very complex, it just translate a Delphi
dynamic array into an array which can be used from within a C
program :

function thbGetUsersList(PUserData: PThUserData; ACount: Cardinal): Integer;
{$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF}
var
  LList: TThUserDataArray;
  I: Cardinal;
  L: Integer;
begin
  LList := GetUsersList; //returns an array of TThUserData
  L := Length(LList);
  for I := 0 to Pred(L) do
    if I <= ACount then
      PThUserData(Longword(PUserData) + sizeof(TThUserData) * I)^ := LList[I];
  Result := L;
end;

Well... I don't see anything which can be wrong.
thbGetServersList adopt exactly the same way to proceed, none
exception is raised and I get exactly the good result.

Any hint appreciated ;-)

Regards,

A.R.

Replies:

www.cryer.info
Managed Newsgroup Archive