Ho un crash che mi puzza di malloc bug, anche se non riesco a capire come sia possibile.
Nel corpo di una stessa funzione faccio una malloc per una stringa, e poche istruzioni dopo tento di liberarla con la free.
Ora, che io sappia, il bug della malloc si verifica quando si tenta di deallocare uno spazio non allocato con malloc usando la free ma non è questo il caso.
Sicuramente devo avere qualche puntatore sputtanato,ma non riescoa venirne a capo..posto il codice che è molto semplice
codice:
static __inline void LOG(int level,char *fmt, ...)
{
char LOGLBL [6][16] =
{
"SEVERE",
"WARNING",
"INFO",
"FINE",
"FINEST",
"ALL"
};
if (level<=LOG_LEVEL)
{
time_t now;
char * ctime_str;
char * str_date;
va_list args;
char buf[1024];
va_start(args, fmt);
vsprintf(buf, fmt, args);
va_end(args);
OutputDebugString(buf);
str_date = (char *)malloc(128*sizeof(char));
ctime_str = (char *)malloc(128*sizeof(char));
now = time(NULL);
ctime_str = ctime(&now);
strncpy(str_date,ctime_str,strlen(ctime_str)-1);
str_date[strlen(ctime_str)-1] = '\0';
fprintf(log_file, "[%s][%s]: %s",str_date,LOGLBL[level], buf);fflush(log_file);
#ifdef _DEBUG_LOGV
fprintf(stderr, "[%s][%s]: %s",str_date,LOGLBL[level], buf); fflush(stderr);
#endif
free(str_date);
free(ctime_str); // in questo punto avviene il crash
}
}