Is this code okay? (mem leaks etc)

Posted by Zeno on Wed 27 Jun 2007 01:58 PM — 6 posts, 20,972 views.

USA #0
I plan to release this to the public, I just want to make sure it's okay before I do so. (No mem leaks, etc)

In act_comm.c at the top:
char * contact1;
char * contact2;
char * contact3;
char * contact4;
char * contact5;
char * contact6;
char * contact7;
char * contact8;


In talk_channel at the top:
    char histbuf[MAX_STRING_LENGTH];

In the talk_channel CONTACT case:
        sprintf( buf, "&R[&O%s&R] &W$n &w- '&Y$t%s&w'&D", verb, ANSI_RESET );
        strtime                    = ctime( &current_time );
        strtime[strlen(strtime)-1] = '\0';
        sprintf(histbuf, "&g(%s) &W%s: &Y%s&D\n\r", strtime, ch->name, argument);
        contact8 = contact7;
        contact7 = contact6;
        contact6 = contact5;
        contact5 = contact4;
        contact4 = contact3;
        contact3 = contact2;
        contact2 = contact1;
        contact1 = str_dup(histbuf);
        break;


That's it. Everthing else is just stuff to display it etc. (This is a channel history)

I'm just not totally sure if I should have used str_dup or STRFREE somewhere else, or the sort.
Amended on Wed 27 Jun 2007 02:07 PM by Zeno
USA #1
You don't free the memory you allocate with the str_dup. When you set contact8 to contact7, you probably want to clear whatever contact8 contains (if it contains something already). I don't think you need to care too much about emptying the strings on shutdown unless you are a compulsive perfectionist. (I'd do it, but that's just me...) But freeing the contents of contact8 when you lose the pointer is pretty important.

Incidentally, why didn't you make 'contact' an array? :)
USA #2
So I should DISPOSE contact8? Is that it?

As for the array, yeah I planned to redo it into one before I release it.
USA #3
Yes, I think that disposing of contact8 when you're about to overwrite it is all you need to do. Just make sure that there's actually a string there before overwriting it. (That would mean you'd have to initialize all of them to NULL beforehand.)
USA #4
http://www.mudbytes.net/index.php?a=files&s=viewfile&fid=960
USA #5
Silly me. I didn't think there was a chan history snippet for Smaug released.