Destructors of global variables are not called

未關閉
#379 0 則留言 1 個 reaction 已指派 2 人 在 GitHub 檢視

維護者通常 1 天內回覆

@nunoplopes 已經在處理了。

開始於 2026年10月8日。

  • #497 來自 @copilot-swe-agent —— 已關閉,未合併

評估

這個 Issue 還沒有評估資料。

描述

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.
主要語言
Rust
星號
310
分支
18
平均合併
10 小時 18 分鐘
30 天內合併 PR
125

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

Cpp2Rust/cpp2rust 的其他 Issue

查看 Cpp2Rust/cpp2rust 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。