Solved

AV when creating nested object when creating TStringList

Posted on 2004-10-21
226 Views
Last Modified: 2010-04-05
This is actually probably a simple problem, but i am even MORE simple ...

When creating a TTargetMap as declared and defined in the unit below, I receive an UNWANTED AV in:

 constructor TScript.Create;
....
  AV ==> ScriptList := TstringList.Create;



Source:

unit TMTest;

interface

uses
   Classes, Forms;

type
   TScript = class(TObject)
      private
      protected
      public
         ScriptList:   TStringList;
         ScriptImage:  Integer;
         ScriptStep:   Integer;
         constructor Create;
         destructor Destroy; override;
   end; // TScript

   TTargetMap = class(TObject)
      private
      protected
      public
         TargetList: TStringList;
         Parent:  TForm;
         Script:  TScript;
         constructor Create(ParentForm: TForm);
         destructor Destroy; override;
   end; // TTargetMap

implementation

constructor TScript.Create;
begin
   inherited Create;
   ScriptList := TstringList.Create;
   ScriptStep := -1;
   ScriptImage := -1;
end; // function TScript.Create

destructor TScript.Destroy;
begin
   inherited Destroy;
   ScriptList.Free;
end; // function TScript.Destroy

constructor TTargetMap.Create(ParentForm: TForm);
begin
   inherited Create;
   Parent := ParentForm;
   TargetList := TStringList.Create;
   Script.Create;
end; // function TTargetMap.Create

destructor TTargetMap.Destroy;
begin
   inherited Destroy;
   Script.Free;
end; // function TTargetMap.Destroy

end. // unit TMTest

0
Question by:mkudra
    3 Comments
     
    LVL 26

    Accepted Solution

    by:

    You need to use the class constructor, not the object

    constructor TTargetMap.Create(ParentForm: TForm);
    begin
       inherited Create;
       Parent := ParentForm;
       TargetList := TStringList.Create;
       Script.Create;  <----------------------- ERROR --
    end; // function TTargetMap.Create


    Should be

    constructor TTargetMap.Create(ParentForm: TForm);
    begin
       inherited Create;
       Parent := ParentForm;
       TargetList := TStringList.Create;
       Script:=TScript.Create;
    end; // function TTargetMap.Create

    --
    Regards,
    Russell

    0
     

    Author Comment

    by:mkudra
    Thanks, Russell!  Well worth the points to make forward progress!
    0
     
    LVL 26

    Expert Comment

    by:Russell Libby

    You are very welcome.

    Thanks for the points,
    Russell


    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    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

    Suggested Solutions

    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…
    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 Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    875 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

    13 Experts available now in Live!

    Get 1:1 Help Now