[comp.sys.mac.programmer] Bug in LSP V2.01?

schorn@inf.ethz.ch (Peter Schorn) (10/18/89)

I've run into a problem with Lighspeed Pascal V2.01 which appears to
be a compiler bug. I'm running LSP under Multifinder 6.1b7 on a Mac IIx in a
2MB partition. Running the following program with "Go" produces the
error messages "Your Application Zone is damaged. Proceed with caution"
and then "Nil dereference". These messages don't appear if the program
is run in single stop mode (in fact it works correctly then).

program test;
 type
  integerP=^integer;
  obj=object
    x:integerP;
    procedure init;
  end;

 procedure obj.init;
  procedure h(var y:integerP); begin new(y); y^:=19; end;
 begin h(x); end;

 var o: obj;
begin new(o); o.init; writeLn(o.x^); end.

Is this a compiler bug or am I doing something wrong?

Peter                                     schorn@inf.ethz.ch  or
                                          schorn@cs.unc.edu

siegel@endor.harvard.edu (Rich Siegel) (10/21/89)

In article <3935@ethz-inf.UUCP> schorn@inf.ethz.ch (Peter Schorn) writes:
>
> procedure obj.init;
>  procedure h(var y:integerP); begin new(y); y^:=19; end;
> begin h(x); end;
>
> var o: obj;
>begin new(o); o.init; writeLn(o.x^); end.
>
>Is this a compiler bug or am I doing something wrong?

I'd venture to guess that you're doing something wrong. Since objects
are allocated as handles (relocatable blocks), passing a field of an
object as a VAR parameter to a routine that may move memory (which
procedure H does, because New() calls NewPtr(), which moves memory)
is a distinctly unsafe thing to do. Before calling procedure H,
method O.Init should HLock(Handle(SELF)), and HUnlock(Handle(SELF))
after H returns.

(That's a simplification; actually, you'll want to HGetState(),
move SELF high, lock it, and then HSetState when you're done.)

You may wish to define and implement a TObject.Lock() and TObject.Unlock
for convenience.

>Peter                                     schorn@inf.ethz.ch  or
>                                          schorn@cs.unc.edu

		---Rich

~~~~~~~~~~~~~~~
 Rich Siegel
 Staff Software Developer
 Symantec Corporation, Language Products Group
 Internet: siegel@endor.harvard.edu
 UUCP: ..harvard!endor!siegel

"There is no personal problem which cannot be solved by sufficient
application of high explosives."

~~~~~~~~~~~~~~~