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 ba...@4tel.no on 2000/06/15 08:04:43 UTC

DOMString.transcode() and VC++ MFC CString

I have downloaded a xerces-c binary (1.1.0) for WinNT 4.0. The application
in which XML is to be used, is all written in VC++ 6.0 using the MFC class
'CString' extensively.

Getting a hold of DOMString elements is no problem, but the application
needs to convert these into the MFC class CString. The following method is a
first attempt:

//
// Converts a DOMString into CString.
//
CString DOMStringToCString(DOMString &stringToConvert)
{
	CString stringToReturn = stringToConvert.transcode();
	return stringToReturn;
}


Since transcode returns a char*, I assume that somehow deallocation of the
memory allocated by transcode() must be dealt with.

Does anyone have any experience doing this with respect to NOT creating
memory leaks? Will the above method take care of the problem, or will this
result in a leak?

Thanx!

Re: DOMString.transcode() and VC++ MFC CString

Posted by Dean Roddey <dr...@charmedquark.com>.
If the CString object has an 'adopt' mode, then tell it to adopt the
transcoded buffer. Else, after assigning to it, do delete [] on the returned
buffer. Or, if CString can take a Unicode string, which it probably can,
then you can just assign the raw buffer from the DOMString to the CString
and avoid the whole problem.

--------------------------
Dean Roddey
The CIDLib C++ Frameworks
Charmed Quark Software
droddey@charmedquark.com
http://www.charmedquark.com

"You young, and you gotcha health. Whatchoo wanna job fer?"


----- Original Message -----
From: <ba...@4tel.no>
To: <xe...@xml.apache.org>
Sent: Wednesday, June 14, 2000 11:04 PM
Subject: DOMString.transcode() and VC++ MFC CString


> I have downloaded a xerces-c binary (1.1.0) for WinNT 4.0. The application
> in which XML is to be used, is all written in VC++ 6.0 using the MFC class
> 'CString' extensively.
>
> Getting a hold of DOMString elements is no problem, but the application
> needs to convert these into the MFC class CString. The following method is
a
> first attempt:
>
> //
> // Converts a DOMString into CString.
> //
> CString DOMStringToCString(DOMString &stringToConvert)
> {
> CString stringToReturn = stringToConvert.transcode();
> return stringToReturn;
> }
>
>
> Since transcode returns a char*, I assume that somehow deallocation of the
> memory allocated by transcode() must be dealt with.
>
> Does anyone have any experience doing this with respect to NOT creating
> memory leaks? Will the above method take care of the problem, or will this
> result in a leak?
>
> Thanx!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
>


Re: DOMString.transcode() and VC++ MFC CString

Posted by Michael Mason <mg...@decisionsoft.com>.
bard.moseng@4tel.no wrote:

> Since transcode returns a char*, I assume that somehow deallocation of the
> memory allocated by transcode() must be dealt with.
> 
> Does anyone have any experience doing this with respect to NOT creating
> memory leaks? Will the above method take care of the problem, or will this
> result in a leak?

You'll get a leak, and as Dean's pointed out you are better off using
getRawBuffer() to get an array of XMLCh (equivalent to wchar_t under
Windows) and then using that for your CString. Transcoding to 8-bit char
will lose information.

If you really want to do that, you need to do

  char *buf = myDOMString.transcode();
  CString s = buf;
  delete [] buf;

But you'll get bitten by debug/non debug heaps if you do this and mix up
your debug flags (see other threads -- this has been discussed
extensively).

Mike.

-- 
Mike Mason, Software Engineer
XML Script Development Team                    Office: 44-1865-203192
http://www.xmlscript.org/                      Mobile: 44-7050-288923