PDA

View Full Version : Freeing items form dyn. array before deleting



marmin
13-05-2008, 02:02 PM
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.

Robert Kosek
13-05-2008, 04:03 PM
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 FreeAndNil(a[i]) 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.

JSoftware
13-05-2008, 05:21 PM
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

waran
13-05-2008, 05:54 PM
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.

Brainer
13-05-2008, 07:05 PM
Maybe strings; but I can't tell for sure.
According to the book Delphi 6. Developer's Guide, strings are freed automatically. :)


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. ;)

chronozphere
13-05-2008, 09:45 PM
..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. :)

marcov
14-05-2008, 08:25 AM
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.

Brainer
14-05-2008, 01:10 PM
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:

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;

I hope it's all clear now. :)

chronozphere
14-05-2008, 02:27 PM
huh?... i've never seen that before. :o

Are objects like records/structs or more like classes? What are the differences? :?

harrypitfall
14-05-2008, 11:12 PM
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.

Brainer
15-05-2008, 05:59 PM
huh?... i've never seen that before. :o
Are objects like records/structs or more like classes? What are the differences? :?
Objects are obsolete and shouldn't be used anymore. Well, the difference between records and objects is that objects can have methods, while records cannot. That's all. :)

marcov
23-05-2008, 09:34 AM
Objects are obsolete and shouldn't be used anymore. Well, the difference between records and objects is that objects can have methods, while records cannot. That's all. :)

In D2006+ records can contain methods can (.NETism needed for for..each), and in FPC objects are not formally deprecated.

However, except some really rare cases (e.g. emulation of statically allocated iterator objects is one), one should indeed use classes.

marmin
25-05-2008, 06:49 AM
huh?... i've never seen that before. :o
Are objects like records/structs or more like classes? What are the differences? :?
Objects are obsolete and shouldn't be used anymore. Well, the difference between records and objects is that objects can have methods, while records cannot. That's all. :)That's why I use Objects. They must not be removed from Pascal, they have to be there because of backwards compat.

Brainer
25-05-2008, 07:11 AM
In D2006+ records can contain methods can (.NETism needed for for..each)
Hmm, I'm not that sure. :? I think they can only contain methods related to operator overriding. Correct me if I'm wrong.

arthurprs
25-05-2008, 07:31 AM
In D2006+ records can contain methods can (.NETism needed for for..each)
Hmm, I'm not that sure. :? I think they can only contain methods related to operator overriding. Correct me if I'm wrong.

no they may have functions, constructors, procedures, but not destructors

Brainer
25-05-2008, 10:38 AM
no they may have functions, constructors, procedures, but not destructors
I don't get it then. :S Why does Borland say objects are obsolete, while they allowed records to have its own functions and even a constructor?

User137
25-05-2008, 11:06 AM
Is there any reason i should use records if objects do same and can have procedures? Is object packed?

arthurprs
25-05-2008, 03:33 PM
no they may have functions, constructors, procedures, but not destructors
I don't get it then. :S Why does Borland say objects are obsolete, while they allowed records to have its own functions and even a constructor?

don't know =|

JSoftware
25-05-2008, 06:30 PM
Records with methods are nice in the sense that no advanced memory management is required to chug it around at random places in ram. Objects contain certain type information to my knowledge. The compiler will also place helper functions for objects to allocate and free objects and such

I can only wait for FPC to add similar functionality