08-12-2013 05:08 AM
Hi,
I have done a fonction that rread data from DataBase. Ininiatialized an array of 3 D char (2D array of string) . I having problem to free this array after using "DBGetVariantArrayValue". I get the message :
NON-FATAL RUN-TIME ERROR: "SQLServerDB.c", line 151, col 13, thread id 0x00001E0C: Attempt to free pointer to memory not allocated by malloc() or calloc().
But If I free array of char just after malloc is working well !!
int hstmt = 0; /* Handle to SQL statement */
 int resCode; /* Result code */
 int i=0,j=0,
 numLine=0,
 numCol=0;
 
 VARIANT *cArray;
 
 char szFinalQuery[512]="";
char ***ArrOfStr ;
 
 
//**** Get data from DB **** 
 
 //Disable error
 DisableBreakOnLibraryErrors ();
 
 //Send query to SQL server DB
    Fmt(szFinalQuery,"%s %s", szQuery, szDBTableName); 
    hstmt = DBActivateSQL (hdbc, szFinalQuery); 
 
 //Get result from DB 
   resCode = DBGetVariantArray (hstmt, &cArray,&numLine, &numCol);
//3 D array of char // 2D array of string allocation of memory
   ArrOfStr = (char***)malloc(numCol * sizeof(char**)); 
       for(i=0; i<numCol; i++){
           ArrOfStr[i]= (char**)malloc(numLine * sizeof(char*)); 
           for (j=0;j<numLine;j++) {
                  ArrOfStr[i][j]= (char*)malloc(MAXCHAR); 
           }
       }
 
 //Fill out 2D array of string with data from DB
     for (i = 0; i < numLine; i++) {
          for (j = 0; j < numCol; j++) {
                 resCode = DBGetVariantArrayValue (cArray, numLine, numCol, CAVT_CSTRING, i, j, &ArrOfStr[j][i]);
 
                 // Null terminering character if data = NULL
                 if (resCode == DB_NULL_DATA){
                     Fmt(ArrOfStr[j][i],"");
                 }
          }
    }
 
 //Frees the variant array that DBGetVariantArray returned.
 resCode = DBFreeVariantArray (cArray, 1, numLine, numCol);
//Deactivate SQL //free ressources
 DBDeactivateSQL (hdbc); 
 
 //Free 3D array of char
          for(i=0; i<numCol; i++){ 
                 for (j=0;j<numLine;j++) {
                         free (ArrOfStr[i][j]);  // GET ERROR HERE !!!
                 }
                 free(ArrOfStr[i]); 
           }
        free (ArrOfStr);