I am trying to create an object to represent a menu entry, which a user will see displayed on a small TFT screen and make a selection.
However, I don't think I am storing my strings right and they end up getting garbled when both displayed to serial and to the TFT.
Header:
class MenuEntry {
private:
int id;
const void* label; // void to handle both flash memory pointers and pointers to Strings
bool flash;// flag to determine if pointer is flash or String
bool clearDataOnDestruct = true;
MenuEntry(int idIn, const void* labelIn, bool isFlashIn);
public:
/**
* don't use, for handling arrays of MenuEntries
*/
MenuEntry();
MenuEntry(int idIn, const String* labelIn);
MenuEntry(int idIn, const char* labelIn);
MenuEntry(int idIn, const __FlashStringHelper * labelIn);
int getId();
bool isFlash();
const String* getLabelStr();
const __FlashStringHelper* getLabelFlash();
~MenuEntry();
};
Implementation:
MenuEntry::MenuEntry(){
}
MenuEntry::MenuEntry(int idIn, const void* labelIn, bool isFlashIn){
id = idIn;
label = labelIn;
flash = isFlashIn;
}
MenuEntry::MenuEntry(int idIn, const String* labelIn){
MenuEntry(idIn, labelIn, false);
}
MenuEntry::MenuEntry(int idIn, const char* labelIn){
String* labelStr = new String(labelIn);
MenuEntry(idIn, labelStr);
}
MenuEntry::MenuEntry(int idIn, const __FlashStringHelper * labelIn){
MenuEntry(idIn, labelIn, true);
}
int MenuEntry::getId(){
return id;
}
bool MenuEntry::isFlash(){
return flash;
}
const String* MenuEntry::getLabelStr(){
return (const String*)label;
}
const __FlashStringHelper* MenuEntry::getLabelFlash(){
return (const __FlashStringHelper*)label;
}
MenuEntry::~MenuEntry(){
}
Example of Usage:
const char* dir = "/";
const int numFiles = getNumFilesInDir(dir);
MenuEntry* entries = new MenuEntry[numFiles];
writeSerialLine(F("created arr of entries"));
File root = SD.open(dir);
writeSerialLine(F("Opened root"));
writeSerialLine(F("Getting File List"));
CUR_TAB_LEVEL++;
{
int i = 0;
File curEntry;
do {
curEntry = root.openNextFile();
const char testResult = validFile(&curEntry);
if(testResult == -1){
break;
}
if(!testResult){
continue;
}
entries[i] = MenuEntry(i, curEntry.name());
writeSerial(i);
writeSerial(F(" "));
// string that comes back is garbled
String* labelStr = entries[i].getLabelStr();
writeSerial(&labelStr);
i++;
}while(true);
}
...
I assume that I am missing an aspect of Memory Management in C, and that the char arrays are not being kept around after the initial pointer set. Could anyone nudge me in the right direction?