ATTiny404: After 1556 calls to analogWrite, sketch restarts. #1129
Replies: 1 comment
-
You fail to check that malloc was successful. You just attempted to allocate an array 4x the size of its total ram. Unlikely to have suceeded. so you got back a null pointer, never tested for it and proceeded to wreak havok. And the malloc machinery you should not be using is eating approximately half of your flash, too. Note that writing to a null pointer is perfectly fucking valid! addressed 0-64 are the low and high I/O addresses, these are SFRs with extremely powerful bit-tests and sets. On Modern AVR, writing to 0x0000 is VPORTA.DIR, - 0x0000-0x001F, the low I/O space (with SBIS, SBIC, and that good stuff that wizards can use to make asm run faster than greased lightning. In the bad old days that was a random assortment of "useful" registers, now 0x0000 to 0x001B are the 4 VPORT registers per port, and the last 4 are the GPR/GPIOR registers, then above that are the 32 high I/O space registers. Most of these are read-only-memory that always reads 0 or 0xFF, but that's also where the SREG and stack pointer are. Right, so using a pointer you get from malloc without testing if it is nul is always unsafe. Don't do it. You are on a 4k AVR with 256b of RAM! Code like it! No malloc! Allocate statically. Variables not declared volatile and provable not accessed during accessible code paths are ignored, which is why your massive array that could be optimized away long before the size check didn't exceed memory bounds at compile time (and malloc failures must be caught at runtime), Closing as this is not a defect in core |
Beta Was this translation helpful? Give feedback.
-
In the attached code, I make continuous calls to analogWrite. After 1556 calls, the sketch appears to crash and restart. It occurs regardless of which port I use. It also seems unaffected by malloc'ing 1k of memory or adding a useless 1k byte array, which suggests the problem is not a memory leak.
The attached file is actually an INO file, renamed to TXT to make it acceptable as an attachment.
Thanks for any help anyone is able to render!
Michael
AnalogWrite_Test.txt
Beta Was this translation helpful? Give feedback.
All reactions