Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AnsiString thread memory bug

Status
Not open for further replies.

sebbetek

Programmer
Nov 27, 2006
4
SE
I use Borland C++ 6, and there seems to be a bug!
I made a simple test program starting several threads, where each thread Execute function calls a recursive function which loops and makes some heavy allocation... (not)! Actually I narrowed it down to this, and it still gives EOutofmem exceptions!
Recurs is called from Execute.
int i has had values from 2-10 in a couple of tests when eoutofmem occurs, so the recursion hasn't got so far...

typedef AnsiString MyString;

int TTestThread::Recurs(int i)
try
{
int j = 0;
const int rmax = 20;
const int hmax = 350;

for(j = 0; j < hmax; j++)
{
MyString strTest2 = "test";
strTest2 += MyString("Random test: ") + "test3";
//OutputDebugString(IntToStr(j).c_str());
}
}
catch(Exception & e)
{
OutputDebugString(e.Message.c_str());
//ShowMessage(e.Message);
}
if(i < rmax)
{
Sleep(10);
i = Recurs(i);
}
else
{
i = rmax + 1;
//OutputDebugString("exiting recurs?");
}
return i;

}

Is this bug known and are there any workarounds?
I tryed using std::string instead, and that actually seemed to solve it, because no more exceptions of this kind ocurrs. But when I try to use it in my real project, which is based on an exe and several dlls, some other strange things occur (eg. memory access overruns in the std::string class functions - another bug in Borlands implementation or what??)

Best Regards,
Sebastian
 
First question, are you fully patched? BCB 6 had 4 updates plus a linker update.



James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
To answer your question, I'm not 100% sure, but I should be.
At my company, we automatically install update 1,2 and 4, but I don't know if there are any new ones?

In the about box, the version info is:
Version 6.0 (Build 10.166)

(The second problem in my post should be ignored, the project is large, so there are many things that could be wrong, but the first one is a very simple and clean test: an exe with default settings, some threads and a call stack, not too large, using some AnsiString functions (especially the operator+=, and global operator+, and maybe with the condition that the string is created and initialized with some value within the loop's curly braces.)
 
I've not seen the problems with AnsiString that you are describing. Just out of curiosity, what happens when you move "MyString strTest2 = "test";" out of the for-loop. It looks like you are creating strTest2 350 times and never deleting it. That could create a "memory leak."

Code:
    MyString strTest2 = "test";
    for(j = 0; j < hmax; j++)
    {
        strTest2 += MyString("Random test: ") + "test3";
        //OutputDebugString(IntToStr(j).c_str());
    }


James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Then it doesn't happen. But that shouldn't be a leak, because it's "created" on the stack, and should be deleted through each loop, or at least not recreated, AFAIK.

BTW, I found that the problem didn't happen when I turned off Codeguard... so maybe it's a CodeGuard bug?
 
That's possible. I'm not a big fan of Codeguard so I don't use it.


James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top