You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Dean Roddey <dr...@charmedquark.com> on 2000/08/26 07:22:20 UTC
Re: Properly managing memory returned by transcode()
Instead of"
> DOMString attrName( XMLString::transcode( "MyAttribute" ) );
>
I would think you could just do:
DOMString attrName("MyAttribute");
and it would internally transcode it into its own buffer efficiently. That
way you avoid the problem in this one. You are indeed leaking every
transcoded string you pass in in the way you are doing it.
> DOMString attrVal( el.getAttribute( attrName ) );
>
> Button *result = new Button( attrVal.transcode() );
>
Probably something like this would be better:
{
char* valPtr;
ArrayJanitor<char> janVal(valPtr = attrVal.transcode());
Button *result = new Button(valPtr);
}
which would insure that things would get cleaned up. Actually one thing that
needs to be done (and if I could ever figure out how to keep my commit
access working, I'd make these little changes), is to have ArrayJanitor and
Janitor both provide a getData() method, so it could be simplified to this:
{
ArrayJanitor<char> janVal(attrVal.transcode());
Button *result = new Button(janVal.getData());
}
--------------------------
Dean Roddey
The CIDLib C++ Frameworks
Charmed Quark Software
droddey@charmedquark.com
http://www.charmedquark.com
"It takes two buttocks to make friction"
- African Proverb
RE: Properly managing memory returned by transcode()
Posted by Jim Reitz <je...@home.com>.
>Actually one thing that
>needs to be done (and if I could ever figure out how to keep my commit
>access working, I'd make these little changes), is to have ArrayJanitor and
>Janitor both provide a getData() method, so it could be simplified to this:
>
>{
> ArrayJanitor<char> janVal(attrVal.transcode());
> Button *result = new Button(janVal.getData());
>}
I am currently using auto_ptr<> even though I knew it was calling the wrong
delete method. It works because the arrays are of primitive data types. If
the arrays were of real objects, then the destructors would not be called.
Obviously, this is not good programming practice since it would all break if
someone changed from chars to some C++ object.
The ArrayJanitor<> looks like the best solution. I didn't know it existed.
I was considering making my own auto_ptr class for arrays. I guess I won't
have to now. But you're right Dean, we do need a get() or getData() method.
Jim Reitz
jereitz@home.com
p.s., Michael mentioned the book "Effective C++". I highly recommend the
"More Effective C++" book as well. In it, the author talks about a number
of advanced C++ topics. It's good to read once front to back, and good for
a reference also.