[xsde-users] memory management of variable-length elements?

Boris Kolpackov boris at codesynthesis.com
Fri Jun 24 04:48:15 EDT 2011


Hi Sebastian,

Sebastian Friebe <Sebastian.Friebe at xfab.com> writes:

> That was, what I was expecting. But the destructors are empty. 
> There is nothing done there.
> 
>   // XtdsTestBlock (variable-length)
>   //
>   class XtdsTestBlock ....
> 
>   XtdsTestBlock::
>   ~XtdsTestBlock ()
>   {
>   }

It is not the destructor of the class itself that will free the
memory. Rather, it is the destructor of the "holding" class that
will do the job. Here is an example. Suppose you have two classes,
Foo and Bar, both variable-length and Bar contains Foo (in the
schema terms, for example, the Bar complex type has the element
foo of type Foo). Here is the code that illustrates how this
works:

Bar* b = new Bar;

Foo* f = new Foo;
b->foo (f); // Now b is responsible for cleaning up f.

delete b; // Bar's destructor frees f.

If your environment allows you to use smart pointers, then the
better version of this code would be:

auto_ptr<Bar> b (new Bar);

Foo* f = new Foo;
b->foo (f); // Now b is responsible for cleaning up f.

// When b goes out of scope, Bar's destructor will free f.

To put all this in other words, the containing objects in the object
model assume ownership of the objects that they contain. The "eventually"
word in the documentation means that the contained objects will be
freed when their container is freed.

Boris



More information about the xsde-users mailing list