By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

free(...) fails in a *struct inside a *struct, please help...

P: 14
(sorry about my english...)
I am a newbie in C (3 month expierience)
I have wrote a simple test programm in VS2005, what i do wrong?Please...

In this programm i create a double linked list.Then pass its first block pointer
inside the structure Array to Array ->first and the last block pointer inside the structure Array to Array ->last.So i can manipulate the double linked list as a dynamic array. The cells of this dynamic array are located in
Array ->first/last ->value and inside there i pass a point of structure and in this point of structure i pass onother one, which i fill then with values (type: double).
So far so good, everything is created as expected.The problem begins when i want to free all the memory i have took from the heap.The only memory i can free is from the 4 double values from the fourth stage struct and the 2 values from the third stage struct and the 2 values(previus, next) from the second stage struct, but not actually any of these structs.
When i try to delete any of these structs i get a message saying:
__________________________________________________ ________________
|
| Windows has triggered a breakpoint in Test.exe.
|
| This may be due to a corruption of the heap, and indicates a bug in
| LListTest.exe or any of the DLLs it has loaded.
|_________________________________________________ ________________
The followed programm has been run and tested only in debug mode.

programm: Test.c

#include <stdio.h>
#include <stdlib.h>
#include "LList2.h"

typedef struct {LBlock2T first; LBlock2T last;} *LL2T;
typedef struct {long *DCindex; void *DCvalue; short *DCtype;} *DCT;
typedef struct {double *a; double *b; double *c; double *d;} *DCVT;

static void Create(LBlock2T datablock);
static void Destroy(LBlock2T datablock);

main
(void)
{
LL2T Array;

Array = malloc(sizeof(LL2T));
Array ->first = NList2(NULL,0,f);
Array ->last = NList2(Array ->first,1,l);
Create(Array ->first);
Destroy(Array ->first);
free(Array);
}
static void Create(LBlock2T datablock)
{
DCT datacarrier;
DCVT data;

datablock ->value = malloc(sizeof(DCT));
datacarrier = datablock ->value;
datacarrier ->DCindex = malloc(sizeof(long));
*(datacarrier ->DCindex) = 1;
datacarrier ->DCtype = malloc(sizeof(short));
*(datacarrier ->DCtype) = 0;
datacarrier ->DCvalue = malloc(sizeof(DCVT));
data = datacarrier ->DCvalue;
data ->a = malloc(sizeof(double));
*(data ->a) = 123.456;
(data ->b) = malloc(sizeof(double));
*(data ->b) = 123.456;
data ->c = malloc(sizeof(double));
*(data ->c) = 123.456;
data ->d = malloc(sizeof(double));
*(data ->d) = 123.456;
}

static void Destroy(LBlock2T datablock)
{
DCT datacarrier;
DCVT data;

datacarrier = datablock ->value;
data = datacarrier ->DCvalue;
free(data ->a);
free(data ->b);
free(data ->c);
free(data ->d);
/* here is where the problem occurs*/
free(data);
free(datacarrier ->DCtype);
free(datacarrier ->DCindex);
free(datacarrier);
free(datablock ->previus);
free(datablock ->next);
free(datablock);
}
__________________________________________________ ________________
heap information at the time of problem:
the symbol > is a green arrow and the symbol -> is a yellow arrow.

-> ntdll.dll!77352ea8()
Frames below may be incorrect and/or missing, no symbols loaded
forntdll.dll]
ntdll.dll!773c0c9a()
ntdll.dll!773ac900()
ntdll.dll!77394b6e()
ntdll.dll!7732894a()
ntdll.dll!77372033()
ntdll.dll!77371c21()
kernel32.dll!75c0b019()
> msvcr80d.dll!_CrtIsValidHeapPointer(const void * pUserData=0x00a61208)
Line 2072 C++
msvcr80d.dll!_free_dbg_nolock(void * pUserData=0x00a61208, int
nBlockUse=1) Line 1279 + 0x9 bytes C++
msvcr80d.dll!_free_dbg(void * pUserData=0x00a61208, int nBlockUse=1)
Line 1220 + 0xd bytes C++
msvcr80d.dll!free(void * pUserData=0x00a61208) Line 1178 + 0xb bytes
C++
Test.exe!Destroy(<unnamed-tag> * datablock=0x00a63f80) Line 57 +
0xc bytes C
Test.exe!main() Line 20 + 0xb bytes C
Test.exe!__tmainCRTStartup() Line 597 + 0x19 bytes C
Test.exe!mainCRTStartup() Line 414 C
kernel32.dll!75c33833()
ntdll.dll!7734a9bd()

__________________________________________________ ________________local variables in the time of problem

_crtheap 0x00a60000 void *
pUserData 0x00a61208 const void *
__________________________________________________ ________________

Thank you for your help.
Jan 15 '08 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
/* here is where the problem occurs*/
free(data); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
free(datacarrier ->DCtype);
free(datacarrier ->DCindex);
free(datacarrier);
free(datablock ->previus);
free(datablock ->next);
free(datablock); < <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
data and datablock are local variables. You can't free() them since you didn't
malloc() them.

Also, why are these variables duplicated in your function????

Also, why are these functions static ??????

Use pointer arguments to pass malloc'd variables around. If you don't, the local variables are copies of the variables used on the call. Your function chnage the copies and not the originals. This will cause all sorts of problems.
Jan 15 '08 #2

P: 14
Thank you weaknessforcats.

datacarrier = datablock ->value;

> I duplicated the pointers because the datablock ->value is type of void* and the datacarrier is type of DCT and i wand to free the memory i earlier state in the programm as malloc(sizeof(DCT)).

Can i just free(datablock ->value) and it will work as i free(datacarrier) even if they are differend types?

> I took this programm from a biggen application which it was part of it but i didn't change the static because i didn't know what was the actual problem of it.

You also sayed than : Use pointer arguments to pass malloc'd variables around. If you don't, the local variables are copies of the variables used on the call. Your function chnage the copies and not the originals. This will cause all sorts of problems.

Can you be more explanatory or give me an examble or correct one point of the code just there were it fails so i can see it...please.

I didn't understand the meaning of pointer arguments, you mean pointer variables to house the addresses of the call?
Jan 15 '08 #3

P: 14
thank you solve it.i just free(function which returns the value i want to free) retrospectively...
Jan 15 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.