Question

I am creating a simple dictionary in C using generic linked lists, thus in the process I am using malloc and free.

Anyway, Valgrind sadly doesn't support OS X 10.9, therefore I am using Xcode's Leaks Instrument. However, I have a bit trouble understanding the feedback this tool is providing me. Simpler, from the data output I can't understand if I have leaks or not.

Here is a screenshot of what the tool is telling me:

enter image description here

link to image

Here is the code that is creating the linked list:

typedef struct Element {
    struct Element *next;
    void *value;
} TElelemt, *TList, **AList;

typedef void (*PrintF) (void *);

int Insert_Elm(AList list, void *value, size_t dimension) {
    TList aux = (TList) malloc(sizeof(TElelemt));

    if (!aux) {
        printf("Allocation error!\n");
        return 0;
    }

    aux->value = malloc(dimension);
    if (!aux->value) {
        printf("Allocation error!\n");
        return 0;
    }

    memcpy(aux->value, value, dimension);
    aux->next = *list;
    *list = aux;

    return 1;
}

void Delete_Elm(AList list) {
    TList elm = *list;

    if (!elm) {
        return;
    }
    free(elm->value);
    *list = elm->next;
    free(elm);

    return;
}

void Delete_List(AList list) {
    while (*list) {
        Delete_Elm(list);
    }

    return;
}

// In main() I am doing something like this where TWord cuv is allocated in stack:
    for (i = 0; i < 1000000; i++) {
        Insert_Elm(&list, &cuv, sizeof(TWord));
    }
    Delete_List(&list);

What is the tool saying and are there any memory leaks?

Was it helpful?

Solution

Your screenshot is showing no memory leaks. In your screenshot the Allocations instruments has graphed your memory usage in blue. If Instruments had detected any memory leaks, it would show them in the graph for the Leaks instrument. But in your screenshot the graph for the Leaks instrument is blank, which means Instruments detected no memory leaks.

One additional thing, your screenshot is showing the statistics for the Allocations instrument. It's telling you the amount of memory your program allocated. Select the Leaks instrument from the instrument list to see the Leaks instrument's statistics. Selecting the Leaks instrument will also tell you whether or not any memory leaks were detected. If you have any leaks, Instruments will list them in the table below the graphs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top