Solved

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

Posted on 2002-05-07
8
593 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

21 Experts available now in Live!

Get 1:1 Help Now