c - Mex 内存泄漏..我需要一些帮助来发现错误

标签 c matlab pointers malloc mex

我正在尝试制作此代码的 mex 文件。 mex 文件已创建,但是当我尝试使用此 mex 文件时,我的 matlab 崩溃了,有人可以找到错误吗? 我的代码在 C 语言中运行得很好,这意味着问题出在 mex 文件创建中。

  #include "mex.h"
    void IDWT(double z[2],float X[20][4], double row,double col, double of,double nv,double S, double I[9][8])
    {    
    float *sum2;
    float **sum;
    float **sum1;
    float *v;
    float **B;
    int *indexes;
    double *idxl;
    double **idx1;
    float *dis;
    double N=8;
    double M=9;
    int i,j,k,l,t,f,g,h;
    int r=0;
    int temp=0;
    int f2=4;
    int sum22;
    int rmax,rmin,cmax,cmin;
    int q=rmax*cmax;
    float kTmp;
    rmin=((row-S)<1)?1:(row-S);
    rmax=(N<(row+S))?N:(row+S);
    cmin=((col-S)<1)?1:(col-S);
    cmax=(M<(col+S))?M:(col+S);

    sum= (float *)mxMalloc(q*f2*sizeof(float));
    sum1= (float *)mxMalloc(q*f2*sizeof(float));
    sum2= (float *)mxMalloc(q*sizeof(float));
    B= (float *)mxMalloc(q*f2*sizeof(float));   
    v=(float *)mxMalloc(f2*sizeof(float));
    idx1= (double *)mxMalloc(rmax*cmax*sizeof(double));
    idxl= (double *)mxMalloc(q*sizeof(double));
    dis= (float *)mxMalloc(q*sizeof(float));
    indexes= (int *)mxMalloc(q*sizeof(int));


    for (i = 0; i < rmax; i++){ 
        for( j = 0 ; j <cmax ; j++ ){ 
         idx1[i][j]= I[i][j]; }}

    for (i = 0; i < rmax; i++)
        { for (j = 0; j < cmax; j++)
        {idxl[i + j*rmax] = idx1[i][j];}}


    for(l=0; l<(rmax*cmax); l++)
        {r=idxl[l]-1;
        for(k=0; k<f2; k++)
        {B[l][k]=X[r][k];}}


    for (i = 0,j=(of-1); i <f2; i++)
        {v[i]  = X[j][i]; }


    for (i = 0; i<f2 ; i++) {
    for (j = 0 ; j < (rmax*cmax); j++) {    
        sum[j][i] = B[j][i] - v[i];
        sum1[j][i]= pow(sum[j][i],2); }}

    for (i = 0; i <(rmax*cmax) ; i++) {
        sum22=0;
        for (j = 0 ; j < f2; j++) {
        sum22+=sum1[i][j];}
        sum2[i]=sum22;}


    for(i = 0; i <(rmax*cmax) ; i++)
        {    dis[i]= sum2[i]/f2;}



    for( f = 0; f < rmax*cmax; f++)
        indexes[f] = f;
        for(g = 0; g < rmax*cmax; g++)
        {for(h = 0; h < rmax*cmax; h++)
        {if(dis[indexes[g]] < dis[indexes[h]])
            {kTmp = indexes[g];
                    indexes[g] = indexes[h];
                    indexes[h] = kTmp;}}}

    for (j = 0; j <2; j++)
       { temp=indexes[j]; 
        z[j]=idxl[temp];}
    for (t = 0; t <2; t++)
       { printf("%lf---\n", z[t]);}

    mxFree (sum);
    mxFree (sum1);
    mxFree(sum2);
    mxFree(B);  
    mxFree(v);
    mxFree(idx1);
    mxFree(idxl);
    mxFree(dis);
    mxFree(indexes);



    }

    void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    {

    //DECLARING ALL THE ARGUMENTS
     float *X;  double row;  double col;  double of;  double nv;  double S; double *I;
    //DOUBLE PRECISION CORRESPONDANCE OF THE OUTPUT
    double *Z;

    X = mxGetPr(prhs[0]);
    row = mxGetScalar(prhs[1]);
    col = mxGetScalar(prhs[2]);
    of = mxGetScalar(prhs[3]);
    nv = mxGetScalar(prhs[4]);
    S = mxGetScalar(prhs[5]);
    I= mxGetPr(prhs[6]);

     plhs[0] = mxCreateDoubleMatrix(2,1,mxREAL);

    Z = mxGetPr(plhs[0]);
    IDWT(Z,X,row,col, of, nv, S,  I);

    }

这是堆栈跟踪

MATLAB crash file:C:\Users\ABEERA~1\AppData\Local\Temp\matlab_crash_dump.1808-1:


------------------------------------------------------------------------
       Segmentation violation detected at Mon Jun 01 11:47:58 2015
------------------------------------------------------------------------

Configuration:
  Crash Decoding     : Disabled
  Default Encoding   : windows-1252
  MATLAB Architecture: win64
  MATLAB Root        : C:\Program Files\MATLAB\R2013a
  MATLAB Version     : 8.1.0.604 (R2013a)
  Operating System   : Microsoft Windows 8
  Processor ID       : x86 Family 6 Model 42 Stepping 7, GenuineIntel
  Virtual Machine    : Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
  Window System      : Version 6.2 (Build 9200)

Fault Count: 1


Abnormal termination:
Segmentation violation

Register State (from fault):
  RAX = 3ff0000000000000  RBX = 00000000983d2080
  RCX = 0000000000000001  RDX = 0000000000000008
  RSP = 000000000403a470  RBP = 00000000983823a0
  RSI = 0000000000000000  RDI = 0000000000000002

   R8 = 00000000983823a0   R9 = 0000000000000002
  R10 = 0000000000000000  R11 = 0000000000000000
  R12 = 0000000000000002  R13 = 000000007d9fdba0
  R14 = 0000000000000004  R15 = 0000000000000000

  RIP = 00007fff892111b3  EFL = 00010202

   CS = 0033   FS = 0053   GS = 002b

    Stack Trace (from fault):
    [  0] 0x00007fff892111b3      C:\Users\abeera tariq\Desktop\data\prp.mexw64+00004531
    [  1] 0x00007fff89211ac2      C:\Users\abeera tariq\Desktop\data\prp.mexw64+00006850 mexFunction+00000210
    [  2] 0x000000000442f731 C:\Program Files\MATLAB\R2013a\bin\win64\libmex.dll+00063281 mexRunMexFile+00000129
    [  3] 0x000000000442e202 C:\Program Files\MATLAB\R2013a\bin\win64\libmex.dll+00057858 inSwapMexfileReader+00000226
    [  4] 0x000000000442f2b4 C:\Program Files\MATLAB\R2013a\bin\win64\libmex.dll+00062132 _constructor_lm_mexmaci64+00001620
    [  5] 0x0000000074e8c64f C:\Program Files\MATLAB\R2013a\bin\win64\m_dispatcher.dll+00050767 Mfh_file::dispatch_fh+00000623
    [  6] 0x0000000074e8ced2 C:\Program Files\MATLAB\R2013a\bin\win64\m_dispatcher.dll+00052946 Mfunction_handle::dispatch+00000802
    [  7] 0x0000000004f5bc7b C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00834683 inDestroyWS+00392123
    [  8] 0x0000000004f4b091 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00766097 inDestroyWS+00323537
    [  9] 0x0000000004f3008f C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00655503 inDestroyWS+00212943
    [ 10] 0x0000000004f465a4 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00746916 inDestroyWS+00304356
    [ 11] 0x0000000004f3c3b0 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00705456 inDestroyWS+00262896
    [ 12] 0x0000000004f2f112 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00651538 inDestroyWS+00208978
    [ 13] 0x0000000004f30bd0 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00658384 inDestroyWS+00215824
    [ 14] 0x0000000004f1cf79 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00577401 inDestroyWS+00134841
    [ 15] 0x0000000074e8c64f C:\Program Files\MATLAB\R2013a\bin\win64\m_dispatcher.dll+00050767 Mfh_file::dispatch_fh+00000623
    [ 16] 0x0000000074e8ced2 C:\Program Files\MATLAB\R2013a\bin\win64\m_dispatcher.dll+00052946 Mfunction_handle::dispatch+00000802
    [ 17] 0x0000000004e9e70e C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00059150 inValidateLoadedObject+00002350
    [ 18] 0x0000000004fa6495 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+01139861 IInterpreter::IInterpreter+00000229
    [ 19] 0x0000000004faca45 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+01165893 inRegisterFunctionHandles+00004405
    [ 20] 0x0000000004faca12 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+01165842 inRegisterFunctionHandles+00004354
    [ 21] 0x0000000004fac9d7 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+01165783 inRegisterFunctionHandles+00004295
    [ 22] 0x0000000004f9ba0e C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+01096206 inGetSeqVarPtrsFromCurrentWS+00005742
    [ 23] 0x0000000004e9be89 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00048777 inEvalCmdWithLocalReturn+00000329
    [ 24] 0x0000000004e9bdcc C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00048588 inEvalCmdWithLocalReturn+00000140
    [ 25] 0x0000000004e9bd81 C:\Program Files\MATLAB\R2013a\bin\win64\m_interpreter.dll+00048513 inEvalCmdWithLocalReturn+00000065
    [ 26] 0x0000000079fbbecd C:\Program Files\MATLAB\R2013a\bin\win64\libmwbridge.dll+00048845 mnDebugPrompt+00000109
    [ 27] 0x0000000079fbd01b C:\Program Files\MATLAB\R2013a\bin\win64\libmwbridge.dll+00053275 mnParser+00000763
    [ 28] 0x000000007499daa4   C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00187044 mcrInstance::mnParser_on_interpreter_thread+00000036
    [ 29] 0x0000000074982220   C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00074272 mcr::setInterpreterThreadToCurrent+00031664
    [ 30] 0x0000000074982280   C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00074368 mcr::setInterpreterThreadToCurrent+00031760
    [ 31] 0x00000000057af9d6   C:\Program Files\MATLAB\R2013a\bin\win64\uiw.dll+00391638 UIW_IsUserMessage+00000086
    [ 32] 0x00000000057b0095   C:\Program Files\MATLAB\R2013a\bin\win64\uiw.dll+00393365 UIW_SetGLIMUserMsg+00000117
    [ 33] 0x00007fff8eeeb6f6                     C:\Windows\system32\USER32.dll+00571126 BroadcastSystemMessageExA+00000570
    [ 34] 0x00007fff8ee63a85                     C:\Windows\system32\USER32.dll+00014981 IsWindow+00001653
    [ 35] 0x00007fff8ee63af6                     C:\Windows\system32\USER32.dll+00015094 IsWindow+00001766
    [ 36] 0x00007fff8f4798ef                      C:\Windows\SYSTEM32\ntdll.dll+00628975 KiUserCallbackDispatcher+00000031
    [ 37] 0x00007fff8ee6303a                     C:\Windows\system32\USER32.dll+00012346 PeekMessageW+00000202
    [ 38] 0x00007fff8ee67433                     C:\Windows\system32\USER32.dll+00029747 GetMessageA+00000251
    [ 39] 0x00007fff8ee673d7                     C:\Windows\system32\USER32.dll+00029655 GetMessageA+00000159
    [ 40] 0x0000000005789a1b   C:\Program Files\MATLAB\R2013a\bin\win64\uiw.dll+00236059 UIW_SetCurrentDialog+00000731
    [ 41] 0x00000000057b1532   C:\Program Files\MATLAB\R2013a\bin\win64\uiw.dll+00398642 ws_FreeSystemFont+00000482
    [ 42] 0x00000001800d8930 C:\Program Files\MATLAB\R2013a\bin\win64\libmwservices.dll+00887088 services::system_events::PpeDispatchHook::~PpeDispatchHook+00055600
    [ 43] 0x00000001800d92f5 C:\Program Files\MATLAB\R2013a\bin\win64\libmwservices.dll+00889589 services::system_events::PpeDispatchHook::~PpeDispatchHook+00058101
    [ 44] 0x00000001800da100 C:\Program Files\MATLAB\R2013a\bin\win64\libmwservices.dll+00893184 services::system_events::PpeDispatchHook::~PpeDispatchHook+00061696
    [ 45] 0x00000001800e28d2 C:\Program Files\MATLAB\R2013a\bin\win64\libmwservices.dll+00927954 svWS_ProcessPendingEvents+00001090
    [ 46] 0x000000007498329d   C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00078493 mcr::setInterpreterThreadToCurrent+00035885
    [ 47] 0x0000000074983562   C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00079202 mcr::setInterpreterThreadToCurrent+00036594
    [ 48] 0x0000000074976915   C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00026901 mcrInstantiationError::operator=+00003781
    [ 49] 0x0000000074979d52   C:\Program Files\MATLAB\R2013a\bin\win64\mcr.dll+00040274 mcr_invoke_on_interpreter_thread+00001602
    [ 50] 0x0000000140005591 C:\Program Files\MATLAB\R2013a\bin\win64\MATLAB.exe+00021905
    [ 51] 0x0000000140005c3a C:\Program Files\MATLAB\R2013a\bin\win64\MATLAB.exe+00023610
    [ 52] 0x0000000140006103 C:\Program Files\MATLAB\R2013a\bin\win64\MATLAB.exe+00024835
    [ 53] 0x00007fff8d601611                   C:\Windows\system32\KERNEL32.DLL+00005649 BaseThreadInitThunk+00000013
    [ 54] 0x00007fff8f4564ad                      C:\Windows\SYSTEM32\ntdll.dll+00484525 RtlUserThreadStart+00000029


This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.

If this problem is reproducible, please submit a Service Request via:
    http://www.mathworks.com/support/contact_us/

A technical support engineer might contact you with further information.

Thank you for your help.

最佳答案

暂定答案,但看看这篇文章:

for (i = 0; i < rmax; i++){ 
    for( j = 0 ; j <cmax ; j++ ){ 
    idx1[i][j]= I[i][j]; }}

I 是 [9][8],但 cmax 可以达到 9 (M = 9):

cmax=(M<(col+S))?M:(col+S);

因此,您正在访问越界元素。

第二种可能性:

int q=rmax*cmax;

q 在 rmax 和 cmax 分配值之前声明。因此,您使用 q 的分配可能是问题所在。

关于c - Mex 内存泄漏..我需要一些帮助来发现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30552450/

相关文章:

matlab - 使用 GUIDE (MATLAB GUI) 时 GUI 元素的属性存储在哪里

c - "malloc in local function, free memory in main"可以吗?如何?

c++ - 指针转换为 char* 数组

c - 尝试计算字符串C程序中每个字符的数量

c - 这段c语言代码有什么问题?

objective-c - objective-c 中的前缀匹配

matlab - 如何加快matlab中的读取速度

当您取消引用空指针时,您能证明为什么强制转换很重要吗?

matlab - 对单位使用 sqrt 函数时出错

c - (node *) NULL 在 C 中是什么意思?