Mark Butcher

free() crashing - M5223X project

Discussion created by Mark Butcher on Jun 12, 2007
Latest reply on Jun 25, 2007 by CrasyCat
Hi All

I have been avoiding any libraries but have had to use malloc and free so added the C_TRK_4i_CF_SZ_MSL.a libray (as is used in M52235 demo projects).

I set up the heap in the linker to be restricted to 1024 bytes rather than spanning from the end of BSS to (initial stack pointer - stack size).

There are the relevant linker delivered values which are as expected.
#>00000400          HEAP_SIZE (linker command file)
#>200006D0          ___HEAP_START (linker command file)
#>20000AD0          ___HEAP_END (linker command file)
#>200006D0          ___heap_addr (linker command file)
#>00000400          ___heap_size (linker command file)
#>20008000          __SP_INIT (linker command file)

When I call [test = malloc(100)] all is well. I get memory within the expected range.

When I call free (test) I get an exception. Using the debugger I see the following calls:
- void _MSL_CDECL __FREE(void* ptr)
-     __pool_free(get_malloc_pool(), ptr);
-         deallocate_from_var_pools(pool_obj, ptr);
-     Block_link(bp, sb);
In this routine there is an instruction sequence which I can't relate back to the src code:
lea (-4,a5,d7.l),a6  (a6 gets the value 0x000003FE - somewhere in the startup code)
movea.l (a6),a1     (a1 gets the value 0xffff0000)
tst.l a1
beq.s _block_link
move.l 8(a1),d0   (exception caused here)

So my questions. How is it possible that free() can crash - does it require something which I haven't done?
Am I using the correct library for my M52235 application?

Best regards

Mark Butcher