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.
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
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.
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
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.
According to the book Delphi 6. Developer's Guide, strings are freed automatically.Originally Posted by waran
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).Originally Posted by waran
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.
Hmm.. i'm not sure how you would distinguish classes and objects in this matter. Do you mean classtypes? :?..objects are freed automatically, but classes ARE NOT freed automatically..
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.
Both Free Pascal and Delphi support the old Turbo Pascal object model that is defined using txx = object(tparent)Originally Posted by chronozphere
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.
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.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.
This is exactly the same as for e.g. local variables.
Sorry, I didn't write it clearly. Here's what I meant:Originally Posted by chronozphere
[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.
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.
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.
Bookmarks