Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I have an executable that crashes after main has finished all its instructions, and the last assembler instruction is executed. Just before the program crashes, I do disas on GDB:

   0x2001c054 <+84>:    movt    r3, #16384      ; 0x4000
   0x2001c058 <+88>:    ldrh    r3, [r3, #16]
   0x2001c05a <+90>:    uxth    r3, r3
   0x2001c05c <+92>:    str     r3, [sp, #0]
   0x2001c05e <+94>:    bl      0x2001c0f8 <scream>
   0x2001c062 <+98>:    mov.w   r3, #0
   0x2001c066 <+102>:   mov     r0, r3
   0x2001c068 <+104>:   add     sp, #28
=> 0x2001c06a <+106>:   pop     {pc}
End of assembler dump.

Now if I go to the next instruction (ni in GDB) I get the following in GDB:

0xfffffffe in ?? ()

Also, OpenOCD (which I use to communicate with my STM32F2 board) gives the following error:

Error: address + size wrapped(0xfffffffe, 0x00000004)

How can I tell my executable to exit gracefully?

share|improve this question
1  
You could use an infinite loop, or just set the controller in a wait state, better if standby or sleep mode – clabacchio Mar 22 '12 at 16:37
Yes, but that feels like a hack. – Randomblue Mar 22 '12 at 17:04
4  
It's not a "hack". Do you think that Linux or Windows' kernels ever return from main? A program on a microcontroller (unless running under an existing OS) is more like an OS kernel. Never exit. – Toby Jaffey Mar 22 '12 at 17:21
Yeah, Joby is right - that's how microcontrollers work. Expand your mind man. It's a whole new world down here. – AngryEE Mar 23 '12 at 15:13

2 Answers

up vote 7 down vote accepted

Is this on a microcontroller? If so, you should never exit main() - use an infinite loop instead.

share|improve this answer

If this is an operating system or other app on the bare metal, main should never exit. If you really must exit, then your C startup code could reset the board after main.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.