Yes, I'd spotted that and corrected for it. Here's a little more detail on the error:
Code: Select all
I (2227) APP: Server ready...
I (312675) APP: Opening file /spiffs/index.htm, size: 928
I (312676) APP: 928 bytes read
CORRUPT HEAP: multi_heap.c:429 detected at 0x70732f3c
abort() was called at PC 0x4008e04f on core 1
0x4008e04f: multi_heap_assert at C:/msys32/home/user/esp-idf/components/heap/multi_heap.c:377
(inlined by) multi_heap_malloc_impl at C:/msys32/home/user/esp-idf/components/heap/multi_heap.c:429
Backtrace: 0x4008e6e8:0x3ffbe270 0x4008e8bd:0x3ffbe290 0x4008e04f:0x3ffbe2b0 0x40082805:0x3ffbe2d0 0x40082831:0x3ffbe2f0 0x40082e3d:0x3ffbe310 0x4000beaf:0x
3ffbe330 0x40131d24:0x3ffbe350 0x4012c657:0x3ffbe370 0x40129d30:0x3ffbe390 0x4012a24d:0x3ffbe3b0 0x40127e9e:0x3ffbe420 0x40128d0d:0x3ffbe440 0x40129159:0x3f
fbe460 0x4008c62d:0x3ffbe490
0x4008e6e8: invoke_abort at C:/msys32/home/user/esp-idf/components/esp32/panic.c:660
0x4008e8bd: abort at C:/msys32/home/user/esp-idf/components/esp32/panic.c:660
0x4008e04f: multi_heap_assert at C:/msys32/home/user/esp-idf/components/heap/multi_heap.c:377
(inlined by) multi_heap_malloc_impl at C:/msys32/home/user/esp-idf/components/heap/multi_heap.c:429
0x40082805: heap_caps_malloc at C:/msys32/home/user/esp-idf/components/heap/heap_caps.c:130
0x40082831: heap_caps_malloc_default at C:/msys32/home/user/esp-idf/components/heap/heap_caps.c:130
0x40082e3d: _malloc_r at C:/msys32/home/user/esp-idf/components/newlib/syscalls.c:37
0x40131d24: mem_malloc at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/core/mem.c:124
0x4012c657: pbuf_alloc at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/core/pbuf.c:1331
0x40129d30: tcp_pbuf_prealloc at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/core/tcp_out.c:268
0x4012a24d: tcp_write at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/core/tcp_out.c:572
0x40127e9e: lwip_netconn_do_writemore at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/api/api_msg.c:1427 (discriminator 6)
0x40128d0d: lwip_netconn_do_write at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/api/api_msg.c:1819
0x40129159: tcpip_thread at C:/msys32/home/user/esp-idf/components/lwip/lwip/src/api/tcpip.c:483
0x4008c62d: vPortTaskWrapper at C:/msys32/home/user/esp-idf/components/freertos/port.c:401
CPU halted.
Here's my code:
Code: Select all
char *readFile(char *fname)
{
int size, res;
char *buf;
struct stat st;
if (stat(fname, &st) == 0) {
size = st.st_size;
} else {
ESP_LOGE(TAG, " Error - cannot find file!");
return NULL;
}
ESP_LOGI(TAG, " Opening file %s, size: %d", fname, size);
FILE *fd = fopen(fname, "rb");
if (fd == NULL) {
ESP_LOGE(TAG, " Error opening file");
return NULL;
}
if (size == 0) {
ESP_LOGE(TAG, " Error - file size zero");
return NULL;
}
buf = calloc(size, 1); // File buffer
if (buf == NULL) {
ESP_LOGE(TAG, " Error allocating read buffer");
free(buf);
fclose(fd);
return NULL;
}
res = size;
res = fread(buf, 1, size, fd);
if (res <= 0) {
ESP_LOGE(TAG, " Error reading from file");
}
else {
ESP_LOGI(TAG, " %d bytes read", res);
buf[res] = '\0';
}
res = fclose(fd);
if (res) {
ESP_LOGE(TAG, " Error closing file");
}
free(buf);
return((char*)buf);
}
esp_err_t index_get_handler(httpd_req_t *req)
{
/* Send response with custom headers and body set as the
* string passed in user context*/
// Get the file to return
char* resp_str = (char*) readFile("/spiffs/index.htm");
// Set the response headers
httpd_resp_set_hdr(req, "status", "200");
httpd_resp_set_hdr(req, "content-type", "text/html; charset=UTF-8");
httpd_resp_set_hdr(req, "server", "ESP32-10103");
// Return the response
httpd_resp_send(req, resp_str, strlen(resp_str));
free(resp_str);
return ESP_OK;
}
No errors during assembly. I'd say it's definitely the read routine that's causing the problem - almost certainly because memory space isn't getting freed from unused variables or something to do with the intricacies of mixing pointers and other var types, but as I said earlier, I'm brand-new to C and learning it on the fly, so I have no idea what the problem is or how to fix it.