遺伝的アルゴリズムのコードを書いていますが、未使用のメモリを解放できない状態で立ち往生しています。これは私のmain()コードです:
szChromosomes = initial_population(&data[0]);
while (iCurrentGen <= data->m_iMaxGenerations)
{
arrfSelectedChromosomes = selection(&data[0], szChromosomes);
iSelectedLen = order_descending_grid(arrfSelectedChromosomes);
szAuxGen = crossover(&data[0], arrfSelectedChromosomes, szChromosomes);
free_generation(&data[0], szChromosomes);//Error line
szChromosomes = szAuxGen;
szAuxGen = NULL;
}
initial_population(&data [0])は、次のようにszChromosomes配列(後で解放しようとします)を作成します。
char** initial_population(struct INPUT_DATA* d)
{
int i, j = 0;
float fMember = 0.0;
char** szChromosomes = (char**)malloc(d->m_iPopulationSize * sizeof(char*));
srand(time(NULL));
for (i = 0; i < d->m_iPopulationSize; ++i)
{
szChromosomes[i] = (char*)malloc(d->m_iBitsPChromosome * sizeof(char));
for (j = 0; j < d->m_iBitsPChromosome; ++j)
{
szChromosomes[i][j] = rand_1_0(0.0, 1.0) == 1? '1' : '0';
}
szChromosomes[i][j] = '\0';
}
return szChromosomes;
}
free_generation関数を呼び出すと、以下のForループが実行されます。
int i;
for (i = 0; i < d->m_iPopulationSize; ++i)
{
free(szChromosomes[i]);
}
free(szChromosomes);
szChromosomes = NULL;
free(szChromosomes [i]);への最初の呼び出し時。が発生すると、次のエラーが発生します。
検出されたヒープ破損:通常のブロック後(#99)。CRTは、ヒープバッファの終了後にアプリケーションがメモリに書き込んだことを検出しました。