通过mex使用.dll(构建Visual Studio 2013 Toolset vs120时出现分段错误)>

问题描述 投票:1回答:2

[客户端必须格式化以分号“;”分隔的接收数据流。并通过mex函数将数据传递到Matlab环境。我创建了.dll在C中进行预处理(Visual Studio 2010和2013)。

使用Visual Studio 2013(ToolSet vs120)生成的.dll出现分段错误,而通过Visual Studio 2010(ToolSet vs100)生成的.dll正常工作。

同一.c文件用于生成.dll。

在注释mex函数中的free(y_1)时,带有Visual Studio 2013的.dll起作用。

我正在将2D指针从.dll传递到mex,这是它引发错误的地方。我已经添加了.dll和相应的mex函数的代码。

/*Decleration*/
typedef struct twoDArray {
    char **new_line ;
    int noOfLines ;
}TWODARRAY;
 TWODARRAY *y_1;
static TWODARRAY x_1;
 /*********************************************************/
/*Mex function*/

       if (!strcmp(fct_name, "GetSignal")) {
       ProcAdd2 = (MYPROC2)GetProcAddress(hinstDLL, "getAvailableSignals");
       lineCount = 0;
       if (-1 == sock_no) {
            mexErrMsgIdAndTxt("MATLAB:Client:conversionFailed","Connection to server not established.");
            }
       else {
            y_1 = (TWODARRAY *)malloc(sizeof *y_1);
            y_1 = (ProcAdd2)(sock_no);
            x_1.new_line = y_1->new_line;
            x_1.noOfLines = y_1->noOfLines;
            free(y_1);
            plhs[0]=mxCreateCellMatrix(x_1.noOfLines, 1);
            for (i=0;i<x_1.noOfLines;i++) {
            /* insert the line into the output array... */
                mxSetCell(plhs[0],i,mxCreateString(x_1.new_line[i]));
                }
            while(lineCount<x_1.noOfLines) {
                //printf("\n%s\n",  x_1.new_line[lineCount]);
                lineCount++;
                }

            }   
        } 
      /*********************************************************/
    /* .Dll Function .C file*/
    TWODARRAY *  GetSignalArray(char *received_str) {
    TWODARRAY *z = (TWODARRAY *)malloc(sizeof *z);
    z->new_line = NULL;
    z->noOfLines = N_NO_OF_LINES;
    char* str_tok;
    int lineCount = 0;
    z->new_line = (char **)malloc(sizeof(char*) * N_NO_OF_LINES);/* Get the address of the token */
    str_tok = strtok(received_str, ";");

    /* Search for the token anf arrange the stream of strings into 2D character array */
    while (str_tok != NULL) {
        if (lineCount >= z->noOfLines) {
            z->new_line =  (char **)realloc(z->new_line, (sizeof(char*)*(z->noOfLines+N_NO_OF_LINES)));
            z->noOfLines += N_NO_OF_LINES;
            }
        z->new_line[lineCount] = _strdup(str_tok);
        //printf("\nZ 2D Array\n%s\n",  z->new_line[lineCount]);
        lineCount++;
        str_tok = strtok(NULL, ";");
        }
        z->noOfLines = lineCount;
        return(z);
}


DECLDIR TWODARRAY * getAvailableSignals(SOCKET ConnectSocket)
    {
        char *sendbuf = "0000GetAvailableSignals";
        static char *received_stream;
        TWODARRAY *y = (TWODARRAY *)malloc(sizeof *y);
        int lineCount = 0;

        if (ConnectSocket == INVALID_SOCKET) {
            printf("socket failed with error: %ld\n", WSAGetLastError());
            return (y);
        }
        /* Function to send a request and receive a response from server */
        received_stream=send_data(ConnectSocket,sendbuf);

        /* Function to generate 2D array from selected signal stream */
        y = GetSignalArray(received_stream);
        return (y);
    }

使用Visual Studio 2013 .dll后崩溃的原因

[客户端必须格式化以分号“;”分隔的接收数据流。并通过mex函数将数据传递到Matlab环境。我创建了.dll来进行... ...>

matlab visual-studio-2010 visual-studio-2013 dll mex
2个回答
0
投票

我不认为它在特定版本的Visual Studio上崩溃对您的问题很重要。您的代码似乎正在泄漏内存。您分配了两次内存:

TWODARRAY *z = (TWODARRAY *)malloc(sizeof *z);
...
TWODARRAY *y = (TWODARRAY *)malloc(sizeof *y);

0
投票

通过2013生成的汇编代码

© www.soinside.com 2019 - 2024. All rights reserved.