Board index » cppbuilder » OleStrToString() seems to cause huge memory leak...HELP!

OleStrToString() seems to cause huge memory leak...HELP!

I have an application that uses a COM DLL for a number of routines.
One of the most frequently called functions takes a BSTR as one of its
parameters. (can be called millions of times in a session.)

The application is working just fine, except that it has a massive memory
leak. After some tedious searching, I eventually traced it down to the BSTR.
I tried all sorts of things to free the memory allocated to the BSTR but had
no luck.

Here is a test program I wrote to try and solve the problem, I tried
different combinations, but it didn't work......Can someone please help me,
if I can't get BSTR's to work, then Our project will require big changes :(

The way this program is now there is a HUGE memory leak.

/* TEST PROGRAM */ (Uses a plain form with member variable [ bool
 m_bStop ] )

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    AnsiString str;

    int nCount=0;

    m_bStop = false;

    while(nCount<100000 && m_bStop ==false)
    {

        str = "C:\\Test\\Test\\Test\\Test\\Test\\Testing Pass #" +
IntToStr(nCount);
        BSTR pbStr = StringToOleStr(str);          // If I dont use the
pbStr and just copy str to txText->Text,

// there is no memoryleak
        txText->Text = OleStrToString(pbStr);

        if(nCount%20 == 0) Application->ProcessMessages();

        delete[] pbStr;     // This delete doesnt seem to help at all...Ive
also tried plain old delete without ([ ]).
        nCount++;
    }

    Application->ProcessMessages();

Quote
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    m_bStop=true;

Quote
}

PLEASE HELP!
(Mail me if possible)

THANKS ALOT! :)
===============
Ryan Gallagher

 

Re:OleStrToString() seems to cause huge memory leak...HELP!


Ryan Gallagher escribi en mensaje <6n3v23$r...@forums.borland.com>...

[snip]

Quote
>Here is a test program I wrote to try and solve the problem, I tried
>different combinations, but it didn't work......Can someone please help me,
>if I can't get BSTR's to work, then Our project will require big changes :(

>The way this program is now there is a HUGE memory leak.

>/* TEST PROGRAM */ (Uses a plain form with member variable [ bool
> m_bStop ] )

>void __fastcall TForm1::Button1Click(TObject *Sender)
>{
>    AnsiString str;

>    int nCount=0;

>    m_bStop = false;

>    while(nCount<100000 && m_bStop ==false)
>    {

>        str = "C:\\Test\\Test\\Test\\Test\\Test\\Testing Pass #" +
>IntToStr(nCount);
>        BSTR pbStr = StringToOleStr(str);          // If I dont use the
>pbStr and just copy str to txText->Text,

>// there is no memoryleak
>        txText->Text = OleStrToString(pbStr);

>        if(nCount%20 == 0) Application->ProcessMessages();

>        delete[] pbStr;     // This delete doesnt seem to help at all...Ive
>also tried plain old delete without ([ ]).

Maybe I'm a little bit out of my depth... I *must* admit OLE baffles me....
but, from my *very* beginner point of view... you're not creating the BSTR
dinamically... so using delete on it won't do a thing :(
Also, you're running the loop an enormous number ot f times, so you get a
huge number of variables stacking up in memory, although they can't be
accessed... I know that, being local to the loop, they should be
destroyed... but *maybe* the problem is there...

I know that I'll probably be talking nonsense... it's only that seing a
"delete" w/o a "new" strikes me as odd :((

I only hope I haven't wasted your time :)

(PS: I haven't really tried my idea... my BCB just quit on me
yestarday -I've just done some big tweaking w/ W95, so I guess I have to
reinstall it. )

Justo my .002 :)

Javier JJ

fjpp.s...@softhome.net

-------
Delete ".spam" from the "from" field to answer
-------
Borra ".spam" de la direccin para contestar :)

Re:OleStrToString() seems to cause huge memory leak...HELP!


There are several functions out there that will take a null pointer , and
allocate memory for you using "new". Then it is up to you to call the delete
operator on them. But anyway, I've traced my problem down to BSTR. It seems
you cannot call delete on them because they use system memory, and therefor
you need to call the API function SystemFreeString(BSTR bString) to get rid
of its resources..... " I did not know that! :) "

Thanks for your comments
=========================

Quote
Javier wrote in message <6n51ot$s...@forums.borland.com>...

>Ryan Gallagher escribi en mensaje <6n3v23$r...@forums.borland.com>...

>[snip]

>>Here is a test program I wrote to try and solve the problem, I tried
>>different combinations, but it didn't work......Can someone please help
me,
>>if I can't get BSTR's to work, then Our project will require big changes
:(

>>The way this program is now there is a HUGE memory leak.

>>/* TEST PROGRAM */ (Uses a plain form with member variable [ bool
>> m_bStop ] )

>>void __fastcall TForm1::Button1Click(TObject *Sender)
>>{
>>    AnsiString str;

>>    int nCount=0;

>>    m_bStop = false;

>>    while(nCount<100000 && m_bStop ==false)
>>    {

>>        str = "C:\\Test\\Test\\Test\\Test\\Test\\Testing Pass #" +
>>IntToStr(nCount);
>>        BSTR pbStr = StringToOleStr(str);          // If I dont use the
>>pbStr and just copy str to txText->Text,

>>// there is no memoryleak
>>        txText->Text = OleStrToString(pbStr);

>>        if(nCount%20 == 0) Application->ProcessMessages();

>>        delete[] pbStr;     // This delete doesnt seem to help at
all...Ive
>>also tried plain old delete without ([ ]).

>Maybe I'm a little bit out of my depth... I *must* admit OLE baffles me....
>but, from my *very* beginner point of view... you're not creating the BSTR
>dinamically... so using delete on it won't do a thing :(
>Also, you're running the loop an enormous number ot f times, so you get a
>huge number of variables stacking up in memory, although they can't be
>accessed... I know that, being local to the loop, they should be
>destroyed... but *maybe* the problem is there...

>I know that I'll probably be talking nonsense... it's only that seing a
>"delete" w/o a "new" strikes me as odd :((

>I only hope I haven't wasted your time :)

>(PS: I haven't really tried my idea... my BCB just quit on me
>yestarday -I've just done some big tweaking w/ W95, so I guess I have to
>reinstall it. )

>Justo my .002 :)

>Javier JJ

>fjpp.s...@softhome.net

>-------
>Delete ".spam" from the "from" field to answer
>-------
>Borra ".spam" de la direccin para contestar :)

Re:OleStrToString() seems to cause huge memory leak...HELP!


Ryan Gallagher escribi en mensaje <6n5t5u$t...@forums.borland.com>...

Quote
>There are several functions out there that will take a null pointer , and
>allocate memory for you using "new". Then it is up to you to call the
delete
>operator on them. But anyway, I've traced my problem down to BSTR. It seems
>you cannot call delete on them because they use system memory, and therefor
>you need to call the API function SystemFreeString(BSTR bString) to get rid
>of its resources..... " I did not know that! :) "

>Thanks for your comments
>=========================

Thanks to you for replying!!.... Noy I'll save myself some tumbling about
if/when I get into this situation.... I'm sorry I wasn't more helpful :((

Yours

Javier JJ

fjpp.s...@softhome.net

-------
Delete ".spam" from the "from" field to answer
-------
Borra ".spam" de la direccin para contestar :)

Quote

>Javier wrote in message <6n51ot$s...@forums.borland.com>...

>>Ryan Gallagher escribi en mensaje <6n3v23$r...@forums.borland.com>...

>>[snip]

>>>Here is a test program I wrote to try and solve the problem, I tried
>>>different combinations, but it didn't work......Can someone please help
>me,
>>>if I can't get BSTR's to work, then Our project will require big changes
>:(

>>>The way this program is now there is a HUGE memory leak.

>>>/* TEST PROGRAM */ (Uses a plain form with member variable [ bool
>>> m_bStop ] )

>>>void __fastcall TForm1::Button1Click(TObject *Sender)
>>>{
>>>    AnsiString str;

>>>    int nCount=0;

>>>    m_bStop = false;

>>>    while(nCount<100000 && m_bStop ==false)
>>>    {

>>>        str = "C:\\Test\\Test\\Test\\Test\\Test\\Testing Pass #" +
>>>IntToStr(nCount);
>>>        BSTR pbStr = StringToOleStr(str);          // If I dont use the
>>>pbStr and just copy str to txText->Text,

>>>// there is no memoryleak
>>>        txText->Text = OleStrToString(pbStr);

>>>        if(nCount%20 == 0) Application->ProcessMessages();

>>>        delete[] pbStr;     // This delete doesnt seem to help at
>all...Ive
>>>also tried plain old delete without ([ ]).

>>Maybe I'm a little bit out of my depth... I *must* admit OLE baffles
me....
>>but, from my *very* beginner point of view... you're not creating the BSTR
>>dinamically... so using delete on it won't do a thing :(
>>Also, you're running the loop an enormous number ot f times, so you get a
>>huge number of variables stacking up in memory, although they can't be
>>accessed... I know that, being local to the loop, they should be
>>destroyed... but *maybe* the problem is there...

>>I know that I'll probably be talking nonsense... it's only that seing a
>>"delete" w/o a "new" strikes me as odd :((

>>I only hope I haven't wasted your time :)

>>(PS: I haven't really tried my idea... my BCB just quit on me
>>yestarday -I've just done some big tweaking w/ W95, so I guess I have to
>>reinstall it. )

>>Justo my .002 :)

>>Javier JJ

>>fjpp.s...@softhome.net

>>-------
>>Delete ".spam" from the "from" field to answer
>>-------
>>Borra ".spam" de la direccin para contestar :)

Re:OleStrToString() seems to cause huge memory leak...HELP!


for other answers .............

http://www.inprise.com/bcppbuilder/books/chapter17/

Other Threads