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

Thomas Frenzel (TomSun) tftomsun at streamteam.de
Mon Nov 2 05:34:30 EST 2009


Hi Boris,

ah, i didnt know this detach option. I think this is already sufficient.

Regards,
Tom
  _____  

From: Boris Kolpackov [mailto:boris at codesynthesis.com]
To: Thomas Frenzel (TomSun) [mailto:tftomsun at streamteam.de]
Cc: xsde-users at codesynthesis.com
Sent: Mon, 02 Nov 2009 11:30:14 +0100
Subject: Re: Disable automatic deletion of char arrays.

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
  {
    ...
  
  private:
    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
  {
    void
    first (char*);
  
    char*
    first_detach ();
  
    void
    last (char*);
  
    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
  example:
  
  void
  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.
  
  Boris
    


More information about the xsde-users mailing list