Search code examples
cmemory-managementstructmallocfree

C: program prints garbage text even after structs are freed


I have a program with the following structs:

typedef struct slide
{
    int number;
    int maxX;
    int y;
    int r, g, b;
    struct line *first;
} slide;

typedef struct line {
    char content[256]; // number subject to change
    int r, g, b;
    struct line *prev;
    struct line *next;
} line;

Creating instances of these structs is done with the following code:

slide* createSlideArray(int s) {
    slide* slides = malloc(sizeof(struct slide)*s);
    return slides;
}

line *nextLine(line *prev) {
    line *n = malloc(sizeof(line));
    n->prev = prev;
    prev->next = n;
    return n;
}

And finally here is the code to free the structs after the program loop finishes, and before a new file is opened:

void freeLines(line *l) {
    line *next;
    while(l) {
        next = l->next;
        free(l);
        l = next;
    }
}

in main:

int i;
for (i=0;i<slideCount;i++) {
    freeLines(slides[i].first); // works through next till NULL
}
free(slides);

As you can see, an instance of the slide struct holds a "first" line struct, the line struct is a doubly linked list. The line's content is read to the screen (using ncurses)

While in the program loop the user can type a command :open filename to open a new file, this is where my issue lies.

This slides and lines should be freed. When the loop starts again it'll open a new file, create the slides and lines, and use them as the content. The content, however, is partially filled with garbage text. I'm pretty sure this issue is in the lines struct and how it is being free. I'm not seeing that I'm doing wrong however.

Edit: I should make it clear that on the first iteration of the program the text is perfect, it is only when I try to parse a new file that the garbage text appears, and it is very homogeneous (appears at the front of each line, same characters)

I'm on Ubuntu if that makes a difference.

Here is a link to the project: DSS


Solution

  • There are some clear issues that I can spot from reading the code in your post. I can't definitively say if these are responsible for your problem as the code in your post is not a compilable example and I didn't go delving into your linked project.

    Your nextLine function:

    You have neglected to set n->next.

    You have also neglected setting the prev pointer of the following node if there is one. ( prev->next->prev if prev->next != NULL ).

    You current design precludes you from using this function to setup the first node in the list. It also does not show how you intend to create that node.

    This function does not allow you to add a node to the begining of the list.

    Your createSlideArray function:

    This function does not initialize the slides it creates. These slide instances must be initialized. It seems sensible to do it here, but you might have a good reason to do it elsewhere. In either case, initializing the slideObject.first member is critical. Without this you will not be able to tell if the slide has a list of lines or not and your freeLines function will fail as it will be passed garbage as its parameter.

    Note:

    A different way of implementing doubly linked lists is using a "head node" which is always present and links to the first and last node in the list. This simplifies some issues, but changes others. You can research this if you are interested.