A.Agiannidis
Joined: 04 Jun 2005 Posts: 2
|
Misbehaving malloc and free functions? |
Posted: Fri Jun 10, 2005 5:30 am |
|
|
Hi all,
THE TASK:
in my attempt to dynamically create a list of custom structures representing messages to be sent out, i wrote this piece of code. I defined a struct List and a struct Message. The intention was to create a linked list.
THE PROBLEM:
calling malloc returns a pointer to an address (say 0x48). I then initialise the elements (locations 0x48, 0x49). I then call free with pointer pointing to 0x48 but when I trace the memory management code it attempts to free a node starting from 0x42. Anyone knows why? Also if you check the memory map below you will see that nodes are not created in consecutive memory locations. malloc will give me addresses like 48,53,5B etc. However, the size of the structure message is only 5 bytes long so I would expect to get locations like 43, 48, 4C etc.
The problem was realised when attempting to push it to the limits and use the whole available memory. For a couple of calls to malloc you cant see any problem. But once you have created a list with say 10 nodes and then attempt to free them, although creating them gives no problems, freeing the memory brings a problem that crashes mplab at almost the start of the process.
Any thoughts?
THE CODE:
Code: | #include <16F876.h>
#include <STDLIBM.h>
#use delay(clock=4000000)
#fuses XT,WDT,NOPROTECT,PUT,BROWNOUT, NOLVP
#use rs232(baud=9600,xmit=PIN_C6,bits=8,parity=N,stream=rs232
#use i2c(SLAVE,SCL=PIN_C3,SDA=PIN_C4,ADDRESS=0xd2,RESTART_WDT,FORCE_HW,FAST)
struct LiFIFOBuffer{
int16 firstmsg;
int16 lastmsg;
int8 msgcount;
} Lififo;
struct LiMessage {
int byte1;
int byte2;
int wantsreply;
int16 nextmessage;
} ;
void addNewEntry(struct LiMessage *ptrNewMessage)
{
struct LiMessage *lastMsg;
if (Lififo.msgcount==0)
{
Lififo.firstmsg = ptrNewMessage;
Lififo.lastmsg = ptrNewMessage;
}
else
{
lastMsg = Lififo.lastmsg;
lastMsg->nextmessage = ptrNewMessage;
Lififo.lastmsg = ptrNewMessage;
}
Lififo.msgcount++;
}
int16 getAndRemoveEntry()
{
struct LiMessage *firstMsg;
if (Lififo.msgcount==0)
{
Lififo.firstmsg = 0x00;
Lififo.lastmsg = 0x00;
return 0;
}
firstMsg = Lififo.firstmsg;
Lififo.firstmsg = firstmsg->nextmessage;
Lififo.msgcount--;
return firstMsg;
}
int16 newMessage(int a, int b, int c)
{
struct LiMessage *ptrMsg;
ptrMsg = malloc(sizeof(struct LiMessage));
ptrMsg->byte1 = 0x0a;
ptrMsg->byte2 = 0x0b;
ptrMsg->wantsreply = 0x0c;
ptrMsg->nextmessage = 0x0000;
return ptrMsg;
}
void main()
{
int16 *ptrMsg;
//struct LiMessage *ptrMsg;
ptrMsg = (struct LiMessage*) malloc(sizeof(struct LiMessage));
// At this point ptrMsg = 0x0047
// Filling memory locations 0x0048, 0x0049
((struct LiMessage*) ptrMsg)->byte1 = 0x77;
((struct LiMessage*) ptrMsg)->byte2 = 0x77;
// calling free. ptrMsg still points at 0x0047.
// see what happens bellow inside the free function in STDLIBM
free((struct LiMessage*) ptrMsg);
lififo.firstmsg = 0;
lififo.lastmsg =0 ;
lififo.msgcount =0;
for ([censored]=1;[censored]<10;[censored]++)
{
ptrMsg = newMessage([censored],[censored]+1,[censored]+2);
addNewEntry(ptrMsg);
}
[censored] = 1;
for ([censored]=1;[censored]<10;[censored]++)
{
ptrMsg = getAndRemoveEntry();
free(ptrMsg);
}
}
void free( void * ptr)
{
node_t *node,*temp;
long nsize,nextsize;
// ptr is still 0x0047
if(ptr==NULL) // not a valid pointer
return;
else
{
// At this point node goes back to 0x0042. Why is that?
node=ptr-sizeof(node_t);
if(bit_test(node->size,pos))// node occupied
{
nsize=node->size-csize;
update_node(node,nsize);
ptr=NULL;
}
else // wrong input, return
{
ptr=NULL;
return;
}
}
traverse();
}
Nodes have been created at locations:
4B, 53,5B, 63, 6B, A3, AB, B3.
When attempting to free the node in memory location AB MPLAB crashes inside the
void remove_node(node_t *node) {// remove the given node from the memlist
node_t *ptr;
[color=red] for(ptr=__DYNAMIC_HEAD;ptr->next!=node;++ptr);[/color]
ptr->next=node->next;
node=NULL;
}
Address 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ASCII
0000 -- 00 F4 1B 4E 00 00 00 -- -- 00 00 00 00 00 00 -...N... --......
0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
0020 48 00 53 00 BB 00 08 4B 00 01 4B 00 B3 00 0B BB H.S....K ..K.....
0030 00 05 00 B8 00 C0 00 35 00 B8 00 C0 00 10 00 10 .......5 ........
0040 00 2D C0 00 C0 00 00 00 85 50 00 0A 0B 0C 53 00 .-...... .P....S.
0050 85 58 00 0A 0B 0C 5B 00 85 60 00 0A 0B 0C 63 00 .X....[. .`....c.
0060 85 68 00 0A 0B 0C 6B 00 85 A0 00 0A 0B 0C A3 00 .h....k. ........
0070 00 00 00 00 00 00 00 00 4B 00 00 00 00 00 00 00 ........ K.......
0080 -- FF F4 1B 4E 3F FF FF -- -- 00 00 00 00 00 -- -...N?.. --.....-
0090 -- 00 FF 00 00 -- -- -- 02 00 -- -- 07 00 00 07 -....--- ..--....
00A0 85 A8 00 0A 0B 0C AB 00 85 B0 00 0A 0B 0C B3 00 ........ ........
00B0 85 B8 00 0A 0B 0C BB 00 85 C0 00 0A 0B 0C 00 00 ........ ........
00C0 2D 10 01 00 00 00 00 00 00 00 00 00 00 00 00 00 -....... ........
00D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00F0 00 00 00 00 00 00 00 00 4B 00 00 00 00 00 00 00 ........ K.......
0100 -- 00 F4 1B 4E -- 00 -- -- -- 00 00 00 00 00 00 -...N-.- --......
0110 5D 90 01 00 00 00 00 00 00 00 00 00 00 00 00 00 ]....... ........
0120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
0130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
0140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
0150 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
0160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
0170 00 00 00 00 00 00 00 00 4B 00 00 00 00 00 00 00 ........ K.......
0180 -- FF F4 1B 4E -- FF -- -- -- 00 00 00 00 -- -- -...N-.- --....--
0190 56 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 V....... ........
01A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
01B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
01C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
01D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
01E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
01F0 00 00 00 00 00 00 00 00 4B 00 00 00 00 00 00 00 ........ K.......
|
|
|