Skip to content

[WIP] Fix destructors of global variables not being called - #497

Closed
nunoplopes with Copilot wants to merge 1 commit into
masterfrom
copilot/fix-global-variable-destructors
Closed

nunoplopes with Copilot wants to merge 1 commit into
masterfrom
copilot/fix-global-variable-destructors

Conversation

Copilot AI commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Thanks for asking me to work on this. I will get started on it and keep this PR's description up to date as I form a plan and make progress.


This section details on the original issue you should resolve

<issue_title>Destructors of global variables are not called</issue_title>
<issue_description>```c++
#include

struct SideEffectTracker {
int name;

explicit SideEffectTracker(int tag) : name(tag) {
    std::cout << "[CONSTRUCTOR] " << name << " initialized.\n";
}

~SideEffectTracker() {
    std::cout << "[DESTRUCTOR]  " << name << " destroyed.\n";
}

void touch() const {
    std::cout << "[USAGE]       " << name << " accessed.\n";
}

};

// 1. Global statics (Initialized before main() in order of declaration)
SideEffectTracker g_global1(1);
SideEffectTracker g_global2(2);

void run_local_statics() {
// 2. Function-local static (Lazy initialization: only runs on first call)
static SideEffectTracker s_local_static(3);
s_local_static.touch();
}

int main() {
std::cout << "=== Entering main() ===\n\n";

g_global1.touch();

std::cout << "\n-- First call to run_local_statics() --\n";
run_local_statics();

std::cout << "\n-- Second call to run_local_statics() --\n";
run_local_statics();

std::cout << "\n=== Exiting main() ===\n";
return 0;
// Static destructors run after this line in REVERSE order of construction

}


should print:

[CONSTRUCTOR] 1 initialized.
[CONSTRUCTOR] 2 initialized.
=== Entering main() ===

[USAGE] 1 accessed.

-- First call to run_local_statics() --
[CONSTRUCTOR] 3 initialized.
[USAGE] 3 accessed.

-- Second call to run_local_statics() --
[USAGE] 3 accessed.

=== Exiting main() ===
[DESTRUCTOR] 3 destroyed.
[DESTRUCTOR] 2 destroyed.
[DESTRUCTOR] 1 destroyed.

- Closes Cpp2Rust/cpp2rust#379

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

Copilot AI linked an issue Oct 8, 2026 that may be closed by this pull request
@nunoplopes nunoplopes closed this Oct 8, 2026
@nunoplopes
nunoplopes deleted the copilot/fix-global-variable-destructors branch October 8, 2026 18:27
Copilot stopped work on behalf of nunoplopes due to an error October 8, 2026 18:28
Copilot AI requested a review from nunoplopes October 8, 2026 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Destructors of global variables are not called

2 participants