Board index » cppbuilder » Global variable freezes C++ Builder

Global variable freezes C++ Builder


2006-11-14 08:27:57 PM
cppbuilder32
Hi all, I'm having a problem with a variable globalization.
I'm coding a library wich includes some function that uses this variable:
locale Loc("it_IT")
I wish to make it global, so I can change charset, setting up just one
variable.
I tried this in functions.cpp:
static locale Loc("it_IT");
or
extern locale Loc;
and initializing it out of main...
but nothing... when I run the app, builder freezes...
Probably I wrong the way to globalize it...
Any idea?
 
 

Re:Global variable freezes C++ Builder

Giovanni wrote:
Quote
I tried this in functions.cpp:

static locale Loc("it_IT");
this will be visible throughout the functions.cpp compilation unit,
and nowhere else.
Quote

or

extern locale Loc;
and initializing it out of main...
but nothing... when I run the app, builder freezes...
Probably I wrong the way to globalize it...
Any idea?
I'd have to see code, but my guess is that you don't have a proper
definition. Some place you need Loc without the "extern".
e.g.
// main.cpp
extern locale Loc;
int main()
{
// do something
return 0;
}
// functions.cpp
locale Loc;
// various functions
--
Liz
Please check the newsgroup guidelines and general netiquette
info.borland.com/newsgroups/guide.html
info.borland.com/newsgroups/guide.html
info.borland.com/newsgroups/netiquette.html
 

Re:Global variable freezes C++ Builder

Giovanni wrote:
Quote
...
bool getNextInitial(AnsiString Text,int& Pos,char& Initial)
{
//This function uses the isalpha() to check if a character is
//a letter. The function needs a locale object.
...
locale Loc("it_IT");
...
}
Loc in this function is visible ONLY to this function. If you want
to use Loc declared elsewhere, then you can't declare it in the
function.
declare and define it at the top of functions.cpp:
locale Loc("it_IT");
declare (but don't define it) again in main:
e.g.
extern locale Loc;
--
Liz
Please check the newsgroup guidelines and general netiquette
info.borland.com/newsgroups/guide.html
info.borland.com/newsgroups/guide.html
info.borland.com/newsgroups/netiquette.html
 

{smallsort}

Re:Global variable freezes C++ Builder

Ok. That's all:
//functions.h----------------------------------------------
#include <locale>
...
bool getNextInitial(AnsiString Text,int& Pos,char& Initial);
... and so on...
//functions.cpp--------------------------------------------
...
bool getNextInitial(AnsiString Text,int& Pos,char& Initial)
{
//This function uses the isalpha() to check if a character is
//a letter. The function needs a locale object.
...
locale Loc("it_IT");
...
}
//---------------------------------------------------------
In main() I call the function getNextInitial...
In my library, there are many functions that use locale, so I wish
initialize Loc in the main() and all methods in function.h have to
use main()'s initialization. Thus I can change localization one time in
the main, instead many times in function.cpp
tnx again, Giovanni
 

Re:Global variable freezes C++ Builder

Giovanni wrote:
Quote
I tried. Now I explain to you what happens.
I cannot get main Window(Builder seems freezed). After some seconds
popups the CPU debbugging window that tells me:
Process.....faulted with message: access violation at .........:
read of address ......... . Process stopped. Use step or run to
continue. Stepping or running bring me up always this error message

mmm :(
I can only guess, because you haven't shown your code but I STRONGLY
suggest that you've attempted to access your locale before
initializing it...
So, as a fix, define it in function.hpp, and call an initialization
routine too.
--
Liz
Please check the newsgroup guidelines and general netiquette
info.borland.com/newsgroups/guide.html
info.borland.com/newsgroups/guide.html
info.borland.com/newsgroups/netiquette.html
 

Re:Global variable freezes C++ Builder

Liz ha scritto:
Quote
declare and define it at the top of functions.cpp:

locale Loc("it_IT");

declare (but don't define it) again in main:

e.g.

extern locale Loc;

I tried. Now I explain to you what happens.
I cannot get main Window(Builder seems freezed). After some seconds
popups the CPU debbugging window that tells me:
Process.....faulted with message:
access violation at .........: read of address ......... . Process
stopped. Use step or run to continue.
Stepping or running bring me up always this error message
mmm :(
 

Re:Global variable freezes C++ Builder

Giovanni wrote:
As Liz has suggested:
Quote
//functions.h----------------------------------------------
#include <locale>
void SetMyLocale( const char *locname ); // call this from main
Quote
bool getNextInitial(AnsiString Text,int& Pos,char& Initial);
//functions.cpp--------------------------------------------
static locale *Loc = new locale("it_IT");
void SetMyLocale( const char *locname )
{
delete Loc;
Loc = new locale( locname );
}
Quote
bool getNextInitial(AnsiString Text,int& Pos,char& Initial)
{
// The function needs a locale object.
use Loc
}
//---------------------------------------------------------
In main() I call the function getNextInitial...
In main, first call SetMyLocale( "newlocale");
Quote
In my library, there are many functions that use locale, so I wish
initialize Loc in the main() and all methods in function.h have to
use main()'s initialization. Thus I can change localization one time in
the main, instead many times in function.cpp
If this library is in several modules, then the Loc would need to be
non-static in the above code. and the header would also have
external locale*Loc;
 

Re:Global variable freezes C++ Builder

Bob Gonder ha scritto:
Quote
>//functions.h----------------------------------------------
void SetMyLocale( const char *locname ); // call this from main
>//functions.cpp--------------------------------------------
static locale *Loc = new locale("it_IT");
In main, first call SetMyLocale( "newlocale");
And if it's a visual application instead console?
Have I to insert into main form's constructor?
tnx
 

Re:Global variable freezes C++ Builder

Giovanni wrote:
Quote
Bob Gonder ha scritto:
>static locale *Loc = new locale("it_IT");
>In main, first call SetMyLocale( "newlocale");

And if it's a visual application instead console?
Have I to insert into main form's constructor?
Makes no difference the type of application.
If you don't want to use the default locale ("it_IT"), call the
SetMyLocale before you call any of your libraries that use it.
_where_ you do that is up to you.
The main form's constructor might be ok.
WinMain might be another place.
 

Re:Global variable freezes C++ Builder

Done it ;-)
I solved my problem thus:
in TForm's constructor i call SetMyLocale("it_IT");
in functions.h I declare
void SetMyLocale(const char *l);
in functions.cpp
first of all
locale Loc; ( isalpha(...) wants an object, so I had change it)
and in implementation of SetMyLocale:
Loc=locale(l);
In this way I can change all charset by the main form.
Did I coded correctly or did I made some pig's food? :)
Do I forget something?
Tnx you all to helped me...
Bob Gonder ha scritto:
Quote
Giovanni wrote:

>Bob Gonder ha scritto:
>>static locale *Loc = new locale("it_IT");
>>In main, first call SetMyLocale( "newlocale");
>And if it's a visual application instead console?
>Have I to insert into main form's constructor?

Makes no difference the type of application.
If you don't want to use the default locale ("it_IT"), call the
SetMyLocale before you call any of your libraries that use it.

_where_ you do that is up to you.
The main form's constructor might be ok.
WinMain might be another place.


 

Re:Global variable freezes C++ Builder

Giovanni wrote:
Quote
locale Loc; ( isalpha(...) wants an object, so I had change it)
No it doesn't.
It wants a reference to an object.
Give it Loc instead of &Loc.
if( isalpha( 'A', Loc ) )
(My C++ is rusty, so it might even be *Loc instead)
Quote
and in implementation of SetMyLocale:
Loc=locale(l);

In this way I can change all charset by the main form.
In this way you can cause a memory leak or worse.
Let's look at this line:
Loc = locale( l );
This creates a temporary locale object.
It then copies the temporary, overwritting the object that occupied
Loc. Whatever was in Loc is lost. It cannot be sent to delete() to
free any resources.
It then does delete() on the temporary. The resources allocated for/by
the temporary are freed, and might not be available for Loc to use.
Those are some of the reasons my code used pointers and new() and
delete().
Quote
Did I coded correctly or did I made some pig's food? :)
Yes, you made pig's food.
If what you wrote works for you, it is because Dinkumware saved your
bacon. But someday you will replace an object that really doesn't like
being overwritten, and you'll be in it face deep.
 

Re:Global variable freezes C++ Builder

"Bob Gonder" < XXXX@XXXXX.COM >wrote in message
Quote
Giovanni wrote:

>locale Loc; ( isalpha(...) wants an object, so I had change it)

No it doesn't.
It wants a reference to an object.
Give it Loc instead of &Loc.
if( isalpha( 'A', Loc ) )
(My C++ is rusty, so it might even be *Loc instead)

>and in implementation of SetMyLocale:
>Loc=locale(l);
>
>In this way I can change all charset by the main form.

In this way you can cause a memory leak or worse.

Let's look at this line:
Loc = locale( l );
Seems like a typical assignment operator.
Quote
This creates a temporary locale object.

It then copies the temporary, overwritting the object that occupied
Loc. Whatever was in Loc is lost. It cannot be sent to delete() to
free any resources.

It then does delete() on the temporary. The resources allocated for/by
the temporary are freed, and might not be available for Loc to use.
Huh?
Quote
Those are some of the reasons my code used pointers and new() and
delete().
I don't think I've ever seen anyone advise using pointers, new()
and delete() rather than static objects in order to prevent
leaks. <g>
 

Re:Global variable freezes C++ Builder

Duane Hebert wrote:
Quote
"Bob Gonder" wrote in message
>Let's look at this line:
>Loc = locale( l );

I don't think I've ever seen anyone advise using pointers, new()
and delete() rather than static objects in order to prevent
leaks. <g>
Ok.
What happens to the existing Loc object the next time you assign to
it? (This was in a routine, not a static initializer.)
His code:
static locale Loc;
MySetLocale( char*loc )
{ Loc = locale(loc);
}
 

Re:Global variable freezes C++ Builder

"Bob Gonder" < XXXX@XXXXX.COM >wrote in message
Quote
Duane Hebert wrote:

>"Bob Gonder" wrote in message

>>Let's look at this line:
>>Loc = locale( l );
>
>I don't think I've ever seen anyone advise using pointers, new()
>and delete() rather than static objects in order to prevent
>leaks. <g>

Ok.
What happens to the existing Loc object the next time you assign to
it? (This was in a routine, not a static initializer.)

His code:
static locale Loc;
MySetLocale( char*loc )
{ Loc = locale(loc);
}
I would expect the existing Loc object to be
destroyed and replaced with the assigned one.
I mean, isn't this the same as:
int x(10);
x = int(20);
I looked at dinkum's help and there's no
specific operator= for locale() so I assume
it used the compiler supplied one which should
be safe to use like this.
Of course, I would likely choose setLocale() or
something to reassign the existing locale. But
I don't see how leaking is a problem here. What
does codeguard say?
 

Re:Global variable freezes C++ Builder

Quote
I would expect the existing Loc object to be
destroyed and replaced with the assigned one.
I mean, isn't this the same as:
int x(10);
x = int(20);

I looked at dinkum's help and there's no
specific operator= for locale() so I assume
it used the compiler supplied one which should
be safe to use like this.

Of course, I would likely choose setLocale() or
something to reassign the existing locale. But
I don't see how leaking is a problem here. What
does codeguard say?

Ops...you're right locale needs a reference...but...
mmm....boys....is the Loc variable destroyed when the application ends?
If yes, for all execution, doesn't it keep the initialized value?