pmatrix needs to be a triple pointer to be able to set it to NULL in the caller.
Code:
void freeMatrix(pixel *** pmatrix, size_t rows){
for (int i = 0; i < rows; ++i)
free((*pmatrix)[i]);
free(*pmatrix);
*pmatrix = NULL;
}
However, a better way to allocate a 2-d array is like this:
Code:
pixel** matrix_new(int rows, int cols) {
pixel** m = malloc(rows * sizeof *m); // allocate row pointers
m[0] = calloc(rows * cols, sizeof **m); // allocate data block
for (int i = 1; i < rows; ++i) // set row pointers
m[i] = m[i - 1] + cols;
return m;
}
void matrix_delete(pixel*** m) {
free((*m)[0]); // delete data block
free(*m); // delete row pointers
*m = NULL;
}