struct test_struct
{
test_struct() {}
~test_struct() {}
};
#include <vector>
#include <memory>
#include <cstdio>
int main()
{
printf("ctor begin\n");
{
std::vector<std::unique_ptr<test_struct>> test_vec;
const int count = 100000;
for (auto i = 0; i < count; i++) {
test_vec.emplace_back(new test_struct);
}
printf("dtor begin\n");
}
printf("dtor end\n");
}
私は VS2010 を使用していますが、ばかげたパフォーマンスの問題が見つかりました。上記のコードは、デバッグ ビルドとリリース ビルド (ctrl+f5) の両方でうまく機能しますが、デバッガーがアタッチされている場合 (f5)、unique_ptr クラスの dtor 呼び出しが耐えられないほど遅くなります。結果のマシン コードはかなり最適化されているため、デバッガーの問題ではなくコンパイラーの問題であるとは思いませんが、対処方法がわかりません。私の質問は
- この問題はあなたのマシンで再現できますか?
- この動作の理由は何ですか?
- 回避策はありますか?