Thread: Vararg problem

  1. #1
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79

    Vararg problem

    Hi...

    I've got a problem with the code below. It works the first time it's called. But if/when I call it a second time, the application crashes.

    What have I done wrong?

    Code:
    #include <stdarg.h>
    
    void log_message(char* str, ...) {
    
    	char*      logstr;
    	va_list    vargs;
    	char       time_str[18];
    	time_t     t;
    	struct tm* time_now;
    
    	if (!opts_log_stream) return;
    
    	t = time(NULL);
    	time_now = localtime(&t);
    	strftime(time_str, sizeof(time_str), "%y-%m-%d %H:%M:%S", time_now);
    	logstr = str_dupf("%s: %s\n", time_str, str);
    
    	va_start(vargs, str);
    	vfprintf(opts_log_stream, logstr, vargs);
    	va_end(vargs);
    
    	free(logstr);
    	free(time_now);
    
    }

  2. #2
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    Forget it... I should'n free time_now.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    And logstr, what about it? You didn't allocate it, but you are freeing it. If you don't allocate it, then you shouldn't free it.

  4. #4
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    logstr gets allocated by str_dupf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  2. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  3. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Texture Problem(I got the NeHe tut working, but I have a problem)
    By SyntaxBubble in forum Game Programming
    Replies: 2
    Last Post: 12-02-2001, 10:40 PM