type with constructor in namespace issue
Re: type with constructor in namespace issue
Thanks badidea.
@Admins and others,
So it seems that the problem has nothing to do with the use of Poseidon, but would only be present when compiling for Linux 64-bit (no problem when compiling for Windows).
The problem when compiling for Linux 64-bit is:
- global variables are constructed after the module constructor execution (or module constructor is executed before global variables construction),
and symmetrically:
- global variables are destructed before the module destructor execution (or module destructor is executed after global variables destruction).
@Admins and others,
So it seems that the problem has nothing to do with the use of Poseidon, but would only be present when compiling for Linux 64-bit (no problem when compiling for Windows).
The problem when compiling for Linux 64-bit is:
- global variables are constructed after the module constructor execution (or module constructor is executed before global variables construction),
and symmetrically:
- global variables are destructed before the module destructor execution (or module destructor is executed after global variables destruction).
Re: type with constructor in namespace issue
Lets draw some attention to this issue:
Your list just got a bit longer :-)coderJeff wrote:Top of my TODO list is the next FreeBASIC release.
Re: type with constructor in namespace issue
Someone who uses Linux and observes this bug for a 64-bit target compilation can he fill in a bug report?
Otherwise I would do it, but just with the forum information!
Otherwise I would do it, but just with the forum information!
Re: type with constructor in namespace issue
fxm wrote:So it seems that the problem has nothing to do with the use of Poseidon, but would only be present when compiling for Linux 64-bit (no problem when compiling for Windows).
Yes its the 64 bit version of the compiler that doesn't work correctly.
My Geany and my terminal both were using the 32 bit version of the compiler and they both worked.
I just tried from the terminal using the 64bit fbc and it showed the problem.
I will post a bug report later, but I would like some help with the description.
Re: type with constructor in namespace issue
I think that a bug report as following is pretty clear for a developer:
Bad timing on static object construction/destruction when compiling for Linux 64-bit
The fault when compiling for Linux 64-bit is the following:
- static (or global) objects are constructed after the module constructor execution instead of before,
and symmetrically:
- static (or global) objects are destructed before the module destructor execution instead of after.
The order is right when compiling for Linux 32-bit and Windows 32/64-bit.
Check there is not the same problem for static simple numeric variables initialization (value defined at declaration level).
+ one forum example (without namespace block)
+ a link to the forum topic
Last edited by fxm on Sep 19, 2018 20:01, edited 7 times in total.
Re: type with constructor in namespace issue
(@fxm: is the (de)initializer/constructor/destructor execution order documented or how do we know what's the "correct" behaviour? - I'm asking just out of interest)
Re: type with constructor in namespace issue
More generally, this problem concerns the construction/destruction of static variables (a Dim Shared variable is also static by default).
For example, at the Storage Classes page of documentation, it is written:
For example, at the Storage Classes page of documentation, it is written:
But no precision on the exact timing for initialization, except in the Glossary (S) at "static storage" paragraph:Static variable, object and array lifetimes begin at program creation and end with program termination.
static storage
Refers to storage in the .BSS or .DATA sections of an executable. Variables, objects and arrays with static storage are allocated and initialized at compile-time and destroyed (in the case of objects) and deallocated at program-termination. Explicitly initialized variables, objects and arrays are allocated in the .DATA section.
Re: type with constructor in namespace issue
@sancho3 or @badidea or anyone which can compile and execute for Linux 64-bit, can you test this code?
Normal output:
Code: Select all
Type UDT1
Dim As Integer I = 123
End Type
Type UDT2
Dim As Integer I
Declare Constructor ()
End Type
Constructor UDT2 ()
This.I = 456
End Constructor
Dim Shared As UDT1 u1
Dim Shared As UDT2 u2
Dim Shared As Integer I = 789
Sub init () Constructor
Print "from module constructor execution:"
Print @u1.I, u1.I
Print @u2.I, u2.I
Print @I, i
Print
u1.I = 321
u2.I = 654
I = 987
End Sub
Print "after module constructor execution:"
Print @u1.I, u1.I
Print @u2.I, u2.I
Print @I, i
Print
Sleep
Code: Select all
from module constructor execution:
4239392 123
4239396 456
4223116 789
after module constructor execution:
4239392 321
4239396 654
4223116 987
Re: type with constructor in namespace issue
slackware linux 64 bit (gcc version 5.3.0)
The exact order is going to depend on the linker, the compiler, and the C-run time start-up code. I will have to research more to give a more technical & exact answer.
In general, data that requires a constructor procedure (UDT or module) can not be relied upon for execution order, and therefore global constructors that refers to another global also requiring a global constructor is not reliable.
The out-of-order problem becomes even more prominent with multiple modules (object files) also having module constructors and globally constructed variables.
Code: Select all
from module constructor execution:
6312568 123
6312576 456
6312392 789
after module constructor execution:
6312568 321
6312576 654
6312392 987
In general, data that requires a constructor procedure (UDT or module) can not be relied upon for execution order, and therefore global constructors that refers to another global also requiring a global constructor is not reliable.
The out-of-order problem becomes even more prominent with multiple modules (object files) also having module constructors and globally constructed variables.
Re: type with constructor in namespace issue
This is why I would like to know how this behaves for a simple numerical variable (without constructor) with an initializer.
Let's see if sancho3 or badidea can give it a try.
If that (surprising for me) behavior is normal, it should be desirable for such global variables (with construction) have their accesses from a module constructor forbidden by the compiler.
Let's see if sancho3 or badidea can give it a try.
If that (surprising for me) behavior is normal, it should be desirable for such global variables (with construction) have their accesses from a module constructor forbidden by the compiler.
Re: type with constructor in namespace issue
fbc32 -gen gas (1.05.0):fxm wrote:@sancho3 or @badidea or anyone which can compile and execute for Linux 64-bit, can you test this code?
Code: Select all
from module constructor execution:
134533740 123
134533744 456
134533644 789
after module constructor execution:
134533740 321
134533744 654
134533644 987
Code: Select all
error 82: Selected -gen gas ASM backend for non-x86 CPU, x86-64
Code: Select all
from module constructor execution:
134533740 0
134533744 0
134533644 789
after module constructor execution:
134533740 123
134533744 456
134533644 987
Code: Select all
from module constructor execution:
6313104 0
6313112 0
6312984 789
after module constructor execution:
6313104 123
6313112 456
6312984 987
Code: Select all
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Re: type with constructor in namespace issue
Thanks.
The problem is therefore only for static variables with constructor (explicit or implicit).
However, no problem for me with gcc both for Win32 and Win64.
The problem is therefore only for static variables with constructor (explicit or implicit).
However, no problem for me with gcc both for Win32 and Win64.
Re: type with constructor in namespace issue
Bug report posted.
I used your description verbatim.
I used your description verbatim.
Re: type with constructor in namespace issue
I left a longer answer on the bug report. Here's the short answer.
I installed ubuntu 14.04 and see the same behaviour. Yep, it all works as expected.
I think the confusing part for most people would be:
- module constructor
- object constructor
They sound related; they use a similar mechanism to function, but they are not related.
There are 2 important facts to know:
- module construction is independent and separate from object construction
- we see different behavior on different systems because of an gcc configuration option that is enabled on some systems and disabled on others.
module constructors expose a low level capability/feature of the linker and c run-time. For any other behaviour we would need to drop that feature and implement something else.
I installed ubuntu 14.04 and see the same behaviour. Yep, it all works as expected.
I think the confusing part for most people would be:
- module constructor
- object constructor
They sound related; they use a similar mechanism to function, but they are not related.
There are 2 important facts to know:
- module construction is independent and separate from object construction
- we see different behavior on different systems because of an gcc configuration option that is enabled on some systems and disabled on others.
module constructors expose a low level capability/feature of the linker and c run-time. For any other behaviour we would need to drop that feature and implement something else.
Re: type with constructor in namespace issue
No confusion for me, but I logically thought that the construction of static/global objects was always done before the call of the module constructors, similar to the initialization of static/global simple variables.