Good day,

I have been playing around with a few functions today,
and I am guessing my garbage cleanup is not so pretty.

I have a function that I am using to set titles on a char.
It works well, but I can see that I am doing something
wrong. The function works each time, but I can see that
it is using the same memory space over and over and that
memory space is not getting cleaned up after the function
exits.

When I type  (kiLLer) the first time, the bug logs shows me
--> bug log: (kiLLer) - kiLLer)
which is proper.

if I type [AFK] the second time the bug log shows

--> bug log: [AFK] - AFK]er)

The str_prefix will still catch it ... which is good ... but the extra
garbage in there, must be telling me I am doing something wrong.
My function is below.   It is not rocket science... is it?

void set_title(CHAR_DATA *ch, char *title)
        {
        char buf[MAX_STRING_LENGTH];
        char buf2[MAX_STRING_LENGTH];
        char *p;
        int spacecheck;

        if (IS_NPC(ch))
                {
                bugf("Set_pretitle: NPC.");
                return;
                }

        /* copy the title into a temp buffer */
        strcpy(buf2, title);

        spacecheck=TRUE;
        for (p=buf; *title; title++)
        {
                if (*title=='{')
                        {
                        /* remove { and the next char after */
                        if (!*(++title))
                        break;
                        continue;
                        }
                else if (*title=='(' || *title=='[')
                        {
                        continue;
                        }
        spacecheck=(*title==' ');
        *p++=*title;
        }

        bugf("%s - %s", buf2, buf);

        *p=0;

        /* check for illegal names */
if (!str_prefix("KILLER", buf) || !str_prefix("THIEF", buf) || !str_prefix("FLAP", buf) || !str_prefix("AFK", buf))
                {
                send_to_char("Unacceptable title, try another.\n\r", ch);
                return;
                }

        if (!spacecheck)
                {
                strcat(buf2, " ");
                }

        send_to_char("Ok.\n\r", ch);
        strcat(buf2, "{d"); /* colour escapes go here.*/

        free_string(ch->pcdata->pretitle);
        ch->pcdata->pretitle = str_dup(buf2);
        return;
        }

Thanks ... Rick


Reply via email to