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 Denis A Abramov <da...@columbia.edu> on 2002/06/07 22:57:45 UTC

cout << DOMString

I am a little unclear on how this is supposed to work. In fact
this crashes on me all the time.

ostream& operator<< (ostream& target, const DOMString& s)
{
    char *p = s.transcode();
    target << p;
    delete [] p;
    return target;
}

If the memory is allocated within a different heap (I am using the dll
supplied for windows) how can the user delete it? It specifically says in
the documentation that the user is supposed to delete the memory that is
allocated by transcode. Am I using this function wrong? If not then how can
I get a char* out of DOMString without memory leaks?


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: cout << DOMString

Posted by Gert van Spijker <ge...@ab-graph.com>.
> At 09:56 AM 10/06/2002 +0100, Gert van Spijker wrote:
> >Here is a code fragment, that works well on my system, from my MSVC code
> >that converts a nodes value to a MFC CString:
> >
> >char* pValue = XMLString::transcode( pNode->getNodeValue() );
> >CString Value = pValue;
> >delete pValue;
>
> This code fragment isn't correct, since it should be using delete[] to
free
> the memory. If it works, you're lucky and relying on a "quirk" of the
> platform you're using. ;)

You're right there of course. This is what you get after coding exculsively
for Bill's platform for many years.

Gert


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: cout << DOMString

Posted by Andrew Snare <AS...@allshare.nl>.
At 09:56 AM 10/06/2002 +0100, Gert van Spijker wrote:
>Here is a code fragment, that works well on my system, from my MSVC code
>that converts a nodes value to a MFC CString:
>
>char* pValue = XMLString::transcode( pNode->getNodeValue() );
>CString Value = pValue;
>delete pValue;

This code fragment isn't correct, since it should be using delete[] to free 
the memory. If it works, you're lucky and relying on a "quirk" of the 
platform you're using. ;)

On a somewhat semi-related note, this issue seems to crop up all the time. 
Has any consideration been given to an STL-allocator-style API for 
allocating and freeing memory returned by the transcode() family? I believe 
the kind of problems that we're seeing were part of the motivation for the 
STL allocator<> design.

Other ideas for mitigating these problems are introducing a proxy-type for 
the return result, so that the function signatures do not confuse as they 
currently appear to. A proxy-type would also have the advantage that it 
could, by default, release the resource when destroyed (auto_ptr<> style).

For what it's worth, we use an in-house equivalent to std::auto_ptr<> that 
uses delete[] instead of delete during destruction. That's been suitable 
for all our purposes so far, mainly since the results of transcode() are 
usually short-lived; the results are usually sent to I/O or shoved into a 
std::string.

  - Andrew



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: cout << DOMString

Posted by Gert van Spijker <ge...@ab-graph.com>.
Here is a code fragment, that works well on my system, from my MSVC code
that converts a nodes value to a MFC CString:

char* pValue = XMLString::transcode( pNode->getNodeValue() );
CString Value = pValue;
delete pValue;

Conversion is done by a XMLLCPTranscoder* which is initialised by a calling
XMLPlatformUtils::Initialize() from your calling app. Therefore the memory
is allocated by your app, not the dll.

I Hope this helps you

Gert van Spijker

> I am a little unclear on how this is supposed to work. In fact
> this crashes on me all the time.
>
> ostream& operator<< (ostream& target, const DOMString& s)
> {
>     char *p = s.transcode();
>     target << p;
>     delete [] p;
>     return target;
> }
>
> If the memory is allocated within a different heap (I am using the dll
> supplied for windows) how can the user delete it? It specifically says in
> the documentation that the user is supposed to delete the memory that is
> allocated by transcode. Am I using this function wrong? If not then how
can
> I get a char* out of DOMString without memory leaks?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org