Hello,
This mailing list is for discussion the development of GCC itself.
Please use the gcc-help mailing list for help questions. Please send
any replies to that list instead of this one, thanks.

On Thu, 28 Mar 2024 at 09:35, shaoben zhu via Gcc <gcc@gcc.gnu.org> wrote:
>
> I compile my program using g++ 4.8.5, I find that when my program exits, it

That version is ancient and has not been supported by the GCC project
for many years. You might be able to get support for it from a vendor
who provided you with that version.

> first deconstructs the static member variables of class A, and then
> deconstructs a global object of class A. This caused an error in my program.
> Could you tell me how can I avoid this problem?Upgrade compiler
> version?Modify my code?
>
> my code like this:
> class A{
> static int var;
> ~A();                       //A  Destructor depended var
> };
>
> int A::var;
> A       obj;
>
> var deconstructs before obj

That's not what I see using g++ 4.8.5 on CentOS 6. With the following code:

struct M { ~M() { __builtin_puts("~M"); } };
struct A{
static M var;
~A() { __builtin_puts("~A"); }
};

M A::var;
A       obj;

int main()
{
}

I get this output:

~A
~M

N.B. I had to change your int to a class type, because int has no
destructor anyway, so doesn't get destroyed. So your example does not
demonstrate the problem you're describing.

If the definitions of A::var and obj are in the same source file then
the destructors should be in the reverse order of their definitions.
If they are in separate source files, then their initialization order
is unspecified, and so the destruction order is also unspecified. If
you define them in the same file, it should work correctly.

Reply via email to