diff --git a/lib/device/sio/network.cpp b/lib/device/sio/network.cpp index 61b3ee148..b8807a75f 100755 --- a/lib/device/sio/network.cpp +++ b/lib/device/sio/network.cpp @@ -83,6 +83,14 @@ void sioNetwork::sio_open() sio_ack(); + newData = (uint8_t *)malloc(NEWDATA_SIZE); + + if (!newData) + { + Debug_printv("Could not allocate write buffer\n"); + sio_error(); + } + channelMode = PROTOCOL; // Delete timer if already extant. @@ -123,6 +131,9 @@ void sioNetwork::sio_open() protocolParser = nullptr; } + if (newData) + free(newData); + sio_error(); return; } @@ -139,6 +150,10 @@ void sioNetwork::sio_open() delete protocolParser; protocolParser = nullptr; } + + if (newData) + free(newData); + sio_error(); return; } @@ -198,6 +213,9 @@ void sioNetwork::sio_close() if (json != nullptr) delete json; + if (newData) + free(newData); + Debug_printv("After protocol delete %lu\n",esp_get_free_internal_heap_size()); } @@ -293,7 +311,6 @@ void sioNetwork::sio_write() unsigned short num_bytes = sio_get_aux(); bool err = false; - uint8_t *newData = (uint8_t *)malloc(num_bytes); Debug_printf("sioNetwork::sio_write( %d bytes)\n", num_bytes); if (newData == nullptr) @@ -315,14 +332,12 @@ void sioNetwork::sio_write() } status.error = NETWORK_ERROR_NOT_CONNECTED; sio_error(); - free(newData); return; } // Get the data from the Atari bus_to_peripheral(newData, num_bytes); *transmitBuffer += string((char *)newData, num_bytes); - free(newData); // Do the channel write err = sio_write_channel(num_bytes); diff --git a/lib/device/sio/network.h b/lib/device/sio/network.h index db5c0cdb7..17983373c 100755 --- a/lib/device/sio/network.h +++ b/lib/device/sio/network.h @@ -254,6 +254,12 @@ class sioNetwork : public virtualDevice */ unsigned short json_bytes_remaining=0; + /** + * @brief the write buffer + */ + uint8_t *newData; +#define NEWDATA_SIZE 65535 + /** * Instantiate protocol object * @return bool TRUE if protocol successfully called open(), FALSE if protocol could not open