[DUG] Invalid Pointer Operation

Rohit Gupta rohit at cfl.co.nz
Mon Aug 8 14:46:59 NZST 2005


Rats,  I knew that.  In the original app I am using fully typed 
variables etc... but all 1 based.

So I simplified it in the test app.  I would have found it earlier except 
the error happens at the freeing of the first element (not the creation 
of the last or freeing of the last).

I had visions of TObject having been changed  in D2005.  :-)

Thanks


From:           	"David O'Brien" <dave at iccs.co.nz>
To:             	<rohit at cfl.co.nz>,
       	"'NZ Borland Developers Group - Delphi List'" 
<delphi at ns3.123.co.nz>
Subject:        	RE: [DUG] Invalid Pointer Operation
Date sent:      	Mon, 8 Aug 2005 14:38:22 +1200

I thought dynamic arrays were zero based, so the loop would have 
to be from
0 to 9, not 1 to 10.

David O'Brien
(dave at iccs.co.nz)
027 442 1037
Computer sales and support services
Custom software design

-----Original Message-----
From: delphi-bounces at ns3.123.co.nz [mailto:delphi-
bounces at ns3.123.co.nz] On
Behalf Of Rohit Gupta
Sent: Monday, 8 August 2005 2:33 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: [DUG] Invalid Pointer Operation

I am very confused here.  Using D2005.  The following is on a form 
with two buttons - btnCreate and btnDestroy.  When Destroy is 
pressed I get an "Invalid Pointer Operation".

Seeing that this is the first time I have used open arrays (I detest 
the concept)  and setlength I must have done something wrong 
here.  But it escapes me.

The problem only shows up when I nest a TObject inside another.


unit Unit1;
{$d+,y+,l+,o-}

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, 
Controls, Forms,
  Dialogs, StdCtrls;

type
  TTest = class (TObject)
  private
    FMemory : array of integer;
  public
    constructor Create; reintroduce;
    destructor Destroy; override;
  end;

  TTest2 = class (TObject)
  private
    FTests : array of TTest;
  public
    constructor Create; reintroduce;
    destructor Destroy; override;
  end;

type
  TForm1 = class(TForm)
    btnCreate: TButton;
    btnDestroy: TButton;
    procedure btnDestroyClick(Sender: TObject);
    procedure btnCreateClick(Sender: TObject);
  private
  public
    Test : TTest2;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TTest }

constructor TTest.Create;
begin
  inherited Create;
  Setlength (FMemory,10);
end;

destructor TTest.Destroy;
begin
  FMemory := nil;
  inherited;
end;

{ TTest2 }

constructor TTest2.Create;
var
  I : Integer;
begin
  inherited Create;
  Setlength (FTests,10);
  for I := 1 to 10
  do FTests [I] := TTest.Create;
end;

destructor TTest2.Destroy;
var
  I : Integer;
begin
  for I := 1 to 10
  do FTests [I].Free;
  FTests := nil;
  inherited;
end;

procedure TForm1.btnCreateClick(Sender: TObject);
begin
  if not assigned (Test)
  then Test := TTest2.Create;
end;

procedure TForm1.btnDestroyClick(Sender: TObject);
begin
  Test.Free;
  Test := nil;
end;

end.
Regards

Rohit

====================================================
==================
CFL - Computer Fanatics Ltd.  21 Barry's Point Road, AKL, New 
Zealand
PH    (649) 489-2280 
FX    (649) 489-2290
email rohit at cfl.co.nz  or  r.gupta at xtra.co.nz
====================================================
==================


_______________________________________________
Delphi mailing list
Delphi at ns3.123.co.nz
http://ns3.123.co.nz/mailman/listinfo/delphi

Regards

Rohit

====================================================
==================
CFL - Computer Fanatics Ltd.  21 Barry's Point Road, AKL, New 
Zealand
PH    (649) 489-2280 
FX    (649) 489-2290
email rohit at cfl.co.nz  or  r.gupta at xtra.co.nz
====================================================
==================




More information about the Delphi mailing list