Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: destroy and data erasing

  1. #1

    destroy and data erasing

    Is it true that everything inside the Create section is automatically erased when calling the Destructor? I made a bunch of linked lists, but they're all part of TStage, so all the garbage that 's connected with that is erased from mem.
    I create a Stage, destroy it, and then again create it with the same code.
    But this time I get wierd errors. Like the 'new (XX);' statement gives an Acccess violation, when the first time not. It drives me crazy.
    How can a 'New' statement give an acess violation?
    Marmin^.Style

  2. #2

    destroy and data erasing

    No, if you add new properties such as objects and arrays they aren't automatically destroyed. You must create a destructor, then after your own cleanup call the "inherited Destroy;".

  3. #3

    destroy and data erasing

    So I have to manually hand-clean ALL linked lists and other dynamic arrays that I made? Is the memory after the Destroy call not cleaned? That's awful.
    Marmin^.Style

  4. #4

    destroy and data erasing

    I manually erased the linked list.
    But when I erase the list and call TStage.Free , I get an access violation. If I don't erase it, there's no access violation.. How can it be?

    Are there detailed resources as to how the Free / Destroy method works? I don't understand what is freed and what not.
    Marmin^.Style

  5. #5
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    destroy and data erasing

    I think the most difficult problems of OOP is the proper tracking and freeing up of loose Objects in your programs.

    If you want to properly clean up all the allocated memory for objects that you don't use anymore, you have to destroy or Free everything. That means objects in objects, objects in arrays and anything that you basically had to [pascal]NewObject := TObject.Create;[/pascal]

    The good news is that you can use destructors inside destructors! So for your object that has other objects inside them you can create a new destructor function that will run the code nessissary to destroy all your other memory allocated bits inside.

    Example:
    [pascal]destructor TGameObject.Free;
    var
    i: Integer;
    begin
    // Free each little beasty created!
    for i := 0 to NumberOfGremlins - 1 do
    Gremlins[i].Free;
    end;[/pascal]

    This also stands very true for objects within objects within objects. And so on...
    Jason McMillen
    Pascal Game Development
    Co-Founder





  6. #6

    destroy and data erasing

    Doesn't the TObject implement nil checking of object before free'ing in it's Free procedure?

    I always overload Destroy instead of free as free calls the overloaded destroy.
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  7. #7
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    destroy and data erasing

    Using Destroy is supposed to be more dangerous that Free.

    iirc, Free is supposed to be able to check that the object is non nil before destroying the object where as destroy just does it regardless of it actually being allocated. In which case it would result in a memory error and your program would crash.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  8. #8

    destroy and data erasing

    He means your code should have read...
    [pascal]
    destructor TGameObject.Destroy;
    var
    i: Integer;
    begin
    // Free each little beasty created!
    for i := 0 to NumberOfGremlins - 1 do
    Gremlins[i].Free;
    inherited;
    end;[/pascal]

    Note it is very important that you call inherited in your destructor to ensure correct destruction of all parent classes.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  9. #9

    destroy and data erasing

    Anything created or allocated must be manually freed; objects, arrays, arrays of objects, and records (especially if they contain the previous) for good form.

    Code:
    type
      TThingy = class
      private
        fArray&#58; array of integer;
        procedure DoSomethingIrritating;
      public
        destructor Destroy; override;
        constructor Create;
      end;
    
    &#123;...&#125;
    
    destructor TThingy.Destroy;
    begin
      setlength&#40;fArray,0&#41;;
      inherited Destroy;
    end;
    
    constructor TThingy.Create;
    var i&#58; integer;
    begin
      setlength&#40;fArray,100&#41;;
      for i &#58;= 0 to 100 do
        fArray&#91;i&#93; &#58;= random&#40;1000000&#41;;
    end;
    
    procedure TThingy.DoSomethingIrritating;
    begin
      for i &#58;= 0 to 5 do
        if random&#40;&#41; > 0.5 then
          fArray&#91;high&#40;fArray&#41;&#93; &#58;= pwr&#40;random&#40;90&#41;+10,random&#40;6&#41;+1&#41;;
    
      if random&#40;&#41; > 0.3 then
        DoSomethingIrritating;
    end;
    Don't override MyObject.Free ever. Only override the destroy destructor and Delphi/FPC/Lazarus will do the rest. The point is, just override the descendant function and then call the parent one. Free isn't a function intended to be overridden, and was written to do a few things and then call the destroy event.

    In your code you should call free, but in designing an object you should override destroy. Any further questions?

    (This is pseudocode written in the browser, so don't blame me if it doesn't work. I haven't touched Delphi in 45 days.)

  10. #10

    destroy and data erasing

    (This is pseudocode written in the browser, so don't blame me if it doesn't work. I haven't touched Delphi in 45 days.)
    Just one important note:
    You must put override at the end of the destructor declaration, like this:
    TThingy = class
    private
    fArray: array of integer;
    procedure DoSomethingIrritating;
    public
    destructor Destroy; override;
    constructor Create;
    end;

Page 1 of 3 123 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
  •