I am having trouble freeing a pointer in a pointer array (values).
typedef struct MyStruct{ char** values; }MyStruct;
In C, I create dynamic array.
JSDictionary **array = (JSDictionary **)malloc(sizeof(JSDictionary *) * numRows);
The resultSet should be an array of JSDictionary pointers. I create the struct like:
JSDictionary * newJSDictionaryWithSize(unsigned int size)
{
JSDictionary *new = malloc(sizeof(JSDictionary));
printf("new %p\n", self);
new->_size = size;
new->count = 0;
new->values = (char **) malloc(sizeof(char *) * size);
for(unsigned int i = 0; i < size; i++){
new->values[i] = (char *)malloc(sizeof(char *));
}
return new;
}
Everything creates and works fine. It's the free that gives me a problem.
void deallocJSDictionary(struct JSDictionary *self)
{
printf("dealloc %p\n", self);
for(unsigned int i = 0; i < self->_size; i++){
printf("free %p\n", &self->values[i]);
free(self->values[i]);
}
free(self->values);
free(self);
}
I get a pointer being freed was not allocated error. The pointer being passed in shows the same memory address as the one that I created and added to the array. The values pointer (in debugger) shows the same memory address in the dealloc function as it did when I created it. The problem is trying to free the first point in the values array. Any ideas why the first point in the values pointer array is different?