PDA

View Full Version : An unique OOP question



Robert Kosek
16-09-2006, 04:55 PM
I have been programming alot in the last few days with Asphyre, rewriting an old formerly vaporware project of mine. In some oo coding I have one pressing question: Can an object free itself?

Psuedocode Example:

if lifespan <= 0 then begin
List.Remove&#40;Self&#41;;
Self.Free;
end;


So for instance within the Update call the object could free itself if need be, or would I need to make a function to run through and clean out all the dead objects? Just wondering so I don't, you know, completely mess everything up. :D

technomage
16-09-2006, 05:10 PM
the problem with objects freeing themselves is referencing. If any other object has a reference to that object , be it a list or another object how will you tell it that it has freed? Your worst case would be a rather funny access violation when trying to access the freed object.

You could use an event to trigger a nofification that the object has been freed , my preference would be to set a flag on the object and clean up elsewhere. For example one of my projects the main game engine cleans up the game object list removing "dead" objects.

I think you'll find its "good practice" for objects to be free's by the owner.

Robert Kosek
16-09-2006, 05:48 PM
Well, it was a theory. :) I'll just write a junk cleaning function, since the flag is already present. I was curious though, yet nothing else referenced the object.

dmantione
16-09-2006, 06:42 PM
Yes, an object can destroy itself. It is seldom, but it can be usefull in certain cases.

WILL
17-09-2006, 12:14 AM
Ah, I know exactly what Robert is thinking. :)

I had been trying this with my first game that I did using Object Pascal but never got it to work without problems. Of course I was rather new to OOP at the time, but I've never reapproached it since then.

Simply out of curiosity though, I'd love to see how it would be done 'properly' even if thats not the way one should term it. :thinking:

Robert Kosek
17-09-2006, 12:57 AM
Well, someone at Afterwarp suggested implementing the "Factory Method Pattern (http://en.wikipedia.org/wiki/Factory_method_pattern)". Don't know if this would work, but I'm moving along without it.

For the latest post on what I'm doing, as well as a post with Turbo Delphi units (MPL licensed) for 2D Vectors and Colors (with operator overloading) you can hit my blog (http://thewickedflea.com/blog/). I'll make a project announcement a bit later as I near getting something viable for my game. :D

tanffn
17-09-2006, 07:25 AM
Sorry, I don’t see the problem here.. I assume List is the same instance as of its parent.
I think you can also call freeandnill(self) (as you can’t self:= nil).

The only problem may accrue if it’s multi-thread and several processes are using that same intense (then you can simply add a lock to make it thread-safe).

marmin
05-10-2006, 07:20 PM
Ask the free pascal team.. they must know the answer.

Fran
06-10-2006, 04:40 PM
In fpc and delphi, it's not safe to have an object free itself. That's because of some code optimisation that's happening under the hood.

However in fpc it's safe to do so with tobjects and in delphi 2006 with advanced records.

Note that delphi's tobjects do not work, don't even attempt to use them, they are totally buggy (i still don't get why they didnt fix the tobjects rather than introduce the new advanced records, it's almost the same thing). And fpc doesnt support advanced records so... this little trick will be hard to implement if you're trying to use cross compiler code.

Just make 101% sure you do not access any of the tobject's(record's) data once it's freed, not even methods.

If in doubt, check the dissassembled form of your software (ctrl+alt+c in delphi) while debugging.

It can be a useful coding practice, i know i use it once in a while! Good luck!

TheDon
06-10-2006, 08:23 PM
Can you explain the TObject's problem in detail and what you mean with "don't even attempt to use it"? Instancing a TObject directly is not usefull, you cannot do much with it and everything else is derived from TObject.

TIA

Fran
07-10-2006, 12:54 AM
Sorry, i didnt put it clearly. I meant:

A

type
abc=class
procedure zzz;
...
end;

B

type
abc=object
procedure zzz;
...
end;

C

type
abc=record
procedure zzz;
...
end;

You have to debugger dissassemble A to make sure it works out ok if it frees itself.

You can have B free itself in FPC, but not in delphi, since this type is buggy in delphi.

You can have C free itself in delphi 2006+turbo delphi (turbo delphi=delphi2006), but not FPC (i believe, havent tried it in a while).

In all cases, you also have to make sure to not access any fields/methods of your stucture once it has freed itself.

Hope this clears the confusion :)