I have been trying to find a bug in my code, but I'm struggling to find what is going on. Now I was able to find through debugging that the software restarts from the beginning of main all the time (after initialization code, a few interrupts and main loop). The RST.STATUS shows no reset causes (after clearing the flags just after the start of debugging) thus the device is not booting, just the software restarting. Also the counters (TCC0/1) are not reset. I have checked that there is a lot of free space for the stack. There are two timer overflow interrupts (25 and 500 Hz). Sometimes the restart happens right away and sometimes after a few seconds.
Now I even get the device to a state where one TCC0 counter doesn't count anymore, although IO view shows all the TCC0 registers unchanged. TCC1 still runs. I also was able to stop the debugging just before I think it would restart. Then the program counter was at 0x63D2 and the disassembly showed "memory out of bounds" or read error". Address on top of the window showed _vector_20 and main shows the cursor at the end of TCC1_OVF_vect.
The software is in C, but debugging it spends a lot of time in assembler, which I don't know that well. Thus I can't really follow what is happening. I use AS 6.2 and avrgcc 126.96.36.199, which came with it, but I compile from command line. The device is xmega32a4 on a PCB I have designed, but it has just the device and a regulator. I have used it for testing different things.