Solved

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

Posted on 2002-05-07
8
599 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
Industry Leaders: 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
HTML text in the body of an email (delphi code) 12 259
URL for downloading Google Chrome for Win XP 2 282
CheckListBox usage 3 97
Can Live bindings change TGrid Cell Colour ? 1 63
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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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