Search code examples
cgenericssegmentation-faultcopyfree

I get a segmentation fault because of free even though i used malloc


i am writing a Generic ADT using C and i keep getting a segmentation fault when i free an element

PairResult pairClear(Pair pair)
{
    if(pair == NULL)
    {
        return PAIR_NULL_ARGUMENT;
    }
     KeyElement key=pair->key;
     DataElement data=pair->data;
     if(key)
     pair->free_key(key);//i get the Error here
     if(data)
     pair->free_data(data);
    return PAIR_SUCCESS;
}

the memory for key and data is allocated :

Pair pairCreate( KeyElement key, DataElement data,
                      copyDataElements copy_data,
                      freeDataElements free_data,
                      copyKeyElements copy_key,
                      freeKeyElements free_key)
{
  Pair pair = malloc(sizeof(*pair));
    if(pair == NULL)
    {
        return NULL;
    }
     pair->copy_data=copy_data;
     pair->copy_key=copy_key;
     pair->free_data=free_data;
     pair->free_data=free_key;
   KeyElement new_string_key = copy_key(key);
    DataElement new_string_data = copy_data(data);
    if((new_string_key == NULL) || (new_string_data == NULL))
    {
        pairDestroy(pair);
        return NULL;
    }
    pair->key = new_string_key;
    pair->data = new_string_data;
return pair;
}

this pairDestroy

void pairDestroy(Pair pair)
{
    if(pair == NULL)
    {
        return;
    }

    #ifndef NDEBUG
          PairResult result = 
    #endif
        pairClear(pair);
    assert(result == PAIR_SUCCESS);
    free(pair);
}

these are the copy functions used:

static KeyElement copyKeyInt(KeyElement n) {
    if (!n) {
        return NULL;
    }
    int *copy = malloc(sizeof(*copy));
    if (!copy) {
        return NULL;
    }
    *copy = *(int *) n;
    return copy;
}
static DataElement copyDataChar(DataElement n) {
    if (!n) {
        return NULL;
    }
    char *copy = malloc(sizeof(*copy));
    if (!copy) {
        return NULL;
    }
    *copy = *(char *) n;
    return (DataElement) copy;
}

and these are the free functions used

static void freeInt(KeyElement n) {
    free(n);
}

static void freeChar(DataElement n) {
    free(n);
}

and here is the struct of pair

struct Pair_t {
    KeyElement key;
    DataElement data;
    copyDataElements copy_data;
    freeDataElements free_data;
    copyKeyElements copy_key;
    freeKeyElements free_key;
};

these are all the typedef used :

typedef struct Pair_t* Pair;

typedef enum PairResult_t { 
    PAIR_SUCCESS,
    PAIR_OUT_OF_MEMORY,
    PAIR_NULL_ARGUMENT,
} PairResult;

typedef void *DataElement;
typedef void *KeyElement;
typedef DataElement(*copyDataElements)(DataElement);
typedef KeyElement(*copyKeyElements)(KeyElement);
typedef void(*freeDataElements)(DataElement);
typedef void(*freeKeyElements)(KeyElement);

and a main function so that u could reproduce it

int main()
{
Pair pair;
 for (int i = 1; i < 1000; ++i) {
        char j = (char) i;
        ++j;
     pair=pairCreate(&i,&j,copyDataChar,freeChar,copyKeyInt,freeInt);
     pairDestroy(pair);
}

I added everything I could for a reproducible code if anything should be edited please tell me in the comments


Solution

  • Pair pairCreate(...) {
              ...
              pair->free_data = free_data;
              pair->free_data = free_key;
              //    ^^^^^^^^^           UH OH
              ...
    

    You owe me 15 mins of debugging time.