Problem beim Speicher allokieren/freigeben in Bezug auf Char Daten



  • 😕 😕 😕

    Hallo, zusammen

    Ich benutze MATLAB mxMalloc um Speicher dynamik zu allokieren, das ist wie malloc.
    Mein Funktion sollte neue Daten in die Datenbank einfügen.

    Beim Hinzufügen von Char Daten habe ich Problem, die ich nicht ganz verstehe
    z.B ich füge {"Value1", "Value2", "Value3"} ein,
    aber der schreibt mir {" ", "V", "Va"}ein,
    dieses Problem tritt auf wenn ich die Puffer frei mache (Stelle siehe code), und wenn ich die auskommentiere, dann schreibt es wieder richtig ein. Aber so sollte auch nicht sein, das ich die Speicher nicht frei gibt am ende.

    ...
    
    /* Allocate Statement Handle */
       iResult = SQLAllocHandle(SQL_HANDLE_STMT, oDatabase.m_hdbc, &hstmt);
    
       /* Prepare a SQL string for execution */
       iResult = SQLPrepare(hstmt, (SQLCHAR *)sSQL, SQL_NTS);
    
       /* Allocate pointer buffer of data of different type */
       ppData = (void **) mxCalloc(iN_Column, sizeof(void*));
       if (ppData == NULL)
       {
          return DBI_ALLOC_MEMORY_ERR;
       }
    
       /* Allocate buffer for type ID */
       piTypeID = (int *) mxMalloc(iN_Column * sizeof(int));
       if (piTypeID == NULL)
       {     
          return DBI_ALLOC_MEMORY_ERR;
       }
    
    /* Allocate buffer and bind buffer to a parameter marker in an SQL statement */
    for (iIndex = 0; iIndex < iN_Cell; iIndex = iIndex+iN_Row)
    {
    
       switch (enClassID)
       {
       ...
    
           case mxCHAR_CLASS:
                #ifdef DEBUG
                 mexPrintf("      Type = mxCHAR_CLASS\n");
                #endif
                piTypeID[iColumn] = DBI_TYPE_ID_CHAR;
    
                ppData[iColumn] = (SQLCHAR *) mxMalloc(MAX_STR_LEN+1);
                if (ppData[iColumn] == NULL)
                {              
                   return DBI_ALLOC_MEMORY_ERR;
                }
                iResult = SQLBindParameter(hstmt, iColumn+1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 255, 0, (SQLCHAR *) ppData[iColumn], 0, &iCbChar);
                break;
       ...
       }
    
    iColumn++;
    }
    
       /* Write data row-wise into database */
       for (iRow = 0; iRow < iN_Row; iRow++)
       {
          /* Reset iColumn */
          iColumn = 0;
    
          /* Loop over all columns */
          for (iIndex = 0; iIndex < iN_Cell; iIndex = iIndex + iN_Row)
          {
             /* Get cell array */
             pDataValue = mxGetCell(pData, (iRow+iIndex));
    
    ...
    
              if (piTypeID[iColumn] == DBI_TYPE_ID_CHAR)
             {               
                /* Get buffer length of char value and check if buffer length is greater then 256 */
                iBuflen = mxGetNumberOfElements(pDataValue)+1;
                if (iBuflen > MAX_STR_LEN + 1)
                {
                   return DBI_INSERT_CHAR_BUF_ERR;
                }  
    
                mxGetString(pDataValue, (char *) ppData[iColumn], iBuflen);
    
             }
    ... 
    
             /* Increment column */
             iColumn ++;
          }
    
          /* Execute data insert statement for current row */
          iResult = SQLExecute(hstmt);
       }
    
       /* Free buffers */
       mxFree(piTypeID);
    
       /* Free handle */
       SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
    
       ...
     /*=========== Problem here ============*/           
       for (int i = 0; i < iColumn; i++)
       {
          mxFree(ppData[i]);
       }
       /*=========== Problem ============*/   
    
       mxFree(ppData);
    
       /* Free insert statement */
       FreeQuery(&sSQL);
    
       /* Success */
    	return(1);
    }
    

    Grüße

    weisheit


  • Mod

    Wie ist iCbChar definiert?
    Wird es bei dieser Konstellation benötigt und wenn ist es richtig gesetzt?



  • Martin Richter schrieb:

    Wie ist iCbChar definiert?
    Wird es bei dieser Konstellation benötigt und wenn ist es richtig gesetzt?

    Danke für deine Antwort

    SQLINTEGER iCbChar  = SQL_NTS;
    

    das habe ich von Beispiel für SQLBindParameter übernommen, aber ich denke ich muss noch nachschaue!


Anmelden zum Antworten