Allocating from the heap when a module is initialised

General discussions about working with the Astrobe IDE and programming ARM Cortex-M0, M3, M4 and M7 microcontrollers.
Post Reply
Helpdesk
Posts: 33
Joined: Sat Jan 01, 2011 5:43 am
Contact:

Allocating from the heap when a module is initialised

Post by Helpdesk » Fri Jun 26, 2015 12:03 pm

it took me a long time to fix a nasty problem that looked like memory being overwritten somehow. I found out that the overwritten memory -a record allocated on heap- was allocated during the MODULE Initialisation sequence before the body of MODULE Storage got called which reset the heap start address to its initial value. The result was that subsequent calls to NEW allocated same heap area already used by blocks allocated before initialisation of MODULE Storage! The cure was easy, moving 'Storage' in the IMPORT list helped. For the moment I import 'Storage' in MODULE 'Main'. Is this a foolproof scheme or do you recommend another solution?

cfbsoftware
Site Admin
Posts: 493
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

Re: Allocating from the heap when a module is initialised

Post by cfbsoftware » Fri Jun 26, 2015 12:07 pm

Storage is a special case and you do have to be careful in its use - I'll make a note to point this out in the documentation. Note that you only need to import Storage if you have modified it in any way. The compiler automatically imports MAU and calls its functions when you use NEW and DISPOSE. If you have modified Storage then you should call it before you make any calls to NEW / DISPOSE. If I understand you correctly, I think it should work OK if you call Storage.Allocate in the same MODULE initialisation sequence where you make the first call to NEW.

Post Reply