Title: Message
As Lei pointed out, the XercesDOMParser destructor will destroy its DOMDocument by default. You can prevent this by adopting the document - call adoptDocument() instead of getDocument(). Once you do this, you're responsible for calling release() to destroy the document once you're through with it.
 
Lei also raises a good point about character types. Technically, you're guaranteed to be able to cast a CString to const char * only when using single-byte characters. If you compile with Unicode support, a CString will be composed of wide characters, and no cast to const char * will be available. In its place is a cast to const wchar_t *. You can make your code work regardless of how it's compiled by:
- Using CStringA instead of CString. CStringA always uses single-byte characters, so the const char * cast is always available.
- Using TCHAR rather than char. A TCHAR is always the size of a CString character, so a cast from CString to const TCHAR * is always available. If you go this route, you'll want to use the generic-text routines (such as _tcslen) rather than locking into the ANSI versions (such as strlen) and declare string constants as _T("constant") rather than "constant". This approach takes slightly more work but is more forward-looking (since it allows for the possibility of processing non-ANSI text).
 
Of course, this assumes that there is some reason you have to use CString (which comes with some overhead). As Lei pointed out, a character pointer would suffice in the example code you sent. It's still worth considering TCHAR instead of char.
-----Original Message-----
From: Jimmy Yu [mailto:[EMAIL PROTECTED]
Sent: Tuesday, February 03, 2004 5:12 PM
To: [EMAIL PROTECTED]
Subject: Re: MemBufInputSource with CString

Jesse,
 
Thanks for the const char* advice, that takes care of my crash.
to fix the leak, would you recommend I change my original:
 
DOMDocument* ConstraintChecker::ParseDoc(const char *mem)
{
   MemBufInputSource *memBufIS = new MemBufInputSource((const XMLByte*)mem, strlen(mem), gMemBufId, false);
   XercesDOMParser *domParser  = new XercesDOMParser;
 
   domParser->parse(*memBufIS);
   return(domParser->getDocument());
}
into:
 
DOMDocument* ConstraintChecker::ParseDoc(const char *mem)
{
   MemBufInputSource *memBufIS = new MemBufInputSource((const XMLByte*)mem, strlen(mem), gMemBufId, false);
   XercesDOMParser *domParser  = new XercesDOMParser;
   domParser->parse(*memBufIS);
   DOMDocument *doc = domParser->getDocument();
 
   delete domParser;
   delete memBufIS;
 
   return(doc);
}
Sincerely
-- Jimmy
________________________________________________
The meek shall inherit the earth, but not the mineral rights.
- J. Paul Getty

Reply via email to