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

Thread: Freeing items form dyn. array before deleting

  1. #1

    Freeing items form dyn. array before deleting

    For example, I have an dynamic array of pointers. Do I need to reset them all to Nil before setting the dynamic array to 0. My guess is : No. Is all memory freed when setting to 0.
    How is this exactly managed? Thanks.
    Marmin^.Style

  2. #2

    Freeing items form dyn. array before deleting

    The array of pointers is set to 0, not what they point to. Try it with objects and you'll notice memory bloat. If it's an array of objects, just do a for-loop that calls [i]FreeAndNil(a) and you'll be good to go.

    If they are pointers to records or other types, then AFAIK they are automatically freed -- but never objects.

  3. #3

    Freeing items form dyn. array before deleting

    It depends on the datatype as robert said. If your datatype is a complex type it might be handled by the RTTI handler. If it's an object, static array, interface, string, etc. it will be freed properly. If it's a class or pointer or something it's just a simple type and will not be handled
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  4. #4

    Freeing items form dyn. array before deleting

    Absolutely nothing is freed automatically - neither objects nor pointers.
    Maybe strings; but I can't tell for sure.

    So if you allocated memory for pointers in your array then you have to
    go through it and free them all (anyways your reserved heap is lost for
    further use ["memory leak"]).

    Same thing with an array of objects: Always free.
    If you don't want to do this you need to write or use a language with
    build-in GC.

    And: Don't mix up "free pointer" and "(re)set to nil". Completely different
    things.

  5. #5

    Freeing items form dyn. array before deleting

    Quote Originally Posted by waran
    Maybe strings; but I can't tell for sure.
    According to the book Delphi 6. Developer's Guide, strings are freed automatically.
    Quote Originally Posted by waran
    And: Don't mix up "free pointer" and "(re)set to nil". Completely different
    things.
    I agree. Just to explain these. "Freeing pointer" is freeing the memory used by the pointer, while "(re)setting pointer to nil" means setting the pointer to point on the beginning of the memory (which cannot be accessed).

    I've seen a lot of implementations, and from all I can tell, strings are freed automatically, records are freed automatically, objects are freed automatically, but classes ARE NOT freed automatically, so you have to manually take care of them.

    And btw, I think the best way to store classes is to use built-in types, like TObjectList from Contnrs unit.

  6. #6

    Freeing items form dyn. array before deleting

    ..objects are freed automatically, but classes ARE NOT freed automatically..
    Hmm.. i'm not sure how you would distinguish classes and objects in this matter. Do you mean classtypes? :?

    AFAIK, It's all as follows:

    Strings and records are automaticly freed.
    Pointers (pointing to data) and Objects are NOT automaticly freed.

    I don't know for sure about interfaces, but i guess their refcount wil decrement automaticly when they are freed/finalized.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  7. #7

    Freeing items form dyn. array before deleting

    Quote Originally Posted by chronozphere
    Hmm.. i'm not sure how you would distinguish classes and objects in this matter. Do you mean classtypes? :?
    Both Free Pascal and Delphi support the old Turbo Pascal object model that is defined using txx = object(tparent)
    end;

    objects are auto-freed (they are static objects, record like for allocation purposes) but not auto-destructed. They don't have an official destructor in the first place.

    AFAIK, It's all as follows:

    Strings and records are automaticly freed.
    Pointers (pointing to data) and Objects are NOT automaticly freed.

    I don't know for sure about interfaces, but i guess their refcount wil decrement automaticly when they are freed/finalized.
    That's the whole point. The dyn array contents are not freed, but finalized. For automated types ( e.g. interface/strings/dyn arrays/variants) this means decreasing ref count, and can _lead_ deallocate if necessary.

    This is exactly the same as for e.g. local variables.

  8. #8

    Freeing items form dyn. array before deleting

    Quote Originally Posted by chronozphere
    Hmm.. i'm not sure how you would distinguish classes and objects in this matter. Do you mean classtypes? :?
    Sorry, I didn't write it clearly. Here's what I meant:
    [pascal]
    type
    { .: TMyClass :. }
    TMyClass = class(TObject)
    Z: Single;
    end;

    { .: TMyObject :. }
    TMyObject = object
    X: String;
    end;

    var
    C: TMyClass;
    O: TMyObject;
    begin
    C := TMyClass.Create();
    C.Z := 3.14;
    O.X := 'Test';
    C.Free(); // must be called to avoid memory leaks!
    //X.Free(); // cannot be executed, because objects don't have destructors, they are static objects
    end;
    [/pascal]
    I hope it's all clear now.

  9. #9

    Freeing items form dyn. array before deleting

    huh?... i've never seen that before.

    Are objects like records/structs or more like classes? What are the differences? :?
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  10. #10

    Freeing items form dyn. array before deleting

    Well... lets split... i'll explain what i can do with delphi...

    If you want to have a array of objects, there is some options:

    1) Use TObjectList, this list frees the objects when they're removed or the list cleared/destroyed.
    2) Create your object as descendent of TCollectionItem, and use TCollection ... works like tobjectlist.
    3) Make your object a implementor of a interface, and use array of <thatinterface>. Interfaces ares freed when there is no more variables referencing it.

    If you use a pointer to a record, you will need to manually free the pointer, either if you use tlist or array.

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
  •