我的 VS2012 编译器有一个奇怪的问题,似乎没有出现在 GCC 中。解除分配过程最终需要几分钟而不是几秒钟。有人对此有任何意见吗?步骤调试显示调用 RtlpCollectFreeBlocks() 时出现明显的挂起。我在调试和发布模式下都有这个问题。我正在运行 Windows 7 32 位,但我在 64 位 7 上遇到了同样的问题。
#include "stdafx.h"
#include <iostream>
#include <stdint.h>
#include <cstdlib>
#define SIZE 500000
using namespace std;
typedef struct
{
uint32_t* thing1;
}collection;
/*
* VS2012 compiler used.
* Scenarios:
* 1) Don't allocate thing1. Program runs poorly.
* 2) Allocate thing1 but don't delete it. Program runs awesome.
* 3) Allocate thing1 and delete it. Program runs poorly.
*
* Debug or Release mode does not affect outcome. GCC's compiler is fine.
*/
int _tmain(int argc, _TCHAR* argv[])
{
collection ** colArray = new collection*[SIZE];
for(int i=0;i<SIZE;i++)
{
collection * mine = new collection;
mine->thing1 = new uint32_t; // Allocating without freeing runs fine. Either A) don't allocate or B) allocate and delete to make it run slow.
colArray[i] = mine;
}
cout<<"Done with assignment\n";
for(int i=0;i<SIZE;i++)
{
delete(colArray[i]->thing1); // delete makes it run poorly.
delete(colArray[i]);
if(i > 0 && i%100000 == 0)
{
cout<<"100 thousand deleted\n";
}
}
delete [] colArray;
cout << "Done!\n";
int x;
cin>>x;
}