[fpc-devel] arm-embedded apps crash in exception handler initialization because heapmanager is not initialized

Michael Ring mail at michael-ring.org
Sun Mar 3 11:04:10 CET 2013


Simple solutions are the best ;-) Thanks for opening my eyes to this 
easy approach.

I have created a patch for heapmgr.pp, could I ask you to commit it to svn?

For a little more flexible solution I would like to ask you one more 
questions, it may seem dumb to you but I have never been that deep into 
a compiler before so I am learning a lot atm.

I can see that the compiler param -Ch can be used to define the 
heapsize, I can also see in the assembler code of my main program that 
when I set -Ch I also get a section:

-Ch2048 results in this:

.section .data.n___heapsize
         .balign 4
.globl  __heapsize
__heapsize:
         .long   2048

but I do not seem to be able to access __heapsize in my code, how could 
I use this information to make the heapsize configurable?

and, it is (as far as I understand) a must to include heapmgr in the 
code because if not the program will always die on startup because of 
the object created in Exception class, where would be the right place to 
do this?

Yes, I can put heapmgr as the first unit included in my main program, 
but wouldn't it be better to put it in some global initialization loop, 
like the VariantManager, which always gets initialized without me 
triggering this in any way.

Michael




Index: rtl/embedded/heapmgr.pp
===================================================================
--- rtl/embedded/heapmgr.pp    (revision 23681)
+++ rtl/embedded/heapmgr.pp    (working copy)
@@ -34,6 +34,7 @@

      var
        Blocks: PHeapBlock = nil;
+      InitialBlock: array[0..255] of byte;

      procedure InternalFreeMem(Addr: Pointer; Size: ptruint); forward;

@@ -242,6 +243,7 @@

  initialization
    SetMemoryManager(MyMemoryManager);
+  RegisterHeapBlock(@InitialBlock,sizeof(InitialBlock));
  finalization
    //FinalizeHeap;
  end.







Am 03.03.13 09:52, schrieb Florian Klämpfl:
> Am 02.03.2013 23:17, schrieb Michael Ring:
>> I am not sure if I have hit work in progress code here....
>>
>> my arm-embedded app crashes during initialization, the reason is that
>> now exceptions are part of the rtl but there is no properly initialized
>> heapmanager:
>>
>> Procedure InitExceptions;
>> {
>>    Must install uncaught exception handler (ExceptProc)
>>    and install exceptions for system exceptions or signals.
>>    (e.g: SIGSEGV -> ESegFault or so.)
>> }
>> begin
>>    ExceptionClass := Exception;
>>    ExceptProc:=@CatchUnhandledException;
>>    // Create objects that may have problems when there is no memory.
>>    //CRASH BOOM BANG in the following line
>>    OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
>>
>>
>> Creating the object fails because the MemoryManager is not properly
>> initialized yet.
>>
>> When I put heapmgr unit before sysutils im my program (this should not
>> be necesary, right???)
> Yes, this is a requirement.
>
>> I can debug a little deeper in object
>> initialization but still the root cause exists that there has never been
>> a call to RegisterHeapBlock so objects cannot be initiated because
>> there's no memory block defined that heapmgr can draw memory from.
>>
>> I can hack the RegisterHeapBlock call into the initialization code of
>> the unit heapmgr but this is more a hack than a proper solution -->
>> seems a bad idea to me.
>>
>> How can this be properly solved?
> I think allocating a small block (128 bytes for arm?) during the start
> up of heapmgr should be fine?
>
> var
>    InitialBlock: array[0..255] of byte;
>
> ...
> RegisterHeapBlock(@InitialBlock,sizeof(InitialBlock));
>
> If one includes heapmgr he wants to use the heap after all.
>
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel
>




More information about the fpc-devel mailing list