C: free() invalid pointer; without changing address - c

I'm using C (not C++) and getting the following error:
Error in './c_rk4': free(): invalid pointer: 0x0000000000a911c0
I was able to trace the error back to the lines (1) and (2). The error in (1) does not occur, if I comment out the line I marked. All other free() usages do not produce errors and the program runs as wished if I comment out all lines with free().
I checked with lines like printf("%p\n", y_n); that the address of y_n is the same after malloc() and before free() (and exactly the address in the error message).
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
void odesolver_rk4 (void (*)(double, _Complex double *, _Complex double **),
int, double,
int, _Complex double *, _Complex double ***);
void testfunc (double, _Complex double *, _Complex double **);
int main (int argc, char *argv[argc]) {
const int t_num = 300;
const int y_num = 2;
_Complex double **y_res;
y_res = malloc(t_num*sizeof(_Complex double *));
for (int i = 0; i < t_num; i++)
y_res[i] = malloc(y_num*sizeof(_Complex double));
odesolver_rk4(testfunc, t_num, 20.0, y_num, (_Complex double []){1.0, 0.0}, &y_res);
for (int i = 0; i < t_num; i++) {
for (int j = 0; j <= y_num; j++) {
printf("%f %f ", creal(y_res[i][j]), cimag(y_res[i][j]));
}
printf("\n");
}
for (int i = 0; i < t_num; i++)
free(y_res[i]); // error (2)
free(y_res);
return 0;
}
void odesolver_rk4 (void (*func)(double, _Complex double *, _Complex double **),
int t_num, double t_end,
int y_num, _Complex double *y_start, _Complex double ***y_res) {
double t_step = t_end/t_num;
double t_n = 0;
_Complex double *y_n, *y_A, *y_B, *y_C;
_Complex double *dy_n, *dy_A, *dy_B, *dy_C;
y_n = malloc(y_num*sizeof(_Complex double));
y_A = malloc(y_num*sizeof(_Complex double));
y_B = malloc(y_num*sizeof(_Complex double));
y_C = malloc(y_num*sizeof(_Complex double));
dy_n = malloc(y_num*sizeof(_Complex double));
dy_A = malloc(y_num*sizeof(_Complex double));
dy_B = malloc(y_num*sizeof(_Complex double));
dy_C = malloc(y_num*sizeof(_Complex double));
for (int j = 0; j < y_num; j++)
y_n[j] = y_start[j];
(*y_res)[0][0] = t_n;
for (int j = 0; j < y_num; j++)
(*y_res)[0][j + 1] = y_start[j];
for (int i = 1; i < t_num; i++) {
func(t_n, y_n, &dy_n);
for (int j = 0; j < y_num; j++)
y_A[j] = y_n[j] + dy_n[j]*t_step/2;
func(t_n + t_step/2, y_A, &dy_A);
for (int j = 0; j < y_num; j++)
y_B[j] = y_n[j] + dy_A[j]*t_step/2;
func(t_n + t_step/2, y_B, &dy_B);
for (int j = 0; j < y_num; j++)
y_C[j] = y_n[j] + dy_B[j]*t_step;
func(t_n + t_step, y_C, &dy_C);
for (int j = 0; j < y_num; j++) {
y_n[j] += t_step/6*(dy_n[j] + 2*(dy_A[j] + dy_B[j]) + dy_C[j]);
(*y_res)[i][0] = t_n;
(*y_res)[i][j + 1] = y_n[j]; // something goes wrong here for (1)
}
t_n += t_step;
}
free(y_n); // error (1)
free(y_A);
free(y_B);
free(y_C);
free(dy_n);
free(dy_A);
free(dy_B);
free(dy_C);
}
void testfunc (double t, _Complex double *y, _Complex double **dy) {
(*dy)[0] = -y[1];
(*dy)[1] = y[0];
}

The problem is this loop:
for (int j = 0; j < y_num; j++)
and this line of code:
(*y_res)[i][j + 1] = y_n[j]
Either your loop needs to be j < y_num - 1 or your expression should use [j]. Otherwise, you are stepping past the end of the y_res array. When you do that, you are overwriting the malloc header for the y_n allocation which is why free() later complains that it is an invalid pointer.
By the way, valgrind is a really good tool for finding these kinds of problems.
http://valgrind.org
Also recommended from the comments is AddressSanitizer:
http://clang.llvm.org/docs/AddressSanitizer.html

Related

a value of type "void *" cannot be assigned to an entity of type "int **" last

I'm making a program which dynamically creating 2d array.but it's showing the error which I mentioned on the title. I'm using Visual Studio 2015.
// last.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <time.h>
#include "stdlib.h"
double selectionSort(int * number, int number_count);
void print2d(int ** array, int rows, int cols);
void twodarray();
void main(int argc, char* argv[])
{
int num_count = 10000;
int num[10000];
for (int i = 0; i < num_count; i++)
{
num[i] = rand();
}
double sortTime = selectionSort(num, num_count);
printf("Total Runtime is: %.0f milliseconds. \n", sortTime * 1000);
twodarray();
getchar();
}
double selectionSort(int * number, int number_count)
{
clock_t start, end;
double duration;
int min;
start = clock();
for (int i = 0; i < number_count - 1; i++)
{
min = i;
for (int j = i + 1; j < number_count; j++)
{
if (number[min] > number[j])
{
min = j;
}
}
if (min != i)
{
int temp = number[min];
number[min] = number[i];
number[i] = temp;
}
}
end = clock();
return duration = (double)(end - start) / CLOCKS_PER_SEC;
}
void print2d(int ** array, int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0, j < cols; j++;)
{
printf("%10d ", array[i][j]);
}
puts("");
}
}
void twodarray()
{
int **twod;
int rows = 10;
twod = malloc(rows * sizeof(int));
int i,cols = 10;
for (i = 0; i < rows; i++)
{
twod[i] = malloc(cols*sizeof(int));
print2d(twod, rows, cols);
}
for (i = 0; rows; i++)
{
free(twod[i]);
free(twod);
}
}
In c++ you need to cast when assigining a void * pointer to another type of pointer. But in c++ you should not use malloc(), instead use
int **twod = new int *[rows];
If you didn't mean to write a c++ program, rename the file. Change the extension from .cpp to .c.
Your allocation is wrong too, as pointed out by #KeineLust here.
This is wrong:
int **twod;
int rows = 10;
twod = malloc(rows * sizeof(int));
You need to reserve space for n pointers to int, not for n ints, change to
twod = malloc(rows * sizeof(int *));
And here:
for (j = 0, j < cols; j++;)
^ ^
Use a semicolon instead of a comma and also remove the last semicolon.
Another problem:
for (i = 0; rows; i++)
{
free(twod[i]);
free(twod); /* Don't free twod in the loop, one malloc -> one free */
}
And as pointed out by Nicat and Iharob, it seems that you are mixing C and C++, use the proper extension (.c)

c allocating space for a 2d array of pointers in a loop

I have a rather large program that requires me to use pointers to 2 dimensional arrays. I'm having a difficult time allocating space for the arrays.
I've already tried allocating the space at the time of declaration but I ran into a million road blocks.
I found code here: Create a pointer to two-dimensional array
I have a 2D array of pointers to an array other_arrays
I have this:
static double other_arrays[51][1];
double (*SumH)[51][1] = &other_arrays;
double (*WeightIH)[51][1] = &other_arrays;
double (*Hidden)[51][1] = &other_arrays;
double (*SumO)[51][1] = &other_arrays;
double (*WeightHO)[51][1] = &other_arrays;
double (*Output)[51][1] = &other_arrays;
double (*DeltaWeightIH)[51][1] = &other_arrays;
double (*DeltaWeightHO)[51][1] = &other_arrays;
for (i = 0; i < 2; i++){
for (j = 0; j < 51; j++){
SumH[j][i] = (double *)malloc(sizeof(double));
WeightIH[j][i] = (double *)malloc(sizeof(double));
Hidden[j][i] = (double *)malloc(sizeof(double));
SumO[j][i] = (double *)malloc(sizeof(double));
WeightHO[j][i] = (double *)malloc(sizeof(double));
Output[j][i] = (double *)malloc(sizeof(double));
DeltaWeightIH[j][i] = (double *)malloc(sizeof(double));
DeltaWeightHO[j][i] = (double *)malloc(sizeof(double));
}
}
When I compile I get: error: array type 'double [1]' is not assignable
I've tried some things I've found online such as SumH[j] = (double *)malloc(sizeof(double));
But then I get: error: array type 'double [51][1]' is not assignable
Or something like this yields the same error:
for (j = 0; j < 51; j++){
SumH[j] = (double *)malloc(sizeof(double));
WeightIH[j] = (double *)malloc(sizeof(double));
Hidden[j] = (double *)malloc(sizeof(double));
SumO[j] = (double *)malloc(sizeof(double));
WeightHO[j] = (double *)malloc(sizeof(double));
Output[j] = (double *)malloc(sizeof(double));
DeltaWeightIH[j] = (double *)malloc(sizeof(double));
DeltaWeightHO[j] = (double *)malloc(sizeof(double));
}
SOLUTION
I didn't cast malloc
*SumH[j][i] = *(double *)malloc(sizeof(double));
If you want pointers to arrays and want to initialize them, then you should be doing things much more simply:
double (*SumH)[5][2] = malloc(sizeof(*SumH));
double (*WeightIH)[5][2] = malloc(sizeof(*WeightIH));
Now you can use:
int k = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 2; j++)
{
(*SumH)[i][j] = k;
(*WeightIH)[i][j] = k++;
}
}
Note that an array dimension of [1] is close to pointless.
FWIW, valgrind gives the following code a clean bill of health:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double (*SumH)[5][2] = malloc(sizeof(*SumH));
double (*WeightIH)[5][2] = malloc(sizeof(*WeightIH));
int k = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 2; j++)
{
(*SumH)[i][j] = k;
(*WeightIH)[i][j] = k++;
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 2; j++)
printf("[%f, %f]", (*SumH)[i][j], (*WeightIH)[i][j]);
putchar('\n');
}
free(SumH);
free(WeightIH);
return 0;
}
The output is not very exciting:
[0.000000, 0.000000][1.000000, 1.000000]
[2.000000, 2.000000][3.000000, 3.000000]
[4.000000, 4.000000][5.000000, 5.000000]
[6.000000, 6.000000][7.000000, 7.000000]
[8.000000, 8.000000][9.000000, 9.000000]
If you want to allocate 2d arrays:
/* allocate 2d arrays */
double (*SumH)[51][1] = malloc(51*1*sizeof(double));
double (*WeightIH)[51][1] = malloc(51*1*sizeof(double));
double (*Hidden)[51][1] = malloc(51*1*sizeof(double));
double (*SumO)[51][1] = malloc(51*1*sizeof(double));
double (*WeightHO)[51][1] = malloc(51*1*sizeof(double));
double (*Output)[51][1] = malloc(51*1*sizeof(double));
double (*DeltaWeightIH)[51][1] = malloc(51*1*sizeof(double));
double (*DeltaWeightHO)[51][1] = malloc(51*1*sizeof(double));
/* ... */
free(DeltaWeightHO);
free(DeltaWeightIH);
free(Output);
free(WeightHO);
free(SumO);
free(Hidden);
free(WeightIH);
free(SumH);
Usage would be ... (*SumH)[j][i] ...
I doubt this is want you wanted.
To allocate pointers to the first row of 2d arrays:
/* allocate pointers to first row of 2d arrays */
double (*SumH)[1] = malloc(51*1*sizeof(double));
double (*WeightIH)[1] = malloc(51*1*sizeof(double));
double (*Hidden)[1] = malloc(51*1*sizeof(double));
double (*SumO)[1] = malloc(51*1*sizeof(double));
double (*WeightHO)[1] = malloc(51*1*sizeof(double));
double (*Output)[1] = malloc(51*1*sizeof(double));
double (*DeltaWeightIH)[1] = malloc(51*1*sizeof(double));
double (*DeltaWeightHO)[1] = malloc(51*1*sizeof(double));
Usage would be ... SumH[j][i] ...
To allocate array of pointers to rows
/* allocate array of pointers to rows */
double (*SumH[51])[1];
for(i = 0; i < sizeof(SumH)/sizeof(SumH[0]); i++)
SumH[i] = malloc(1*sizeof(double));
Usage would be ... SumH[j][i] ...

Access violation writing location 0x011CF000

I'm trying to get monochrome image from .bmp image with using bitmap_image.hpp library. But in one place (Pic[i][j] = 0.3 * r + 0.59 * g + 0.11 * b;) i receive that error: Unhandled exception at 0x0019BD8F in PicCircle.exe: 0xC0000005: Access violation writing location 0x011CF000. . So, that's wrong with it?
code:
#define _SCL_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "bitmap_image.hpp"
#define C 0.01
double** ArrayCreate ( int M, int N )
{
int i;
double** ArrayRoot;
ArrayRoot = (double **)malloc(sizeof(double*) * M);
for (i = 0; i < M; i++)
ArrayRoot[i] = (double *)malloc(sizeof(double) * N);
return ArrayRoot;
}
void ArrayDestroy ( double** Array , int M)
{
int i;
for (i = 0; i < M; i++){
Array[i] = (double *)realloc(Array[i], 0);
};
Array = (double **)realloc(Array, 0);
}
void main ( void )
{
double** Pic;
unsigned char r, g, b;
int H, W, i, j;
bitmap_image image("m1.bmp");
H = image.height();
W = image.width();
Pic = ArrayCreate(H, W);
for (i = 0; i < W; i++)
for (j = 0; j < H; j++)
{
image.get_pixel(i, j, r, g, b);
Pic[i][j] = 0.3 * r + 0.59 * g + 0.11 * b;
}
for (i = 0; i < W; i++)
for (j = 0; j < H; j++)
{
if (abs(sqrt(pow(Pic[i + 1][j] - Pic[i][j], 2) + pow(Pic[i][j + 1] - Pic[i][j], 2))) >= C)
Pic[i][j] = 1;
else
Pic[i][j] = 0;
}
ArrayDestroy(Pic, H);
}
In your first loop you access the Pic array as Pic[width][height], but in the second loop you access it as Pic[height][width].
One of those two is incorrect, probably the first one.
Fixing your for loop should correct the issue.
This:
ArrayRoot = (double **)malloc(sizeof(int*) * M);
looks super-broken; it assumes sizeof (int *) to be the same as (sizeof double *) which is probably true, but still a very broken thing to write.
The follow-up is worse:
ArrayRoot[i] = (double *)malloc(sizeof(int) * N);
since sizeof (int) is very probably smaller than sizeof (double) this is going to lead to horror.
The way to avoid this category of error is to never write the type name in the malloc() argument: dereference the pointer being assigned to, instead. The latter would then become:
ArrayRoot[i] = malloc(N * sizeof *ArrayRoot[i]);
^^^^^^^^^^^^^
this part is to
the left of the =
This also drops the cast of course.

cast error and invalid conversion error

error: cast from 'void*' to 'unsigned int' loses precision
error: invalid conversion from 'unsigned int' to 'unsigned int**'
can u tell me how to properly cast this, i am getting error on this line:
color = (unsigned int)malloc(height*sizeof(unsigned int));
inside the main function.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsigned int width;
unsigned int height;
unsigned int **color = NULL;
bool file_write()
{
FILE *fractal = fopen("mandelbrot_imageSequential.ppm","w+");
if(fractal != NULL)
{
fprintf(fractal,"P6\n");
fprintf(fractal,"# %s\n", "Mandelbrot_imageSequential.ppm");
fprintf(fractal,"%d %d\n", height, width);
fprintf(fractal,"40\n");
int x = 0, y = 0;
unsigned int R = 0, G = 0, B = 0;
for(x = 0; x < width; ++x)
{
for(y = 0; y < height; ++y)
{
R = (color[y][x]*10);
G = 255-((color[y][x]*10));
B = ((color[y][x]*10)-150);
if(R == 10) R = 11;
if(G == 10) G = 11;
if(B == 10) B = 11;
putc(R, fractal);
putc(G, fractal);
putc(B, fractal);
}
}
fclose(fractal);
}
return true;
}
int method(int x, int y, double min_re, double max_re, double min_im, double max_im, int max_iterations)
{
double threshold = 4;
double x_factor = (max_re-min_re)/(width-1);
double y_factor = (max_im-min_im)/(height-1);
double c_im = max_im - y*y_factor;
double c_re = min_re + x*x_factor;
double Z_re = c_re, Z_im = c_im;
unsigned int col = 0;
for(unsigned n = 0; n < max_iterations; ++n)
{
double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
if(Z_re2 + Z_im2 > threshold)
{
col = n;
break;
}
Z_im = 2 * Z_re * Z_im + c_im;
Z_re = Z_re2 - Z_im2 + c_re;
}
return col;
}
void method1(double min_re, double max_re, double min_im, double max_im, int max_iterations)
{
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; ++y)
{
int m1 = method(x,y,min_re,max_re,min_im,max_im,max_iterations);
if(m1)
{
color[x][y] = m1*50;
}
}
}
}
int main(int argc, char *argv[])
{
unsigned int max_iterations;
int x,y;
double threshold;
double min_re;
double max_re;
double min_im;
double max_im;
unsigned int NUM_OF_THREADS;
if(argc != 10)
{
printf("There is an error in the input given.\n");
return 0;
}
else
{
height = atoi(argv[1]);
width = atoi(argv[2]);
max_iterations = atoi(argv[3]);
min_re = atof(argv[4]);
max_re = atof(argv[5]);
min_im = atof(argv[6]);
max_im = atof(argv[7]);
threshold = atoi(argv[8]);
NUM_OF_THREADS = atoi(argv[9]);
}
color = (unsigned int)malloc(height*sizeof(unsigned int));
printf("height = %d\twidth = %d\tmaximum_iterations = %d\tminimum_x-value = %.2f\tmaximum_x-value = %.2f\tminimum_y-value = %.2f\tmaximum_y-value = %.2f\tthreshold_value = %.2f\tno. of threads = %d\t\n",height,width,max_iterations,min_re,max_re,min_im,max_im,threshold,NUM_OF_THREADS);
for(x = 0; x < height; x++)
{
color[x] = (unsigned int*)malloc(width*sizeof(unsigned int));
}
time_t ts,te;
time(&ts);
method1(min_re, max_re, min_im, max_im, max_iterations);
time(&te);
double diff = difftime(te,ts);
file_write();
printf("Total Time elapsed: %f\n",diff);
return 0;
}
Why are you casting the return value of malloc to an unsigned int?
First off, don't cast the return value of malloc in C. It is pointless and can actually hide the fact that you forgot to include . C is not C++ in this regard. A void* can be implicitly converted to any pointer type in C.
Secondly, malloc returns a pointer, and you have defined color as an unsigned int**... yet you attempt to assign an unsigned int as well as an unsigned int* to it. Obviously those are incompatible. Just drop the casts and use/declare the type properly.
color = (unsigned int**)malloc(height*sizeof(unsigned int*));
Shouldn't it be this?
You are trying to allocate array of pointers dynamically. So what you need to do is the following:
color = (unsigned int**)malloc(height*sizeof(unsigned int));
Rest of it is fine ...

Memory allocation issue with multidimensional array

#define MAXROWS 88
#define MAXSTATES 10
#define MAXPROBS 6
int obs[MAXROWS]= {0,5,2,3,0,5,2,3,2,4,0,3,5,1,4,3,1,5,2,0,4,4,1,5,3,3,1,4,0,5,1,2,3,0,2,0,5,2,0, 4,4,5,3,0,5,2,5,1,5,4,0,3,1,4,5,2,3,5,1,5,2,4,5,1,5,4,2,5,0,3,4,1,5,2,4,1,5,0,4,2,3,0,5,1,5,2,4,1};//{2,1,0} ;
int q[MAXROWS]= {1};
int s=MAXROWS, i=1,j=0;
double **A;
double **B;
double AD[MAXSTATES][MAXSTATES]={{0,1,0,0,0,0,0,0,0,0},{0,0,1,0,0,0,0,0,0,0},{0,0,0,1,0,0,0,0,0,0},{0,0,0,0,1,0,0,0,0,0},{0.8,0,0,0,0,0.2,0,0,0,0},{0,0,0,0,0,0,1,0,0,0},{0,0,0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,0,1,0},{0.2,0,0,0,0,0.8,0,0,0,0}};//{{.6,.4},{.3,.7}};//{ { .500, .375, .125 }, { .250,.125, .625 }, { .250,.375,.375 } };
double BD[MAXSTATES][MAXPROBS]={{.167,.167,.167,.167,.167,.167},{.167,.167,.167,.167,.167,.167},{.167,.167,.167,.167,.167,.167},{.167,.167,.167,.167,.167,.167},{.167,.167,.167,.167,.167,.167},{.4,.1125,.1125,.1125,.1125,.15},{.4,.1125,.1125,.1125,.1125,.15},{.4,.1125,.1125,.1125,.1125,.15},{.4,.1125,.1125,.1125,.1125,.15},{.4,.1125,.1125,.1125,.1125,.15}};//{{.1,.3,.6},{.5,.4,.1}};//{ { .60, .20 ,.15, .05}, { .25, .25, .25, .25 }, { .05,.10,.35,.50 } };
double *pi;
double pi2[MAXSTATES] = {1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};//{.4,.6};
double *poolA;
double *curPtrA;
double *poolB;
double *curPtrB;
double pproba=0;
double **delta;
double *pool;
double *curPtr;
int **psi;
int *poolpsi;
int *curPtrpsi;
HMM model;
A = (double** )calloc(MAXSTATES, sizeof(double* ));
poolA = (double *)calloc( MAXSTATES * MAXSTATES, sizeof(double));
curPtrA = poolA;
for( i = 0; i < MAXSTATES; i++)
{
*(A + i) = curPtrA;
curPtrA += MAXSTATES;
}
B = (double** )calloc(MAXSTATES, sizeof(double* ));
poolB = (double *)calloc( MAXSTATES * MAXPROBS, sizeof(double));
curPtrB = poolB;
for( i = 0; i < MAXSTATES; i++)
{
*(B + i) = curPtrB;
curPtrB += MAXPROBS;
}
for(i = 0; i <MAXSTATES; i++)
for(j=0; j< MAXPROBS; j++)
B[i][j] = BD[i][j];
for(i = 0; i < MAXSTATES; i++)
for(j=0; j< MAXSTATES; j++)
A[i][j] = AD[i][j];
pi = (double* )calloc(MAXSTATES, sizeof(double* ));
for(i = 0; i <MAXSTATES; i++)
pi[i] = pi2[i];
model.M=MAXPROBS;
model.N=MAXSTATES;
model.A= A;
model.B = B;
model.pi = pi;
//double delta[6][4];
psi = (int** )calloc(MAXROWS, sizeof(int* ));
poolpsi = (int *)calloc( MAXROWS*MAXSTATES, sizeof(int));
curPtrpsi = poolpsi;
for( i = 0; i < MAXROWS; i++)
{
*(psi + i) = curPtrpsi;
curPtrpsi += MAXSTATES;
}
I start getting error on psi = (int **) ... line about heaps which is like this:
"Windows has triggered a breakpoint in TestProj.exe.
This may be due to a corruption of the heap, which indicates a bug in TestProj.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while TestProj.exe has focus.
The output window may have more diagnostic information."
pi = (double* )calloc(MAXSTATES, sizeof(double* ));
i think you should have sizeof(double) not pointer since its an array of double values.
Allocating memory as follows avoids the need to repeat the name of the type all over the place, and thus you won't mismatch it.
double* pi = calloc(MAXSTATES, sizeof(*pi));
int **psi , i;
psi = malloc( MAXROWS * sizeof(int*));
for( i = 0 ; i < MAXROWS ; i++ )
{
psi[i] = calloc (MAXSTATES, sizeof(int));
}
Try to do that and a little check if psi is not null could be great ^^.

Resources