Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: access violation, tobject

  1. #1

    access violation, tobject

    Hi, I encountered asn access violation today, and it puzzles me. It may be simple but I can't figure it out.

    I have declared a Master object

    Code:
    Tmasterobject = object
    
      Sprite : array of TSprite;
    
    End;
    
    TIceblock = Object (Tmasterobject)
    
      // lots of procs, etc. here.
    
    End;
    
    TEnemy = Object (Tmasterobject)
    
      // lots of procs, etc. here.
    
    End;
    ----------

    Niow, when making a new Ticeblock by calling

    Code:
    New (Iceblock);  // pointer to iceblock if of course TIceblock object
    I get an access violation.

    When making the array in TMasterobject fixed (f.e. array [0..4] of TSprite, the violation is not present.

    How can it be?
    Marmin^.Style

  2. #2

    access violation, tobject

    ah nevermind, I'll find another solution. can be locked.
    Marmin^.Style

  3. #3

    access violation, tobject

    Try to specify the constructor (its the 2¬? parameter on new function)
    From brazil (:

    Pascal pownz!

  4. #4

    access violation, tobject

    Try to specify the constructor (its the 2¬? parameter on new function)
    From brazil (:

    Pascal pownz!

  5. #5

    access violation, tobject

    What is this New() function you got? I made simple application that couldn't even compile with that objects:

    [pascal]procedure TForm1.FormCreate(Sender: TObject);
    var sl: TStringList;
    begin
    new(sl);
    //... add something here if even these worked
    sl.Free;
    end;[/pascal]

    According to delphi help the New() is used to make pointer instance of a record. Furthermore, if you use dynamic arrays you must also use Setlength() to initialize it before use.

    Normally it would go:
    [pascal]sl:=TStringList.Create;[/pascal]

  6. #6

    access violation, tobject

    Exactly- that why it's faulty to use it. But, I don't want to use create/destroy (because Pascal won't let me create a totally new one 'on the fly' , but I do want to use the functionality of Objects. 'New' and Dispose, are old, but trusty, i like them,. And, I do not have to define them in advance.
    Marmin^.Style

  7. #7
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2

    access violation, tobject

    Marmin,

    I've been holding off replying because I'm trying to figure out what it is you want to achieve and I'm getting more confused with each post that appears.

    If all you want to do is create an instance of the class TIceBlock, then this will work:-

    [pascal]
    var
    newIceblock : TIceBlock
    begin
    newIceBlock:=TIceBlock.create;
    ...
    ...
    newIceBlock.free;
    end;
    [/pascal]

    However, I'm guessing the problem is that you are getting AV's when you try and access the Sprite array. This will be because its not initialised.

    So... if the array is the same size for all descendant classes, you can define it fully in the base class TMasterObject like this:-

    [pascal]
    type
    TMasterObject = class(TObject)
    public
    Sprite : array of TSprite;
    constructor create;
    destructor destroy; override;
    end;

    constructor TMasterObject.create;
    var
    loop : integer;
    begin
    inherited;

    setLength(Sprite,4);

    for loop:=low(Sprite) to high(Sprite) do
    begin
    Sprite[loop]:=TSprite.create;
    end;
    end;

    destructor TMasterObject.destroy;
    var
    loop : integer;
    begin
    for loop:=low(Sprite) to high(Sprite) do
    begin
    Sprite[loop].free;
    end;

    SetLength(Sprite,0);

    inherited;
    end;
    [/pascal]

    Now, if you want to have different numbers of entries in the Sprites array for descendant classes, then this is one approach (put the common code in the base class and override specific parts in the descendants):-

    [pascal]
    type
    TMasterObject = class(TObject)
    protected
    procedure initSprites(count:integer);
    procedure deinitSprites;
    public
    destructor Destroy; override;

    Sprite : array of TSprite;
    end;

    TIceBlock = class(TMasterObject)
    public
    constructor create;
    end;

    TEnemy = class(TMasterObject)
    public
    constructor create;
    end;

    procedure TMasterObject.destroy;
    begin
    try
    deinitSprites;
    except
    end;

    inherited;
    end;

    procedure TMasterObject.initSprites(count:integer);
    var
    loop : integer;
    begin
    setLength(Sprite,count);

    for loop:=low(Sprite) to high(Sprite) do
    begin
    Sprite[loop]:=TSprite.create;
    end;
    end;

    procedure TMasterObject.deinitSprites;
    var
    loop : integer;
    begin
    for loop:=low(Sprite) to high(Sprite) do
    begin
    Sprite[loop].free;
    end;

    SetLength(Sprite,0);
    end;

    constructor TIceBlock.create;
    begin
    inherited;

    initSprites(4);
    end;

    constructor TEnemy.create;
    begin
    inherited;

    initSprites(16);
    end;

    [/pascal]

    I have assumed TSprite is a class and I've initialised it. Now, you can create any number of instances of TIceBlock or TEnemy and they should have all their arrays configured and initialised with the objects.

    You cannot use NEW and DISPOSE for creating objects. Whilst I guess in theory, the memory will be allocated, there is a whole bunch of stuff that goes on behind the scenes when an object is created.

    I've just been pondering your statement 'And, I do not have to define them in advance' and I've just searched the forums and come across another post you made about this.

    The reason why you have to declare instances was I think explained, but what wasn't provided were any solutions. So... here's one...

    [pascal]
    var
    IceBlocks : TList;

    ...

    IceBlocks:=TList.create;
    IceBlocks.add(TIceBlock.create);

    ...

    TIceBlock(IceBlocks[0]).free;
    IceBlocks.delete(0);

    ...

    IceBlocks.free;
    [/pascal]

    Here, we define a TList. This will hold the references to our ice blocks. We create the list and add an ice block to it. At the end, we free up the ice block and remove it from the list and then finally free the list.

    TList holds references to the generic TObject. You need to use typecasting the TIceBlock(...) part to tell the compiler that the object is actually a TIceBlock. If you split your objects into seperate list (as I've implied), then life is simple. But, you can put all your objects in one list and then check the type using the 'is' operator.

    [pascal]
    if ObjectList[loop] is TIceBlock then
    begin
    ...
    end
    else
    begin
    if ObjectList[loop] is TEnemy then
    begin
    ...
    end;
    end;
    [/pascal]

    I hope this is helpful (if not to you, to others).
    :: AthenaOfDelphi :: My Blog :: My Software ::

  8. #8

    access violation, tobject

    Thanks for your post!
    Yes, I see that I can use TList for creating instances of Iceblock. In fact, this would be the best approach. But.. I have a slight reluctance to use OOP too much.. I just want to control what is under the hood. So I have to make the decision to not using it all together or only use it, and not mixing it.
    In fact, I do use New and Dispose an Object, and I get no errors when doing so. But i shouldn;t, obviously.
    Marmin^.Style

  9. #9
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2

    access violation, tobject

    Hi marmin,

    Please don't take offence, but I find this statement quite funny.

    Quote Originally Posted by marmin
    I have a slight reluctance to use OOP too much
    I find it funny because you are using it (seemingly quite extensively from your sample code). You are just using OOP ala Turbo Pascal. Its how I first learned the concepts.

    Is there a particular reason why you are reluctant to use classes (other than just wanting complete control)?
    :: AthenaOfDelphi :: My Blog :: My Software ::

  10. #10

    access violation, tobject

    Is there a particular reason why you are reluctant to use classes (other than just wanting complete control)?
    No. The answer is very simple.
    I want to understand and control the things I program. This is because I grew up using assembly language.
    If I'd use oop 100 % I would not make these posts and have these silly errors. It is obvious it is not suited, entirely, for me. I still use oop, of course. But not for a 100 percent

    Thanks for reacting.
    Marmin^.Style

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •