Results 1 to 7 of 7

Thread: SetLength

  1. #1

    SetLength

    It is my understanding that SetLength creates an array that is managed by the garbage collector. My questions are :-

    If I create an array of double's with SetLength, I won't have to call any function to deallocate it, right?

    When is the array deallocated? After it goes out of scope? Or like the Java VM, which is indeterminate?

    Thanks.

  2. #2

    SetLength

    I think but can't be certain that you must deallocate the array when done by set the length to 0, but I can't be sure that it isn' t deallocated when the application shuts down when using the VCL. I remember, while using the API and a dynamic array that after a heavy afternoons compiling my machine filled up 256mb of memory because I hadn't been deallocating some hefty map array but that was without the VCL and they behave slightly differently when it come to destroying stuff. I wonder if AliMonster knows, he's down with all that.
    Isometric game development blog http://isoenginedev.blogspot.com/

  3. #3

    SetLength

    ...
    [size=10px][ Join us in #pgd on irc.freenode.net ] [ Sign the Petition for a Software Patent Free Europe ][/size]

  4. #4

    SetLength

    I'm pretty certain that dynamic arrays get disposed when they go out of scope - they're handled in the same way as strings, and you don't see many people worrying about deallocating strings.

    Dynamic arrays are ref-counted, so it seems very likely that they get squished at the end of scope, and very unlikely that they'll be garbage collected at a non-deterministic time.

    Of course, there are a couple of ways (maybe three) to manually kill the memory if you're feeling paranoid: set the array var to nil, pass it to the Finalize procedure, or SetLength to 0 (which I think does a deallocation, not that it matters too much). I've got into the habit of manually setting all my vars to nil/0, but that's just extra-safety for a larger project that I have on the backburner. It should be safe to leave 'em alone.

    The only thing to remember, and the thing that may have bitten you Crisp_N_Dry: clearing up the memory for the dynamic array won't clear anything dynamic that's stored inside the array's elements! (e.g. GetMem'd pointers or created classes). I think.. You'd want to clear the array elements by looping over them in the usual fashion - for i := Low(whatever) to High(Whatever) do FreeMem(Whatever[i]); // or equivalent squishing-stored-dynamic-mem.

    Here's a relevant link: http://www.info.umfcluj.ro/resurse/C...html#Heading22

    Anecdotal evidence, anyway - Charlie Calvert doesn't worry about deallocating the dynamic arrays, and I trust his judgement a lot. Also, two quotes from the help:

    Dynamic-array variables are implicitly pointers and are managed by the same reference-counting technique used for long strings
    ...and:

    This memory is allocated on the heap, but its management is entirely automatic and requires no user code
    If dynamic arrays use the same mechanism as long strings, and long strings are managed automatically, then [insert_conclusion_here].
    "All paid jobs absorb and degrade the mind."
    <br />-- Aristotle

  5. #5

    SetLength

    Quote Originally Posted by Useless Hacker
    Yes, you do have to dealocate it. To do this you would do:
    [pascal]SetLength(YourArray, 0);[/pascal]
    Whether you're using the VCL or not makes no difference, since the array is not a VCL component. Delphi does not manage dynamic arrays with garbage collection, it will only be deallocated when you do so, or possibly when the application closes.
    Delphi applies slightly different rules to some objects, doing work behind the scenes: Interfaces, dynamic arrays and strings. What's life without a few exceptions to the rule, eh? I might be wrong in my assertion that the stuff is cleaned up; maybe a quick test cradle is in order to provide a conclusive answer.
    "All paid jobs absorb and degrade the mind."
    <br />-- Aristotle

  6. #6

    SetLength

    Looks like I was talking crap as usual!

    From the Delphi help:
    Dynamic-array variables are implicitly pointers and are managed by the same reference-counting technique used for long strings. To deallocate a dynamic array, assign nil to a variable that references the array or pass the variable to Finalize; either of these methods disposes of the array, provided there are no other references to it. Dynamic arrays are automatically released when their reference-count drops to zero. Dynamic arrays of length 0 have the value nil. Do not apply the dereference operator (^) to a dynamic-array variable or pass it to the New or Dispose procedure.
    [size=10px][ Join us in #pgd on irc.freenode.net ] [ Sign the Petition for a Software Patent Free Europe ][/size]

  7. #7

    SetLength

    Thanks for the info guys. You are the best.

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
  •