From: | Jesse McClusky |
Date: | 26 Aug 2000 at 22:14:31 |
Subject: | Re: AMIOPEN: Object allocated on stack - init and/or open |
> From: "Jean-Michel Forgeas" <forgeas@wanadoo.fr>
>
> Question 1:
> Since the _new method for objects is the allocator, is it (will it be)
correct
> to reserve the object size on the stack then call its init method to have
a
> fully fonctional instance ?
NO! Bad monkey!
Use /sys/kn/mem/allocdata for classes that are only accessed by the
current thread, and /sys/kn/mem/allocdef for classes that can be
accessed by other threads. Allocating on the stack as you describe
will almost guarantee that the class object gets overwritten with
arbitrary data.
General Rule: Anything you allocate on the stack MUST be freed
BEFORE you leave the function that allocates it.
> Question 2:
> In the red book it is said that for objects one has to call _new then
init, but
> for some objects one must call _new then open.
> Did I misunderstood so we have to always call the init method ?
What objects need _new then open???
Generally speaking, from what I've seen, the correct procedure is to
call _new then init to create it, and deinit then _delete to free it.
HOWEVER, many classes also have two additional tools called
open and close. Open calls _new, then init, returning the new class
instance. Close calls deinit, then _delete.
Keep in mind that I'm talking about tools here now, not methods.
Init and deinit are methods. _new, _delete, open, and close are
separate tool files. I make this distinction because some classes
also have *methods* called open and close, which are totally
unrelated to this discussion.
Jesse
Subscribe/Unsubscribe: open-request@amiga.com
Amiga FAQ: http://www.amiga.com/faq.html