Search code examples
pascallazarus

Can not create TStream in Lazarus


It fails in this simple example to:

      procedure TForm1.Button1Click(Sender: TObject);
      var
        ts: TStream;
      begin
        ts.Create; //<---- fails here
        ts.Free;
      end;

With error:

Project project1 raised exception class 'External: SIGSEGV'.
At address 10000DB38

Solution

  • You are using the wrong code. It should be

     procedure TForm1.Button1Click(Sender: TObject);
          var
            ts: TStream;
          begin
            ts := TStream.Create; //  If Lazarus supports  creation of Stream instances.
            ts.Free;
          end;
    

    Until it is created, your variable ts simply contains junk from previous use of the stack. You have to call the class's constructor to allocate the actual object on the heap and point your ts variable at it.

    If Lazarus complains that it can't create an instance of TStream (it may treat it as an abstract class and I don't have Lazarus on this machine to check), try something like this instead:

     var
        ts: TMemoryStream;
      begin
        ts := TMemoryStream.Create; 
        ts.Free;
      end;
    

    Instead of TMemoryStream, you could use any other concrete TStream-descendant class.