Closed mriksman closed 2 years ago
From the docs;
LoadProhibited, StoreProhibited This CPU exception happens when application attempts to read from or write to an invalid memory location. The address which was written/read is found in EXCVADDR register in the register dump. If this address is zero, it usually means that application attempted to dereference a NULL pointer. If this address is close to zero, it usually means that application attempted to access member of a structure, but the pointer to the structure was NULL
Somewhere around the snprintf
or memcpy
in the client_send_chunk
function?
void client_send_chunk(byte *data, size_t size, void *arg) {
client_context_t *context = arg;
size_t payload_size = size + 8;
byte *payload = malloc(payload_size);
int offset = snprintf((char *)payload, payload_size, "%x\r\n", size);
memcpy(payload + offset, data, size);
payload[offset + size] = '\r';
payload[offset + size + 1] = '\n';
client_send(context, payload, offset + size + 2);
free(payload);
}
There are a few calls in server.c
that have
client_send_chunk(NULL, 0, context);
But then, wouldn't memcpy
be trying to use a NULL
pointer for the src
parameter?
Granted, my knowledge isn't great with programming, but this seems strange...?
I added a line in between the sprintf
and memcpy
to print out the address pointed to by data
. Indeed, it has NULL
many times, but it doesn't usually cause a crash...
>>> HomeKit: [Client 55] Get Accessories
>>> HomeKit: Data pointer 0x4010b498
>>> HomeKit: Data pointer 0x4010b498
>>> HomeKit: Data pointer 0x4010b498
>>> HomeKit: Data pointer 0
I think you're running out of memory. When I was implementing this framework there was a consideration what to do when there is not enough memory. Back in the days I decided not to do anything (and thus it will just crash). Since I saw more of this nowadays, I'm looking to optimize memory usage (and memory fragmentation), but it looks like putting a check after every malloc will make compiled framework size bigger, so I'm still hesitant to merge those checks (you can check my "memory-handling-improvements" branch and see if it makes it clearer what goes wrong.
As in HEAP? I check this, and it doesn’t seem to be dropping. Hovers around 30000. After 12 hours;
HEAP 30360
sys_evt X 10 1760 4
Tmr Svc R 2 1064 3
IDLE R 0 784 2
HomeKit Server B 1 7768 8
tiT B 8 1388 5
ppT B 13 648 6
mdns B 1 2192 10
pmT B 11 624 7
The second-last column is the 'high-water mark' which indicates the lowest free task stack space it has reached. HomeKit Server seems to have plenty.
I'll keep monitoring it.
I have realized that the memory ends at some point, but is randomly happening, it does run out of memory, it just cannot allocate enough memory and keeps looping in the methods that can run, specifically the methods I noticed die because of lack of memory are the mDNS. So as Maxim says, if you run out of ram, it is complex to free more memory due to the fragmentation done during its lifetime. In my case I decided to reboot the chip specifically in the mDNS library because is the one detecting the problems with memory allocation
I'm constantly try to improve stability of esp-homekit framework. There is an experimental branch "memory-handling-improvements" that has work on optimizing memory usage and detect memory issues as they occur. I'm still haven't decided if all changes in this branch need to make it into master, but it might help your case. Check it out and see if it improves your experience.
Leave the case open for now - I'll be setting up a few to test in the coming days/weeks, and will test both branches.
I no longer have any issues with memory and random crashes.
Thanks for confirming that!
Just letting the device sit there on its own to see how stable it is. Have seen this twice now;
Anyone have any ideas?