Solved

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

Posted on 2002-05-07
8
596 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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
 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

773 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