[xsde-users] Re: Disable automatic deletion of char arrays.

Boris Kolpackov boris at codesynthesis.com
Mon Nov 2 05:30:14 EST 2009

Hi Thomas,

Thomas Frenzel (TomSun) <tftomsun at streamteam.de> writes:

> Is there a possibility to turn of the automatic deletion of char arrays 
> and/or other aggregeted pointer based values when the generated class 
> will be deleted? I mean, mostly this is exactly what i want to do, but 
> sometimes i just want to pass a const char array to one of the generated 
> class instances without copying it before. In these cases i have to copy
> data even if its not necessary.

The problem with adding support for something like this is that it will
require an extra flag to indicate that the object should not be freed.
For example:

class person

  const char* first_;
  bool delete_first_;

  const char* last_;
  bool delete_last_;  

Since this feature will most likely be used only for a few object,
adding such a flag for every member will be too wasteful.

However, there is a way to achieve this that requires a bit of
extra work but does not incur any overhead: You can instruct the
XSD/e compiler to generate detach functions by specifying the 
--generate-detach option. The resulting object model will have
a detach function for every variable-length object, for example:

class person
  first (char*);

  first_detach ();

  last (char*);

  last_detach ();


The idea is to pass static strings to modifier functions that expect 
the dynamically allocated strings but then detach them before the object
model is deleted and the destructors try to free these static strings.
This works best if you know exactly which object will be static, for

free_object_model (person* p)
  p.detach_first ();
  p.detach_last ();

  delete p;

person* p = new person;

p->first ((char*) "John");
p->last ((char*) "Doe");

free_object_model (p);

It doesn't look pretty but if your goal is to avoid extra copying
(as opposed to just convenience), then it is possible.


More information about the xsde-users mailing list