What are the advantages/disadvantages to embedding an object within an object, using
Embed SomeObject
when you need to use another object within your
object?

When should you consider using an Embedded object rather than using New to
create an instance of the object, or LNew to create a local instance within a
method?
Regards,
Rags
Posted on 2007-02-16 09:02:19 by rags
Hi Rags
This is an advanced question and mainly related to speed and memory optimization.

Embedding an object is like defining an object variable; which is allocated at the time the whole object is created. This means that you save an object allocation (MemAlloc + initialization) and deallocation (MemFree) call per embedded object. The second advantage is that you get less memory fragmentation, since you allocate the whole object package in one chunk.
Since the initialization for an embedded object is performed automatically, you only need to call the constructor and destructor.
The only think you have to consider is that size of each embedded object MUST be a multiple of 4 to ensure proper alignment.

Local objects are volatile, which means that they exist only for a short time. Since memory fragmentation is an issue for such type of objects, the stack is the best option to allocate them. Dialogs are a good example for volatile objects. They only exist in the OnCommand method when a command was send, like the about or help command.
Local objects, in contrast to embedded objects, are not initialized automatically. To achieve this, you have to use the LNew macro and then call the constructor and destructor methods as usual.

There is a third allocation possibility, which is using the object template. You can use it if you are sure you will have only ONE instance of it you application. Typically, the application object itself is such an object. To access the template object, simply put the ?@? symbol in front of the object name.

Best regards,

Biterider
Posted on 2007-02-17 03:53:16 by Biterider
Thanks Biterider, for the helpful explanation.
Regards,
  Rags
Posted on 2007-02-18 06:37:29 by rags