Skip to content

Commit

Permalink
Merge pull request #647 from FujiNetWIFI/find-memory-leaks
Browse files Browse the repository at this point in the history
Find memory leaks
  • Loading branch information
tschak909 authored Jul 27, 2023
2 parents 02184be + 2821cea commit 49d7a37
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 136 deletions.
4 changes: 4 additions & 0 deletions lib/bus/sio/sio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ void systemBus::_sio_process_cmd()
if (it != _daisyChain.end())
{
_activeDev = it->second;
// heap_trace_start(HEAP_TRACE_LEAKS);
it->second->sio_process(tempFrame.commanddata, tempFrame.checksum);
// heap_trace_stop();
// Debug_printv("heap trace follows.");
// heap_trace_dump();
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions lib/device/rs232/modem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ void rs232Modem::rs232_poll_3(uint8_t device, uint8_t aux1, uint8_t aux2)
if (respond == false)
return;

// Get size of handler
int filesize = fnSystem.load_firmware(FIRMWARE_850HANDLER, NULL);

// Simply return (without ACK) if we failed to get this
if (filesize < 0)
return;
Expand Down Expand Up @@ -258,7 +255,6 @@ void rs232Modem::rs232_send_firmware(uint8_t loadcommand)

// Load firmware from file
uint8_t *code;
int codesize = fnSystem.load_firmware(firmware, &code);
// NAK if we failed to get this
if (codesize < 0 || code == NULL)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/device/sio/fuji.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1868,4 +1868,4 @@ std::string sioFuji::get_host_prefix(int host_slot)
return _fnHosts[host_slot].get_prefix();
}

#endif /* BUILD_ATARI */
#endif /* BUILD_ATARI */
5 changes: 3 additions & 2 deletions lib/device/sio/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ void sioNetwork::sio_close()
else
sio_complete();

Debug_printf("Before protocol delete %lu\n",esp_get_free_heap_size());
Debug_printv("Before protocol delete %lu\n",esp_get_free_internal_heap_size());
// Delete the protocol object
delete protocol;
protocol = nullptr;

if (json != nullptr)
delete json;

Debug_printf("After protocol delete %lu\n",esp_get_free_heap_size());
Debug_printv("After protocol delete %lu\n",esp_get_free_internal_heap_size());
}

/**
Expand Down Expand Up @@ -245,6 +245,7 @@ void sioNetwork::sio_read()
// And send off to the computer
bus_to_computer((uint8_t *)receiveBuffer->data(), num_bytes, err);
receiveBuffer->erase(0, num_bytes);
receiveBuffer->shrink_to_fit();
}

/**
Expand Down
96 changes: 38 additions & 58 deletions lib/fnjson/fnjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void FNJSON::setReadQuery(string queryString, uint8_t queryParam)
_queryString = queryString;
_queryParam = queryParam;
_item = resolveQuery();
json_bytes_remaining=readValueLen();
json_bytes_remaining = readValueLen();
}

/**
Expand All @@ -88,9 +88,8 @@ string FNJSON::processString(string in)

if (endpos != string::npos)
{
in.erase(startpos,endpos-startpos);
in.erase(startpos, endpos - startpos);
}

}

#ifdef BUILD_IEC
Expand All @@ -105,78 +104,74 @@ string FNJSON::processString(string in)
*/
string FNJSON::getValue(cJSON *item)
{
stringstream ss;

if (cJSON_IsString(item))
{
stringstream ss;

Debug_printf("S: [cJSON_IsString] %s\r\n",cJSON_GetStringValue(item));
Debug_printf("S: [cJSON_IsString] %s\r\n", cJSON_GetStringValue(item));

ss << cJSON_GetStringValue(item);

#ifdef BUILD_ATARI
#ifdef BUILD_ATARI

// SIO AUX bits 0+1 control the mapping
// Bit 0=0 - don't touch the characters
// Bit 0=1 - convert the characters when possible
// Bit 1=0 - convert to generic ASCII/ATASCII (no font change needed)
// Bit 1=1 - convert to ATASCII international charset (need to be switched on ATARI, i.e via POKE 756,204)

// SIO AUX2 Bit 1 set?
if ((_queryParam & 1) != 0)
{
// yes, map special characters
string str_utf8mapping = ss.str();
string str_utf8mapping = ss.str();
Debug_printf("S: [Mapping->ATARI]\r\n");

// SIO AUX2 Bit 2 set?
if ((_queryParam & 2) != 0)
{
// yes, mapping to international charset
string mapFrom[] = { "á", "ù", "Ñ", "É", "ç", "ô", "ò", "ì", "£", "ï", "ü", "ä", "Ö", "ú", "ó", "ö", "Ü", "â", "û", "î", "é", "è", "ñ", "ê", "å", "à", "Å", "¡", "Ä", "ß" };
string mapTo[] = { "\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\x09", "\x0a", "\x0b", "\x0c", "\x0d", "\x0e", "\x0f", "\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17", "\x18", "\x19", "\x1a", "\x60", "\x7b", "ss" };
int elementCount = sizeof(mapFrom)/sizeof(mapFrom[0]);
for (int elementIndex=0; elementIndex < elementCount; elementIndex++)
if(str_utf8mapping.find(mapFrom[elementIndex]) != std::string::npos)
string mapFrom[] = {"á", "ù", "Ñ", "É", "ç", "ô", "ò", "ì", "£", "ï", "ü", "ä", "Ö", "ú", "ó", "ö", "Ü", "â", "û", "î", "é", "è", "ñ", "ê", "å", "à", "Å", "¡", "Ä", "ß"};
string mapTo[] = {"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\x09", "\x0a", "\x0b", "\x0c", "\x0d", "\x0e", "\x0f", "\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17", "\x18", "\x19", "\x1a", "\x60", "\x7b", "ss"};
int elementCount = sizeof(mapFrom) / sizeof(mapFrom[0]);
for (int elementIndex = 0; elementIndex < elementCount; elementIndex++)
if (str_utf8mapping.find(mapFrom[elementIndex]) != std::string::npos)
str_utf8mapping.replace(str_utf8mapping.find(mapFrom[elementIndex]), string(mapFrom[elementIndex]).size(), mapTo[elementIndex]);
}
else
{
// no, mapping to normal ASCI (workaround)
string mapFrom[] = { "Ä", "Ö", "Ü", "ä", "ö", "ü", "ß", "é", "è", "á", "à", "ó", "ò", "ú", "ù" };
string mapTo[] = { "Ae", "Oe", "Ue", "ae", "oe", "ue", "ss", "e", "e", "a", "a", "o", "o", "u", "u" };
int elementCount = sizeof(mapFrom)/sizeof(mapFrom[0]);
for (int elementIndex=0; elementIndex < elementCount; elementIndex++)
if(str_utf8mapping.find(mapFrom[elementIndex]) != std::string::npos)
string mapFrom[] = {"Ä", "Ö", "Ü", "ä", "ö", "ü", "ß", "é", "è", "á", "à", "ó", "ò", "ú", "ù"};
string mapTo[] = {"Ae", "Oe", "Ue", "ae", "oe", "ue", "ss", "e", "e", "a", "a", "o", "o", "u", "u"};
int elementCount = sizeof(mapFrom) / sizeof(mapFrom[0]);
for (int elementIndex = 0; elementIndex < elementCount; elementIndex++)
if (str_utf8mapping.find(mapFrom[elementIndex]) != std::string::npos)
str_utf8mapping.replace(str_utf8mapping.find(mapFrom[elementIndex]), string(mapFrom[elementIndex]).size(), mapTo[elementIndex]);
}

ss.str(str_utf8mapping);
Debug_printf("S: [Mapping->ATARI] %s\r\n",ss.str().c_str());
Debug_printf("S: [Mapping->ATARI] %s\r\n", ss.str().c_str());
}
#endif

return processString(ss.str() + lineEnding);
#endif
ss << processString(cJSON_GetStringValue(item) + lineEnding);
}
else if (cJSON_IsBool(item))
{
Debug_printf("S: [cJSON_IsBool] %s\r\n",cJSON_IsTrue(item) ? "true" : "false");
Debug_printf("S: [cJSON_IsBool] %s\r\n", cJSON_IsTrue(item) ? "true" : "false");

if (cJSON_IsTrue(item))
return "TRUE" + lineEnding;
ss << "TRUE" + lineEnding;
else if (cJSON_IsFalse(item))
return "FALSE" + lineEnding;
ss << "FALSE" + lineEnding;
}
else if (cJSON_IsNull(item))
{
Debug_printf("S: [cJSON_IsNull]\r\n");

return "NULL" + lineEnding;
ss << "NULL" + lineEnding;
}
else if (cJSON_IsNumber(item))
{
stringstream ss;

Debug_printf("S: [cJSON_IsNumber] %f\r\n",cJSON_GetNumberValue(item));
Debug_printf("S: [cJSON_IsNumber] %f\r\n", cJSON_GetNumberValue(item));

// Is the number an integer?
if (floor(cJSON_GetNumberValue(item)) == cJSON_GetNumberValue(item))
Expand All @@ -187,51 +182,43 @@ string FNJSON::getValue(cJSON *item)
else
{
// no, return as double with max. 10 digits
ss << setprecision(10)<<cJSON_GetNumberValue(item);
ss << setprecision(10) << cJSON_GetNumberValue(item);
}

return ss.str() + lineEnding;
ss << lineEnding;
}
else if (cJSON_IsObject(item))
{
string ret = "";

item = item->child;

do
{
ret += string(item->string) + lineEnding + getValue(item);
ss << item->string + lineEnding + getValue(item);
} while ((item = item->next) != NULL);

return ret;
}
else if (cJSON_IsArray(item))
{
cJSON *child = item->child;
string ret;

do
{
ret += getValue(child);
ss << getValue(child);
} while ((child = child->next) != NULL);

return ret;
}

return "UNKNOWN" + lineEnding;
else
ss << "UNKNOWN" + lineEnding;

return ss.str();
}

/**
* Return requested value
*/
bool FNJSON::readValue(uint8_t *rx_buf, unsigned short len)
{
{
if (_item == nullptr)
return true; // error

string ret = getValue(_item);

memcpy(rx_buf, ret.data(), len);
memcpy(rx_buf, getValue(_item).data(), len);

return false; // no error.
}
Expand All @@ -253,7 +240,6 @@ int FNJSON::readValueLen()
bool FNJSON::parse()
{
NetworkStatus ns;
_parseBuffer.clear();

if (_json != nullptr)
cJSON_Delete(_json);
Expand All @@ -275,7 +261,7 @@ bool FNJSON::parse()
vTaskDelay(10);
}

Debug_printf("S: %s\r\n",_parseBuffer.c_str());
Debug_printf("S: %s\r\n", _parseBuffer.c_str());
_json = cJSON_Parse(_parseBuffer.c_str());

if (_json == nullptr)
Expand All @@ -284,18 +270,12 @@ bool FNJSON::parse()
return false;
}

char* debugOutput = cJSON_Print(_json);
if (debugOutput != nullptr) {
Debug_printf("Parsed JSON: %s\r\n", debugOutput);
cJSON_free(debugOutput);
}

return true;
}

bool FNJSON::status(NetworkStatus *s)
{
Debug_printf("FNJSON::status(%u) %s\r\n",json_bytes_remaining,getValue(_item).c_str());
Debug_printf("FNJSON::status(%u) %s\r\n", json_bytes_remaining, getValue(_item).c_str());
s->connected = true;
s->rxBytesWaiting = json_bytes_remaining;
s->error = json_bytes_remaining == 0 ? 136 : 0;
Expand Down
22 changes: 5 additions & 17 deletions lib/hardware/fnSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,9 @@ uint32_t SystemManager::get_psram_size()

/*
If buffer is NULL, simply returns size of file. Otherwise
allocates buffer for reading file contents. Buffer must be freed by caller.
allocates buffer for reading file contents. Buffer must be managed by caller.
*/
int SystemManager::load_firmware(const char *filename, uint8_t **buffer)
int SystemManager::load_firmware(const char *filename, uint8_t *buffer)
{
Debug_printf("load_firmware '%s'\r\n", filename);

Expand All @@ -551,25 +551,13 @@ int SystemManager::load_firmware(const char *filename, uint8_t **buffer)
}

int bytes_read = -1;
uint8_t *result = (uint8_t *)malloc(file_size);
if (result == NULL)
if (buffer == NULL)
{
Debug_println("load_firmware failed to malloc");
Debug_println("load_firmware passed in buffer was NULL");
}
else
{
bytes_read = fread(result, 1, file_size, f);
if (bytes_read == file_size)
{
*buffer = result;
}
else
{
free(result);
bytes_read = -1;

Debug_printf("load_firmware only read %u bytes out of %u - failing\r\n", bytes_read, file_size);
}
bytes_read = fread(buffer, 1, file_size, f);
}

fclose(f);
Expand Down
2 changes: 1 addition & 1 deletion lib/hardware/fnSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class SystemManager
void delete_tempfile(FileSystem *fs, const char *filename);
void delete_tempfile(const char *filename);

int load_firmware(const char *filename, uint8_t **buffer);
int load_firmware(const char *filename, uint8_t *buffer);
void debug_print_tasks();

void check_hardware_ver();
Expand Down
Loading

0 comments on commit 49d7a37

Please sign in to comment.