This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Maybe bug in call function to static declaration variable?
- From: Axel Freyn <axel-freyn at gmx dot de>
- To: gcc at gcc dot gnu dot org
- Date: Sun, 14 Nov 2010 16:43:16 +0100
- Subject: Re: Maybe bug in call function to static declaration variable?
- References: <1202777932.20101114094743@ucp.ho.rimed.cu>
Hi Gilberto,
On Sun, Nov 14, 2010 at 09:47:43AM -0500, Gilberto Cuba Ricardo wrote:
> Hi colleagues,
>
> The follow program is an abstraction of on application where it
> produce an error in runtime and i don't know why it's success.
>
> The program compile very good in all cases with Visual Studio 2005,
> 2008, 2010, MinGW 4.5.1 on Windows 7 and with GCC 4.3 on GNU Debian.
> Though, it produce an error in runtime when it's compiled with MinGW
> or GCC and next it's executed.
>
> Anybody can help me to distinguish whether it's an error of my source
> code or a bug of gcc?
I believe it's a problem of your code.
If I reduce your example code to a minimal example showing the problem,
I obtain:
void type_schema();
struct sponge_object_schema
{
sponge_object_schema(){
type_schema();
}
};
void type_schema()
{
static sponge_object_schema result;
};
int main()
{
type_schema();
}
This creates the same runtime-error:
terminate called after throwing an instance of '__gnu_cxx::recursive_init_error'
what(): std::exception
In this more simple example, one can see the problem: In order to create
the static object "result" in the function "type_schema", the function
"type_schema" is called itself (from the constructor needed for "type"):
this is not possible:
- the code executes "type_schema", and allocates memory for the
variable "result".
- then the constructor "sponge_object_schema" is called.
- the constructor calls "type_schema"
- and now in "type_shema", it is not possible to access/use the static
object "result", as its constructor is NOT FINISHED YET
So you have a problem.
I have no idea, whether the compiler could/should detect this problem at
compile-time (well, in this particular example it could, but in general
it is impossible), but at least it's detected at runtime :-)
In your example code, the same thing happens:
"type_schema<sponge_object>" calls the constructor for the static object
-- but then "type_schema<sponge_object>" is again called from this
constructor.
I don't see a way how the compiler COULD create working code which
solves this problem -- For your code it would be necessary to USE the
static object before its constructor is finished...
HTH,
Axel