I was chasing a segmentation error with memory allocations, so I decided to run the application with valgrind. In an unexpected place (but possibly relevant), I came across an "Invalid size 8 record." However, I do not see anything wrong with the code. I would appreciate another look at him.
I am not sure how much to provide, so here is this feature.
43 static int sql_callback(void *sql_record, int argc, char **argv, char **azColName){ 44 int i; 45 SQL_INFO *sql_info; 46 void *sql_temp; 47 sql_info = (SQL_INFO *)sql_record; 48 49 50 sql_info->num_cols=argc; 51 52 sql_info->sql_data=(SQL_DATA**)realloc(sql_info->sql_data,(sql_info->num_rows+1)*sizeof(SQL_DATA *)); 53 sql_info->sql_data[sql_info->num_rows]=calloc(1,sizeof(SQL_DATA *)); 54 55 sql_info->sql_data[sql_info->num_rows]->col_name=calloc(1,sizeof(*azColName)*argc); 56 sql_info->sql_data[sql_info->num_rows]->data=calloc(1,sizeof(*argv)*argc); 57 58 for(i=0; i<argc; i++){ 59 sql_info->sql_data[sql_info->num_rows]->col_name[i]=strdup(azColName[i]); 60 sql_info->sql_data[sql_info->num_rows]->data[i]=strdup(argv[i]); 61 } 62 sql_info->num_rows++; 63 return 0; 64 }
Valgrind has two interesting points of information:
==31925== Invalid write of size 8 ==31925== at 0x405EC2: sql_callback (sql.c:56) ==31925== by 0x5310FF5: sqlite3_exec (in /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6) ==31925== by 0x4060D6: sql_query (sql.c:87) ==31925== by 0x405121: send_trip_record (ssl_main.c:573) ==31925== by 0x404579: do_work (ssl_main.c:380) ==31925== Address 0x70f0fc8 is 0 bytes after a block of size 8 alloc'd ==31925== at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31925== by 0x405E8D: sql_callback (sql.c:53) ==31925== by 0x5310FF5: sqlite3_exec (in /usr/lib/x86_64-linux-gnu/libsqlite3.so.0.8.6) ==31925== by 0x4060D6: sql_query (sql.c:87) ==31925== by 0x405121: send_trip_record (ssl_main.c:573) ==31925== by 0x404579: do_work (ssl_main.c:380) ==31925==
So, I am using the sqlite3 library, and this callback function creates an array of recordset. Inside the array is an array, if data, and this is the variable 'data'. I am doing something similar with col_name, but valgrind is fine with this and doesn't complain.
source share