GetTextOrDefaultC: freeing memory?
GetTextOrDefaultC: freeing memory?
Hi there,
just a short question: how/when is freeing the memory which seems to be allocated by GetTextOrDefaultC done, or do I do it myself? Trying to track down an AV here...
Greetings,
khs
just a short question: how/when is freeing the memory which seems to be allocated by GetTextOrDefaultC done, or do I do it myself? Trying to track down an AV here...
Greetings,
khs
...
I think I have found the source of the AV.
The GetTextOrDefault functions take "const AnsiString" as an argument, meaning in C++Builder a temporary AnsiString is constructed out of whatever parameter is passed. However, previous versions of Builder are buggy when handling temporaries. This code:
, with
produces the following asm:
1) delete some yet-unconstructed AnsiString
2) construct a temporary AnsiString from "IDS_0"
3) call somefunc for the temporary AnsiString
4) forget about the temporary AnsiString
I tried it with the express version of Turbo C++, and the sequence is the correct one: construct, call, delete. However, using Builder4 with the bcc32 5.5.1 from the free commandline tools produces the potentially AVing code. Also if AnsiString is replaced with std::string.
The GetTextOrDefault functions take "const AnsiString" as an argument, meaning in C++Builder a temporary AnsiString is constructed out of whatever parameter is passed. However, previous versions of Builder are buggy when handling temporaries. This code:
Code: Select all
char *foo[2]={"", somefunc("IDS_0")}
Code: Select all
char *somefunc(const AnsiString bar) { return "foo"; }
1) delete some yet-unconstructed AnsiString
2) construct a temporary AnsiString from "IDS_0"
3) call somefunc for the temporary AnsiString
4) forget about the temporary AnsiString
I tried it with the express version of Turbo C++, and the sequence is the correct one: construct, call, delete. However, using Builder4 with the bcc32 5.5.1 from the free commandline tools produces the potentially AVing code. Also if AnsiString is replaced with std::string.
The following code:
works fine under C++Builder 4. I didn't check the generated ASM code. But at least there is no AV generated.
Code: Select all
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char * Temp = siLang1->GetTextOrDefaultC("IDS_0" /* "Some test string" */ );
char Temp2[100];
sprintf(Temp2, "%s", Temp);
delete Temp;
ShowMessage(Temp2);
}
Best regards,
Igor Siticov.
Igor Siticov.
...
Just found this totally by chance:
http://qc.borland.com/wc/qcmain.aspx?d=668
So it seems we have a neatly isolated problem here.
http://qc.borland.com/wc/qcmain.aspx?d=668
So it seems we have a neatly isolated problem here.
test
Also, the code that is produced for
seems fine: first twice { construction of parameter-AnsiString and result-AnsiString, call to GetTextOrDefault }, then the MessageBox, then destruction of all 4 AnsiStrings. And it's without delete[] too.
Code: Select all
MessageBox(0, siLang1->GetTextOrDefault("IDS_xx").c_str(), siLang1->GetTextOrDefault("IDS_yy").c_str(), MB_OK);