Board index » cppbuilder » Is there a memory leak/problem in this code?

Is there a memory leak/problem in this code?


2004-08-05 08:47:47 PM
cppbuilder112
Hi,
I have the below method in an ASP ActiveX object. The ASP page instanciates the object (and makes method calls) for a while, then all of a sudden I get one of those famous "error '80010105'" errors when this method is called from the ASP page. Is there something up with the BSTRs? I know BSTR memory mgt can be tricky if you're unfamiliar with how they work (which I think I am, a little).
All other methods execute with no problem. I've tried using try-catch to print out more info, but it didn't change anything -- like it was untrappable.
Also, is it a problem that the methods aspwrite_this(), aspwrite_that(), and aspwrite_something_else() are all public (STDMETHODIMP) methods exposed by the ASP object?
Anyway, if anyone's got a clue on this, I'd much appreciate the feed back. Thanks! Here is the method:
STDMETHODIMP TmyappImpl::out_to_browser(BSTR width, BSTR action, long id)
{
AnsiString myaction = AnsiString(action);
if( myaction == "showthis" )
aspwrite_this( width, start, max );
else if( myaction == "showthat" )
aspwrite_that( id );
else if( myaction == "showsomethingelse" )
aspwrite_something_else( id );
else
aspwrite_this( width );
return( S_OK );
}
 
 

Re:Is there a memory leak/problem in this code?

Vienna B wrote:
Quote
Hi,

I have the below method in an ASP ActiveX object. The ASP page instanciates the object (and makes method calls) for a while, then all of a sudden I get one of those famous "error '80010105'" errors when this method is called from the ASP page. Is there something up with the BSTRs? I know BSTR memory mgt can be tricky if you're unfamiliar with how they work (which I think I am, a little).

All other methods execute with no problem. I've tried using try-catch to print out more info, but it didn't change anything -- like it was untrappable.

Also, is it a problem that the methods aspwrite_this(), aspwrite_that(), and aspwrite_something_else() are all public (STDMETHODIMP) methods exposed by the ASP object?

Anyway, if anyone's got a clue on this, I'd much appreciate the feed back. Thanks! Here is the method:

STDMETHODIMP TmyappImpl::out_to_browser(BSTR width, BSTR action, long id)
{
AnsiString myaction = AnsiString(action);

if( myaction == "showthis" )
aspwrite_this( width, start, max );
else if( myaction == "showthat" )
aspwrite_that( id );
else if( myaction == "showsomethingelse" )
aspwrite_something_else( id );
else
aspwrite_this( width );

return( S_OK );
}


Not a general solution, but whenever I have to receive a BSTR ( as a
value, not as a pointer ), I always do the following:
void obj:Func( BSTR value ) {
Widestring wVal;
wVal.Attach(value);
AnsiString aVal = wVal;
}
I have many COM interfaces where I have to be publishing information
back and forth, and my subscriber interfaces receive
information in much the same way I've described above.
Hope this helps.
Marcelo
 

Re:Is there a memory leak/problem in this code?

"Marcelo R. Lopez, Jr." < XXXX@XXXXX.COM >wrote:
Quote
Not a general solution, but whenever I have to
receive a BSTR ( as a value, not as a pointer ),
I always do the following:
If you always do that, then your code is always wrong. You are telling WideString to take ownership of memory it has no business taking ownsership of in the first place. The memory belongs to the caller, not the called function.
Quote
Widestring wVal;
wVal.Attach(value);
AnsiString aVal = wVal;
You don't need WideString for that. AnsiString has a constructor which accepts a wchar_t*, and thus can accept the BSTR directly:
AnsiString aVal = value;
Gambit
 

{smallsort}