Solved

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

Posted on 2002-05-07
8
603 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month7 days, 7 hours left to enroll

622 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