Issue with mkl_free()

Issue with mkl_free()

Hi,

I am writing an image registration program and I find that a strange error when using mkl_free(). I wrote an implementation of the matrix exponential function - expm() - from MATLAB using MKL functions. I have tested it individually by itself and it works without running into any errors.

The code can be found here: https://github.com/poliu2s/MKL/blob/master/matrix_exponential.cpp

The problem occurs when I call it from other functions who need it for calculation. My program currently crashes right before I call the first mkl_free() so I suspect it is some form of memory leak. But shouldn't matrices used within the function be self contained? I do not see how freeing them would cause my entire program to crash.

Calls to mkl_free_buffers(); and mkl_thread_free_buffers(); produce no effect that I can observe. I have tried turning off the Intel Memory Manager with mkl_disable_fast_mm() at the cost of speed at the beginning of the program's execution but also to no avail.

I have checked the inputs going into the function itself and the matrix is perfectly valid - just like the ones I used during testing. 

Does anyone have any suggestions as to why my program crashes when I call mkl_free(array)?

12 posts / novo 0
Último post
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.

the code looks fine. I tried to call these routines from another funstion. I don't see problem. regard to memory leaks - the last leaks problem has been fixed in the last 11.0 u5 released resently.

Thanks for responding Gennady.

I was able call the function from another function as well. However, when I implemented a new optimization algorithm which required this function, it started crashing on the second iteration at mkl_free().

I know this may be difficult for others to reproduce, but my collaborator also had a similar issue. Do you have any suggestions on what I could do to resolve this problem?

>>...I do not see how freeing them would cause my entire program to crash...
>>
>>...it started crashing on the second iteration at mkl_free()...

Here are two generic examples to simulate your problem ( replace malloc and free with MKL functions ):

[ Test 1 ]
...
char *pChar = ( char * )malloc( sizeof( char ) * 1 );
...
// Do something else
...
free( pChar );
...
free( pChar ); <= Could crash here
...

[ Test 2 ]
...
char *pChar = ( char * )malloc( sizeof( char ) * 1 );
...
// Do something else
...
if( pChar != NULL )
{
free( pChar );
pChar = NULL;
}
...
// Do something else
...
if( pChar != NULL )
{
free( pChar ); // These two lines of code won't be executed since pChar is already NULL
pChar = NULL;
}
...

actually he has already used mkl_malloc 

double* A = (double*)mkl_malloc(4 * 4 * sizeof(double), 64);

double* B = (double*)mkl_malloc(4 * 4 * sizeof(double), 64);

Hi Po,

The return pointer (return m_exp ) in the funtion matrix_exponential() looks be incorrect memory operation.

as you see, before call the function,  you have

B=malloc() // B point a block of memory, let's say M1

and  m_exp=malloc()  in function internally.   // m_exp point another block of memory, let's M2

 Then return pointer m_exp. 

 B=m_exp.  // move B to the memory pointed by m_exp.  B-> M2

Thus even if you release B,  the memory malloced by B at beginning (M1) is leaked (or B become dangling pointer). 

I guess, this prolem invoke other memory problem. (include crash later when second call) some time.

Best Regards,
Ying

upps, thanks Ying. i have overlooked this issue.

Thanks so much for everyone's help, I appreciate all the comments.

I fixed the memory leak error that Ying mentioned in his post. My new is code is still at: https://github.com/poliu2s/MKL/blob/master/matrix_exponential.cpp.

However, the error with using mkl_free() still exists. I have pinpointed that my application is crashing at mkl_free(M_power); or mkl_free(M_power1); which I have commented out on Github. The matrices I am working with contain in the range of 7500x3 elements.

Is there anything else that I could have overlooked?

Hi Po,

the new code looks fine. Just a small problem, if with the function double * matrix_exponential(double* matrix, double* result),

the compiler  will report, error C2561: 'matrix_exponential' : function must return a value

So I change it to void matrix_exponential(double* matrix, double* result) and uncomment the two line of mkl_free(). and add mkl_free(A), mkl_free(B) in main(). The code seem runs without problem.

You mentioned the matrix in your application contain 7500x4 matrix. Then what the real code looks like?

Best Regards,

Ying

 You mentioned, the matrix

Hi Ying,

Thanks for pointing out that last mistake (I was using it for illustration purposes so I didn't catch it). I have posted a portion of the code I am working with here: https://github.com/poliu2s/MKL/tree/master/project. It better describes the issue. I am running the testPoseDerivative.cpp without issue but there is a memory leak currently in MultiObjModelMKL.cpp that occurs on line 52 with m_exp2. 

If I try to mkl_free that m_exp2 matrix or comment out the line (it is unused in matrix_exponential()), it will cause the test to crash during runtime. The matrices I am using can be seen in EuclideanModel.txt file.

Thanks again for your help so far,

Po

Hi Po,

As there is only part of code, i can't test.  Do you have run memory leak check tools, like Intel Parallel Ampilifer and see if it can detect the real cause?

And what is your mkl version?  and how do you link mkl? if you are using MKL threading library, what about if try MKL sequential library?

Best Regards,

Ying 

 

Could mkl_free accept ptr = 0; as the standard free function? This information would be nice in the documentation.

Deixar um comentário

Faça login para adicionar um comentário. Não é membro? Inscreva-se hoje mesmo!