Urgent help on IXMLHTTPREQUEST

A

Agam Mehta

Hi,

Everything works fine with ixmlhttprequest. It gives me "access
violation" only when i am trying to release it from the memory (i.e
pXMLHttpReq->Release()).

Below is my code.
//////////////////////////////////////////////////////////////////////////
::CoInitialize(NULL);
// Variables.
bstr_t sUrl = "http://localhost/test.xml";
bstr_t sMethod = "GET";
_variant_t vUser = L"USERNAME";
_variant_t vPassword = L"PASSWORD";
_variant_t vAsync = (bool)FALSE;
long lStatus = 0;
BSTR bstrResp;
BSTR bstrResponseText;
HRESULT hr;

IXMLHTTPRequestPtr pXMLHttpReq= NULL;
hr=pXMLHttpReq.CreateInstance("Msxml2.XMLHTTP.4.0");

if(hr != S_OK)
{

}
try
{
// Open the XMLHTTPRequest object with the DELETE method and
// specify that it will be sent asynchronously.
pXMLHttpReq->open(sMethod,
sUrl,
vAsync,
vUser,
vPassword);


pXMLHttpReq->send();

// Get the response status.
pXMLHttpReq->get_status(&lStatus);

// An error occurred on the server.
if(lStatus == 500)
{
}

else
{
// Display the response status.

// Display the response status text.
pXMLHttpReq->get_statusText(&bstrResp);

// Display the response text.
pXMLHttpReq->get_responseText(&bstrResponseText);
}

// Release the memory.


pXMLHttpReq->Release(); //THIS IS WHERE I GET ACCESS VIOLATION ERROR

}
catch(_com_error &e)
{
// Display the error information.

// Release the memory.
pXMLHttpReq->Release(); //THIS IS WHERE I GET ACCESS VIOLATION
ERROR

return 1;
}

CoUninitialize();
//////////////////////////////////////////////////////////////////////////

I have been trying to solve this for a long time but am not able to
find answer to it. Please help . I am using Ms Visual Studio 6.0 sp6.

Thanks for your help.
Agam Mehta
 
W

William DePalo [MVP VC++]

Agam Mehta said:
Everything works fine with ixmlhttprequest. It gives me "access
violation" only when i am trying to release it from the memory (i.e
pXMLHttpReq->Release()).

Almost everyone makes the error you made once.

COM's smart pointers see to it that Release() is called when an object goes
out of scope. The problem is that your object goes out of scope after the
return statement. But at that point COM has already been uninitialized and
the call to Release() does a bad thing. <g>

One thing that you can do is to change the location where the object goes
out of scope:

::CoInitialize(0);
{
// Insert your declarations and code here
}
::CoUninitialize();

Now your object will go out of scope at the right brace _before_
CoUnitialize() is called. The destructor of your object runs and Release()
gets called _before_ COM is uninitialized.

Regards,
Will
 
A

Agam Mehta

Thanks William for your suggestion. But now when i try it the way you
suggested i get "Breakpoint" error. i am trying really simple thing.

///////////////////////////////////////////////////////////////
HRESULT hresult;
hresult = ::CoInitialize(0);
if(hresult == S_OK)
{
IXMLHTTPRequestPtr pXMLHttpReq= NULL;
hresult=pXMLHttpReq.CreateInstance("Msxml2.XMLHTTP.4.0");

pXMLHttpReq->Release();
}
::CoUninitialize();
///////////////////////////////////////////////////////////////

Please help.
Thanks,
Agam Mehta
 
W

William DePalo [MVP VC++]

Agam Mehta said:
Thanks William for your suggestion.

You are welcome.
But now when i try it the way you suggested i get "Breakpoint" error.

Check the output window. The class designer may have put in a breakpoint to
signal a coding error. The breakpoints are often accompanied by a debug
message.
i am trying really simple thing.

Nothing about COM is simple. Ever. :)

Regards,
Will
 
A

Agam Mehta

Hi William DePalo,

I checked my output window and it doesn't have any breakpoint in it.
This sucks. Please help.

Thanks,
Agam Mehta
 
W

William DePalo [MVP VC++]

Agam Mehta said:
I checked my output window and it doesn't have any breakpoint in it.

Hmm, earlier you wrote:

"But now when i try it the way you suggested i get "Breakpoint" error."
This sucks.

Nope.

Developing software is a strange endeavor. It is _far_ too easy to do at
all. And _extraordinarily_ difficult to do well.

As I see it, that's the big problem. And it continues to get worse because
as tools get better they lower the bar to entry. The better tools and more
capable systems lead users to want more and developers to try to do more.
That serves to increase the complexity. So the tools have to get better.
They do. But then they hide more which adds to complexity. Which puts us
back on the carousel for another ride and we go around again and again ...

But I digress ... said:
Please help.

I'll try. Are you running in debug mode? If not, do so. Do you get a
breakpoint error or not? If you do what else do you see in the output window
when the debug error is reported. It is hard to imagine that anyone would
insert a breakpoint error to signal an error and not add a diagnostic
message so you can troubleshoot the problem.

Regards,
Will
 
A

Agam Mehta

William,

Thanks for your response. Let me tell you i am using console
application with no MFC support. I guess this souldn't matter. Yes i
am running this application in debug mode. When i call "Release()" it
shows "Breakpoint message box" and displays "Extra call to Release()
!!!" is showed in my output window.

I have no idea why it even does this.

thank you very much for your help.

Agam Mehta
 
W

William DePalo [MVP VC++]

Agam Mehta said:
Thanks for your response.

You are welcome.
Let me tell you i am using console
application with no MFC support.
OK.

I guess this souldn't matter.

That's right.
Yes i am running this application in debug mode.

Good start.
When i call "Release()" it shows "Breakpoint message box"
and displays "Extra call to Release()
!!!" is showed in my output window.

I have no idea why it even does this.

OK. You need to check a good text on the Component Object Model - COM. COM
objects are reference counted. That is to say that there are methods named
Add() and Release() in an interface which are used to increment the count of
clients that hold references to an object. When the reference count goes to
0 no client is holding a reference and the object may be deallocated. The
diagnostic is telling you that there is one EXTRA call to Release() in yout
application which would have set the reference count to -1. That's what you
have to fix.

Regards,
Will
 
V

Vladimir Nesterovsky

///////////////////////////////////////////////////////////////
HRESULT hresult;
hresult = ::CoInitialize(0);
if(hresult == S_OK)
{
IXMLHTTPRequestPtr pXMLHttpReq= NULL;
hresult=pXMLHttpReq.CreateInstance("Msxml2.XMLHTTP.4.0");

pXMLHttpReq->Release();
}
::CoUninitialize();
///////////////////////////////////////////////////////////////

IXMLHTTPRequestPtr is a smart pointer and it releases COM object by itself
when it goes out of scope. Don't call pXMLHttpReq->Release() yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top