Biogenicsoup
Programmer
Hi all
I am currently working on a program where i use a lot of different vectors. Most of my program runs perfectly but one particular vector is causing trouble when i try to deallocate it.
the structure of a vector is pretty straight forward
My deallocation method looks like this (scaled down for debugging)
Everyting runs smmothly until i reach the final free(vec) command, where the error occurs.
I dont know if this will help, byt here is the output from gdb when running through the DeallocVector method.
Breakpoint 2, DeallocVector (vec=0x6000000000010340) at deallocation.c:6
6 global.vector--;
(gdb) disp vec->type
1: vec->type = 1
(gdb) disp vec->size
2: vec->size = 4
(gdb) disp vec
3: vec = (vector *) 0x6000000000010340
(gdb) disp ((int*)vec->array)[0]
4: ((int *) vec->array)[0] = 0
(gdb) n
7 global.vecsize-=sizeof(vector)*vec->size;
4: ((int *) vec->array)[0] = 0
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
8 global.heapsize-=sizeof(vector)*vec->size;
4: ((int *) vec->array)[0] = 0
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
10 if(vec->type==CHAR)
4: ((int *) vec->array)[0] = 0
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
14 if(vec->type==INT)
4: ((int *) vec->array)[0] = 0
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
15 { DeallocIntArray(vec->array, vec->size);
4: ((int *) vec->array)[0] = 0
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
18 if(vec->type==DOUBLE)
4: ((int *) vec->array)[0] = 89952
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
21 free(vec);
4: ((int *) vec->array)[0] = 89952
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
free(): invalid pointer 0x6000000000010340!
22 }
4: ((int *) vec->array)[0] = 89952
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
22 }
4: ((int *) vec->array)[0] = 89952
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
I am currently working on a program where i use a lot of different vectors. Most of my program runs perfectly but one particular vector is causing trouble when i try to deallocate it.
the structure of a vector is pretty straight forward
Code:
typedef struct vector
{ int type;
int size;
void* array;
} vector;
My deallocation method looks like this (scaled down for debugging)
Code:
void DeallocVector(vector* vec)
{ global.alloc--;
global.vector--;
global.vecsize-=sizeof(vector)*vec->size;
global.heapsize-=sizeof(vector)*vec->size;
if(vec->type==CHAR)
{ DeallocCharArray(vec->array, vec->size);
}
if(vec->type==INT)
{ DeallocIntArray(vec->array, vec->size);
}
if(vec->type==DOUBLE)
{ DeallocDoubleArray(vec->array, vec->size);
}
free(vec);
}
Everyting runs smmothly until i reach the final free(vec) command, where the error occurs.
I dont know if this will help, byt here is the output from gdb when running through the DeallocVector method.
Breakpoint 2, DeallocVector (vec=0x6000000000010340) at deallocation.c:6
6 global.vector--;
(gdb) disp vec->type
1: vec->type = 1
(gdb) disp vec->size
2: vec->size = 4
(gdb) disp vec
3: vec = (vector *) 0x6000000000010340
(gdb) disp ((int*)vec->array)[0]
4: ((int *) vec->array)[0] = 0
(gdb) n
7 global.vecsize-=sizeof(vector)*vec->size;
4: ((int *) vec->array)[0] = 0
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
8 global.heapsize-=sizeof(vector)*vec->size;
4: ((int *) vec->array)[0] = 0
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
10 if(vec->type==CHAR)
4: ((int *) vec->array)[0] = 0
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
14 if(vec->type==INT)
4: ((int *) vec->array)[0] = 0
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
15 { DeallocIntArray(vec->array, vec->size);
4: ((int *) vec->array)[0] = 0
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
18 if(vec->type==DOUBLE)
4: ((int *) vec->array)[0] = 89952
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
21 free(vec);
4: ((int *) vec->array)[0] = 89952
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
free(): invalid pointer 0x6000000000010340!
22 }
4: ((int *) vec->array)[0] = 89952
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)
22 }
4: ((int *) vec->array)[0] = 89952
3: vec = (vector *) 0x6000000000010340
2: vec->size = 4
1: vec->type = 1
(gdb)