I have a heap corruption and can't find what the reason. Please, can you help? I have some pieces of code where to my mind the error is located. The heap corruption is generated here (see comments below):
 free(rowPermutation);
 fclose(wFile);
So, memory allocation is here:
static int N = 2,**orderOfRows, *columnsPermutation,*tmpRowPermutation,*resultPermutation,
    *u,*v,**sourceMatrix,**patternMatrix,**auxMatrix1,*incidence,*perm;
 static FILE *wFile,*file,*patternFile;
 void allocate2dMemory() {
    int i = 0;
    sourceMatrix = (int**) malloc(N * sizeof(int *));
    auxMatrix1= (int**) malloc(N * sizeof(int *));
    orderOfRows = (int**) malloc(N * sizeof(int*));
    patternMatrix = (int**) malloc(N * sizeof(int*));
    incidence = (int*) malloc(N * sizeof(int));
    columnsPermutation = (int*) malloc(N * sizeof(int));
    tmpRowPermutation = (int*) malloc(N * sizeof(int));
    resultPermutation = (int*) malloc(N * sizeof(int));
    perm = (int*)malloc(N * sizeof(int));
    u = (int*) malloc(N * sizeof(int));
    v = (int*) malloc(N * sizeof(int));
    if ((sourceMatrix == NULL) || (auxMatrix1 == NULL) || (incidence == NULL) || (orderOfRows == NULL) || 
            (columnsPermutation == NULL) || (tmpRowPermutation == NULL) || (u == NULL) || (v == NULL) || (resultPermutation == NULL)) {
            fprintf(stderr, "out of memory\n");
            exit(2);
    }
    for (i = 0; i < N; i++) {
            sourceMatrix[i] = (int*) malloc(N * sizeof(int));
            auxMatrix1[i] = (int*) malloc(N * sizeof(int));
            patternMatrix[i] = (int*) malloc(N * sizeof(int));
            incidence[i] = 0;
            if ((sourceMatrix[i] == NULL) || (auxMatrix1[i] == NULL) || (patternMatrix[i] == NULL) ) {
                            fprintf(stderr, "out of memory\n");
                            exit(2);
            }
    }
}
Open files:
 void openFile(char* filename) {
    if ((file = fopen(filename, "r")) == NULL) {
            perror("Open error");
            printf("\nPress any key for exit\n\n");
            getch();
            exit(1);
    }
    if ((patternFile = fopen("pattern.dat", "r")) == NULL) {
            perror("Open error");
            printf("\nPress any key for exit\n\n");
            getch();
            exit(1);
    }
    if ((wFile = fopen("out.txt", "w")) == NULL) {
            perror("Open error");
            printf("\nPress any key for exit\n\n");
            getch();
            exit(1);
    }
Then I close some of them ("wFile" is the file for writing):
  fclose(file);
  fclose(patternFile);
Change rows order:
  void changeRowOrder(int *computation,int **matr) {
    fprintf(wFile,"Make row permutation\n");
    int i,j;
    for (i = 0; i < N; ++i) {
            orderOfRows[computation[i]] = matr[i];
    }
    fputs("\n",wFile);
}
Change the order of columns:
   int **destMatrix = (int**) malloc(N * sizeof(int *));
    if ((destMatrix == NULL)) {
            fprintf(stderr, "out of memory\n");
            exit(2);
    }
    for (i = 0; i < N; i++) {
            destMatrix[i] = (int*) malloc(N * sizeof(int));
            if (destMatrix[i] == NULL) {
                    fprintf(stderr, "out of memory\n");
                    exit(2);
            }
    }
    for(i = 0; i < N; ++i) {
            // save permutation
            resultPermutation[perm[i]] = i;
            for(j = 0; j < N; ++j) {
                    destMatrix[i][j] = orderOfRows[i][perm[j]];
            }
    }
    fprintf(wFile,"Now result permutation is: \n");
    printArray(resultPermutation);
    for(i = 0; i < N; ++i) {
            free(sourceMatrix[i]);
    }
    free(sourceMatrix);
    sourceMatrix = destMatrix;
I need static pointers to see them in all my programm. Here is located another code where an error can exist:
The code below is at the beginning of the programm.
    int res,i,j;
    char c[25],f[25],g;
    int *rowPermutation = (int*)malloc(N*sizeof(int));
    openFile("inb.dat");
    fscanf(file,"%s %s %d %d",&c,&f,&N,&i);
    allocate2dMemory();
    getMaxtrix();
    // and so on ...
     free(rowPermutation);
    fclose(wFile);
I don't allocate memory in other place of my programm. I have noticed that memory is corrupted in the "columnsPermutation" array. First, i copy some elements, then the element begin to change. I have noticed it when I used STL container to fix heap corruption (just to know why the arrays differ).
Please,can you find an error? To my mind, I allocate the memory correctly.

 
     
     
    