views:

195

answers:

3

in this example

procedure foobar;
var tab:array of integer;
begin
  setlength(tab,10);
end;

is the array destroyed or the memory is leaking?

+6  A: 

The memory is freed. (That is, no memory leak!)

Andreas Rejbrand
Elements are also freed if those are managed by the compiler too (dyn arrays, strings, intf, records with such types and so on).
alex
For "tab:array of Integer", everything is freed. For "tab:array of TObject", or any other class, you must free the Objects yourself.
Warren P
True, @Warren, but that has nothing to do with the array. The same advice applies to an ordinary scalar. Objects need to be freed; integers don't.
Rob Kennedy
+1  A: 

The array is automatically freed, but I've seen obscure cases where it isn't for some reason. I solved it by setting the array to nil.

Alan Clark
There are only two reasons why it wouldn't be freed. Either you're doing something scary with pointers that messes up the reference counting, or the array is owned by an object or record which is also leaking.
Mason Wheeler
I know, somehow there was something else causing it not to auto-free. I wish I still had the example to prove it! But I don't.
Alan Clark
That would be a bug in the compiler's code-gen. Or maybe it was a threadvar? The help clearly state that managed type used as threadvars won't free themselves automatically and you need to do so manually.
Ken Bourassa
It wasn't a threadvar, I've never used them. It was just an array of doubles that was a field of a class, FastMM revealed the leak. It was in Delphi 7.
Alan Clark
A: 

Hit the F1 button.

Alexander
Based on the amount of time it takes the hateful MS help system to appear on my machine I'm guessing it's quicker to ask on SO :-) [And, yes, I know it's slightly quicker in D2010]
shunty
This is not an answer - it's totally useless. Next time, make it a comment, please.
Ken White
I think that you're wrong. May be it doesn't answer on that specific question, but it's surely more helpful than answer by Andreas Rejbrand. May be it'll teach Azarien to read help or google before asking next time! (or so I hope) Isn't that great? :D Seriosly, it's more useful to show people, how to get info than answer on each question about every tiny problem. I really don't understand, why you need to ask such questions, wait for (obvious) answer, if you can just RTFM/google/experiment and get answer in 1-2 minutes.
Alexander
`[DCC Error] SOAnswer.txt(1): E2010 Incompatible types: 'Boolean' and 'procedure, untyped pointer or untyped parameter'`
Andreas Rejbrand