Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    698

    [C] free() mi genera un crash

    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
    	}	
    }

  2. #2
    L'errore lo commetti tu, quando chiami ctime, che restituisce un puntatore statico sovrascrivendo ciò che hai allocato con malloc.

    Hai due alternative:
    a. Non allochi con malloc la ctime_str (che riceverà il puntatore statico dalla ctime) e quindi non lo liberi con free;
    b. Chiami ctime_r con ctime_str allocato con malloc, e che poi dei liberare con free:
    Codice PHP:
    ctime_str ctime_r(&nowctime_str); 

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    698
    azz hai ragione

    per il momento penso di poter fare a meno di deallocare ctime_str, ma comunque, che fine fa la memoria allocata da ctime() per la stringa?

  4. #4
    Originariamente inviato da Gil Mour
    azz hai ragione

    per il momento penso di poter fare a meno di deallocare ctime_str, ma comunque, che fine fa la memoria allocata da ctime() per la stringa?
    da:
    http://en.wikipedia.org/wiki/Time.h

    [cite]
    char* ctime(const time_t* timer)
    Convert time_t time value to string in the same format as asctime. The string pointed is statically allocated and shared by ctime and asctime functions. Each time one of these functions is called the content of the string is overwritten. ctime also uses internally the buffer used by gmtime and localtime as return value, so a call to this function will overwrite this.
    [/cite]

  5. #5
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    698
    ok questo quindi vuol dire che una volta allocata le chiamate successive continuano a lavorare in quell'indirizzo senza allocazioni ulteriori

    grazie

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.