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 "Stratton, Jason" <js...@telegea.com> on 2000/09/28 20:50:08 UTC
Xerces-C in a COM object for ASP
I have an interesting/frustrating situation occurring.
I have created a COM object which is used by an ASP. In a method of the COM
object a XML transaction occurs and I need to parse the response. I have
chosen to use Xerces-C for this task. I got it set up and working fine. But
the second time the method is called, it crashes when instantiating the
Xerces-C parser. I thought this was odd, but while playing with it for a
while, I resolved it by using a static instance of the parser. (Effectively
creating it only once.) This works fine. I can call the method multiple
times without a problem. It feels like a hack, but I can live with it.
Now I needed to create another, similar COM object to be used in a different
ASP. For parsing the XML, I use exactly the same code, the same C++ file in
fact. So the same code is compiled and statically linked into two different
COM objects. Again, this appears to work just fine ... until I attempt to
instantiate both COM objects at the same time.
I visit the first ASP and make the transaction successfully, then visit the
second ASP and when I try to kick off the transaction, it crashes when
attempting to instantiate the Xerces-C parser. The same thing happens when
visiting the pages in the opposite order, second ASP then the first will
crash.
It appears that at most only one Xerces-C parser may be created at any point
in time. Is this correct? Is there something important that I am missing?
The code I have written for using Xerces-C is below. Any help will be
greatly appreciated.
Thank you.
Jason Stratton
jstratton@telegea.com
Here is my code. I have cut out & commented the irrelevant parts. I have
also noted where the crashing occurs.
HRESULT TGRTITransaction::ParseXMLResponse(BSTR bstrOutBuf /*other param*/)
{
// <snip> validating param
// The response is coming back as UTF-8, but as Unicode.
// So we must convert the Unicode to single byte char string.
// (Or we could find the UTF-8 in the string and change it to
"UTF-16")
int isbXMLLength = wcslen(bstrOutBuf)*sizeof(wchar_t);
char *psbXML = new char[isbXMLLength];
if (!psbXML)
;// report error & return
BSTR_TO_STRING(bstrOutBuf, psbXML, isbXMLLength);
try
{
XMLPlatformUtils::Initialize();
}
catch (const XMLException& e)
{
// report error
return E_FAIL;
}
static DOMParser parser; // if not static, will crash second
time thru - I don't know why
parser.setDoValidation(false);
parser.setDoNamespaces(false);
TGXMLErrorHandler errorHandler(/*param*/);
parser.setErrorHandler(&errorHandler);
parser.setExpandEntityReferences(true);
parser.setToCreateXMLDeclTypeNode(true);
static const char* BufId = "LiveOakXMLResponse";
MemBufInputSource memBufIS((const XMLByte*)psbXML, isbXMLLength,
BufId, false);
try
{
parser.parse(memBufIS);
}
catch (const XMLException& e)
{
// report error
delete[] psbXML;
XMLPlatformUtils::Terminate();
return E_FAIL;
}
DOM_Document doc = parser.getDocument();
hr = TraverseDOM(doc /*other param*/);
delete[] psbXML;
XMLPlatformUtils::Terminate();
return hr;
}
Re: Xerces-C in a COM object for ASP
Posted by Simon Fell <so...@zaks.demon.co.uk>.
you should call XMLPlatformUtils::Initialize & Terminate once per
process, not per call. Move the XMLPlatformUtils::Initialize /
Terminate to ObjectMain (I'm assuming you're using ATL), and then go
back to creating a parser per request.
Cheers
Simon
On Thu, 28 Sep 2000 14:50:08 -0400, in soap you wrote:
>I have an interesting/frustrating situation occurring.
>
>I have created a COM object which is used by an ASP. In a method of the COM
>object a XML transaction occurs and I need to parse the response. I have
>chosen to use Xerces-C for this task. I got it set up and working fine. But
>the second time the method is called, it crashes when instantiating the
>Xerces-C parser. I thought this was odd, but while playing with it for a
>while, I resolved it by using a static instance of the parser. (Effectively
>creating it only once.) This works fine. I can call the method multiple
>times without a problem. It feels like a hack, but I can live with it.
>
>Now I needed to create another, similar COM object to be used in a different
>ASP. For parsing the XML, I use exactly the same code, the same C++ file in
>fact. So the same code is compiled and statically linked into two different
>COM objects. Again, this appears to work just fine ... until I attempt to
>instantiate both COM objects at the same time.
>
>I visit the first ASP and make the transaction successfully, then visit the
>second ASP and when I try to kick off the transaction, it crashes when
>attempting to instantiate the Xerces-C parser. The same thing happens when
>visiting the pages in the opposite order, second ASP then the first will
>crash.
>
>It appears that at most only one Xerces-C parser may be created at any point
>in time. Is this correct? Is there something important that I am missing?
>
>The code I have written for using Xerces-C is below. Any help will be
>greatly appreciated.
>
>Thank you.
>Jason Stratton
>jstratton@telegea.com
>
>
>Here is my code. I have cut out & commented the irrelevant parts. I have
>also noted where the crashing occurs.
>
>HRESULT TGRTITransaction::ParseXMLResponse(BSTR bstrOutBuf /*other param*/)
>{
> // <snip> validating param
>
> // The response is coming back as UTF-8, but as Unicode.
> // So we must convert the Unicode to single byte char string.
> // (Or we could find the UTF-8 in the string and change it to
>"UTF-16")
> int isbXMLLength = wcslen(bstrOutBuf)*sizeof(wchar_t);
> char *psbXML = new char[isbXMLLength];
> if (!psbXML)
> ;// report error & return
> BSTR_TO_STRING(bstrOutBuf, psbXML, isbXMLLength);
>
> try
> {
> XMLPlatformUtils::Initialize();
> }
> catch (const XMLException& e)
> {
> // report error
> return E_FAIL;
> }
>
> static DOMParser parser; // if not static, will crash second
>time thru - I don't know why
>
> parser.setDoValidation(false);
> parser.setDoNamespaces(false);
> TGXMLErrorHandler errorHandler(/*param*/);
> parser.setErrorHandler(&errorHandler);
> parser.setExpandEntityReferences(true);
> parser.setToCreateXMLDeclTypeNode(true);
>
> static const char* BufId = "LiveOakXMLResponse";
> MemBufInputSource memBufIS((const XMLByte*)psbXML, isbXMLLength,
>BufId, false);
>
> try
> {
> parser.parse(memBufIS);
> }
> catch (const XMLException& e)
> {
> // report error
> delete[] psbXML;
> XMLPlatformUtils::Terminate();
> return E_FAIL;
> }
>
> DOM_Document doc = parser.getDocument();
> hr = TraverseDOM(doc /*other param*/);
>
> delete[] psbXML;
>
> XMLPlatformUtils::Terminate();
>
> return hr;
>}
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
>For additional commands, e-mail: xerces-c-dev-help@xml.apache.org