Solved

Creating with TStrings/TStringList.... Access Violation during run

Posted on 2002-05-07
8
590 Views
Last Modified: 2012-05-04
Hello experts:

Here we go (D6 open and Klyix2 open):  256 MB Ram
(I compile and run with checked IO supervision):

I have new type:

typedefs.pas:

type
  TMyCat = class(TPersistent)
   public
     Remarks: TStrings;
     constructor Create;
     destructor Destroy;
   end;

constructor TMyCat.Create;
begin
    self.Remarks := TStringList.Create;    // <<< Access Vio!
end;

now I have a calling Code:

mainform.pas:

uses typdef;

in TMainFrom.FormCreate we can read:

MyCat.Create;

on stepping in this with F7 I get an Access Violation

Where is the bug?



0
Comment
Question by:Timmermann
  • 4
  • 4
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6994684
tstrings is a partial abstract class
(clear, delete are abstract methods -> means just not implemented only defined)

its a base class for deriving other

i recommend to use tstringlist instead of tstrings in your case

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6994733
:-( atleast it should work

hmm you didn't call inherited ... testing
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 6994744
hmmm ???

this works for me  (with d5, with d6/kylix i can't help)

unit object_test_u;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

type
 TMyCat = class(TPersistent)
  public
    Remarks: TStrings;
    constructor Create;
    destructor Destroy;
  end;




implementation

{$R *.DFM}

constructor TMyCat.Create;
begin
   self.Remarks := TStringList.Create;    // <<< NO Access Vio!
end;

destructor TMyCat.Destroy;
begin
   Remarks.free;    // <<< NO Access Vio!
end;

procedure TForm1.Button1Click(Sender: TObject);
var mycat : tmycat;
begin
  mycat := TMyCat.Create;
  mycat.Remarks.Add('Hello');
  showmessage(mycat.remarks[0]);
end;

end.

maybe the problem is on another point or
its version dependent

can't check with kylix or d6

meikl ;-)
0
 

Author Comment

by:Timmermann
ID: 6994786
> this works for me  (with d5, with d6/kylix i can't help)

Re: hmmm....

                   unit object_test_u;

                   interface

                   uses
                    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
                    typdefs,   // <<<<<
                    StdCtrls;
             

                   type
                    TForm1 = class(TForm)
                      Button1: TButton;
                      procedure Button1Click(Sender: TObject);
                    private
                      { Private declarations }
                    public
                      { Public declarations }
                    end;

                   var
                    Form1: TForm1;



put this in the unit "typdefs"

                   type
                   TMyCat = class(TPersistent)
                    public
                      Remarks: TStrings;
                      constructor Create;
                      destructor Destroy;
                    end;

                   implementation

                   constructor TMyCat.Create;
                   begin
                     self.Remarks := TStringList.Create;    // <<< NO Access Vio!
                   end;

                   destructor TMyCat.Destroy;
                   begin
                     Remarks.free;    // <<< NO Access Vio!
                   end;

                   procedure TForm1.Button1Click(Sender: TObject);
                   var mycat : tmycat;
                   begin
                    mycat := TMyCat.Create;
                    mycat.Remarks.Add('Hello');
                    showmessage(mycat.remarks[0]);
                   end;

                   end.

>maybe the problem is on another point or its version >dependent (can't check with kylix or d6)

                   meikl ;-)

Meikl: please try with a second unit again.

I will try your code in the meantime...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Timmermann
ID: 6994883
> this works for me  (with d5, with d6/kylix i can't help)

Re: hmmm....

                   unit object_test_u;

                   interface

                   uses
                    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
                    typdefs,   // <<<<<
                    StdCtrls;
             

                   type
                    TForm1 = class(TForm)
                      Button1: TButton;
                      procedure Button1Click(Sender: TObject);
                    private
                      { Private declarations }
                    public
                      { Public declarations }
                    end;

                   var
                    Form1: TForm1;



put this in the unit "typdefs"

                   type
                   TMyCat = class(TPersistent)
                    public
                      Remarks: TStrings;
                      constructor Create;
                      destructor Destroy;
                    end;

                   implementation

                   constructor TMyCat.Create;
                   begin
                     self.Remarks := TStringList.Create;    // <<< NO Access Vio!
                   end;

                   destructor TMyCat.Destroy;
                   begin
                     Remarks.free;    // <<< NO Access Vio!
                   end;

                   procedure TForm1.Button1Click(Sender: TObject);
                   var mycat : tmycat;
                   begin
                    mycat := TMyCat.Create;
                    mycat.Remarks.Add('Hello');
                    showmessage(mycat.remarks[0]);
                   end;

                   end.

>maybe the problem is on another point or its version >dependent (can't check with kylix or d6)

                   meikl ;-)

Meikl: please try with a second unit again.

I will try your code in the meantime...
0
 

Author Comment

by:Timmermann
ID: 6994884
argh... another reload sorry....
0
 

Author Comment

by:Timmermann
ID: 6994888
now it works...

I had a mistake in the Project uses order...

thanx!

BTW: For delpi6 and Kylix2 it works..

 constructor TMyCat.Create;
                   begin
                     inherited Create; // added....  (dont forget override destroy)

                     self.Remarks := TStringList.Create;    // <<< NO Access Vio!
                   end;
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6994973
glad you got it sorted :-)

thanks for the points,
it was a lucky help

meikl ;-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now