Destructors of global variables are not called

Aperta
#379 0 commenti 1 reazione 2 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@nunoplopes ci sta già lavorando.

Dal 8/10/2026.

  • #497 di @copilot-swe-agent — chiusa senza merge

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

bug
#include <iostream>

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.
Lingua principale
Rust
Stelle
310
Fork
18
Merge medio
10h 18m
PR unite (30g)
125

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Cpp2Rust/cpp2rust

Tutte le issue di Cpp2Rust/cpp2rust

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.