SiComponents Home Page SiComponents Forums
Here you will be able to get help and share your experience
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GetTextOrDefaultC: freeing memory?

 
Post new topic   Reply to topic    SiComponents Forums Forum Index -> TsiLang Components Suite
View previous topic :: View next topic  
Author Message
khs



Joined: 09 Nov 2006
Posts: 7

PostPosted: Mon Nov 13, 2006 1:26 pm    Post subject: GetTextOrDefaultC: freeing memory? Reply with quote

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
Back to top
View user's profile Send private message
isiticov
Site Admin


Joined: 21 Nov 2002
Posts: 2103

PostPosted: Mon Nov 13, 2006 2:53 pm    Post subject: Reply with quote

You need to free it by yourself and you can free it right after the usage.
Back to top
View user's profile Send private message
khs



Joined: 09 Nov 2006
Posts: 7

PostPosted: Mon Nov 13, 2006 3:54 pm    Post subject: ... Reply with quote

Using plain delete[]?
Back to top
View user's profile Send private message
khs



Joined: 09 Nov 2006
Posts: 7

PostPosted: Mon Nov 13, 2006 4:24 pm    Post subject: ... Reply with quote

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:

Code:
char *foo[2]={"", somefunc("IDS_0")}
, with
Code:
char *somefunc(const AnsiString bar) { return "foo"; }


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.
Back to top
View user's profile Send private message
isiticov
Site Admin


Joined: 21 Nov 2002
Posts: 2103

PostPosted: Mon Nov 13, 2006 6:11 pm    Post subject: Reply with quote

The following code:
Code:
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);
}

works fine under C++Builder 4. I didn't check the generated ASM code. But at least there is no AV generated.
Back to top
View user's profile Send private message
khs



Joined: 09 Nov 2006
Posts: 7

PostPosted: Mon Nov 13, 2006 7:04 pm    Post subject: ... Reply with quote

Yes, in general using GetTextOrDefault only in very simple constructs seems to be a good idea. Or updating to Turbo C++... Wink

So is delete[] ok for the returned char *?
Back to top
View user's profile Send private message
isiticov
Site Admin


Joined: 21 Nov 2002
Posts: 2103

PostPosted: Tue Nov 14, 2006 3:50 am    Post subject: Reply with quote

Yes, I think so.
Back to top
View user's profile Send private message
khs



Joined: 09 Nov 2006
Posts: 7

PostPosted: Tue Nov 14, 2006 12:57 pm    Post subject: ... Reply with quote

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.
Back to top
View user's profile Send private message
khs



Joined: 09 Nov 2006
Posts: 7

PostPosted: Tue Nov 14, 2006 1:00 pm    Post subject: ... Reply with quote

... that has to do with buggy handling of temporary objects used during the construction of arrays.
Back to top
View user's profile Send private message
khs



Joined: 09 Nov 2006
Posts: 7

PostPosted: Tue Nov 14, 2006 2:05 pm    Post subject: test Reply with quote

Also, the code that is produced for

Code:
MessageBox(0, siLang1->GetTextOrDefault("IDS_xx").c_str(), siLang1->GetTextOrDefault("IDS_yy").c_str(), MB_OK);


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.
Back to top
View user's profile Send private message
isiticov
Site Admin


Joined: 21 Nov 2002
Posts: 2103

PostPosted: Tue Nov 14, 2006 2:39 pm    Post subject: Reply with quote

Yes, because you deal with AnsiString returned by GetTextOrDefault() Smile
So you can use it in this way.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    SiComponents Forums Forum Index -> TsiLang Components Suite All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by p h p B B  © 2001, 2005 p h p B B  Group