Problems with library destructor

Hello everyone,

I created a library and I'm having problems with the destructor, I think it's not running because it releases very little memory and doesn't shows the Serial.println in destructor.

These are the classes involved, am I deleting the variables right? or maybe I'm missing variables to erase.

WindowDirectories

class WindowDirectories : public Window {
private:
/*
 * Todos los botones de la ventana
 * TODO: agrupar los botones FILE
 */
typedef enum BUTTONS
{
	BUTTON_1,
...
	BUTTON_MAX
};
/*
 * Estructura que contiene el indice y longitud de los archivos a mostrar
 */
typedef struct STR_DIR
{
	long addr;
	long len;
	bool isDir; //indica si un archivo es directorio o no
	bool filesSelected; //indica si un archivo esta seleccionado
};
	STR_TO_COPY *TO_COPY; //Estructura de copiado
	WindowButton **buttons; //array con todos los botones
	int numFiles; //numero de archivos en el directorio
	STR_DIR *files; //array con el nombre de todos los archivos en el directorio
	char *path; //path actual
	int currentFile; //primer archivo que se esta mostrando en la division actual del scroll, avanza segun MAX_FILES
	byte buttonFocused; //contiene el archivo que tiene actualmente el foco (se ha pulsado sobre el)
	int numSelectedFiles; //numero de archivos seleccionados, utilizado para activar y desactivar los botones de seleccion y controlar el numero de archivos a copiar
	bool isCopying; //true si se estan copiando archivos
	bool isCopyReady; //true si ahi elementos para copiar
	SpiRAM *showDirMemory;
	SpiRAM *copyMemory;

	
public:	
	WindowDirectories();
	~WindowDirectories();
WindowDirectories::~WindowDirectories()
{
	Serial.println("Destructor");
	for (int i = 0; i < BUTTON_MAX; i++ )
	{
		delete this->buttons[i];
	}
	delete [] this->buttons;
	
	delete [] TO_COPY->addr;
	delete [] TO_COPY->len;
	delete [] TO_COPY->path;
	delete TO_COPY;
	if ( this->numFiles > 0 )
	{
		delete [] this->files;	
	}
	delete [] path;
}

WindowButton

class WindowButton{
public:	
/*
 * Enumeracion de los textos constantes almacenados en la memoria flash
 */
typedef enum TEXTS {
	TEXT_ACTIONS,
	TEXT_ABOUT,
	TEXT_SELECT,
	TEXT_OPEN,
	TEXT_COPY,
	TEXT_UNSELECT,
	TEXT_UNSELECT_ALL,
	TEXT_PASTE,
	TEXT_BACK_USB,
	TEXT_GO_BACK,
	TEXT_CANCEL_COPY,
	TEXT_YES,
	TEXT_NO,
	TEXT_RENAME,
	TEXT_REPLACE
};
/*
 * Enumeracion de los textos constantes almacenados en la memoria flash
 */
typedef enum IMAGE_TYPE {
	IMAGE_TYPE_NONE,
	IMAGE_TYPE_FOLDER,
	IMAGE_TYPE_FILE
};

protected:
	SMARTGPU2 *lcd; //objeto sobre el que se dibuja            
	bool isActive; //controla si se puede pulsar el boton o no
	POINT min; //posicion minima del rectangulo de pulsacion
	POINT max; //posicion maxima del rectangulo de pulsacion
	ACTIONS action; //accion que ejecuta el boton
	POINT coordText1; //posicion minima donde aparecera el texto
	POINT coordText2; //posicion maxima donde aparecera el texto
	char *dynamicText; //texto a mostrar
	bool bCanDeleteDynamicText;
	bool bDynamicText;
	TEXTS textIndex;
	COLOUR colour; //color del texto
	FONTSIZE font; //tamano de la fuente
	bool isDrawText; //controla si se dibuja o no el texto
	bool isDrawBackground; //controla si se dibuja o no el fondo
	bool isSelected; //controla si esta seleccionado o no el boton
	bool isFocused; //controla si tiene el foco o no
	IMAGE_TYPE imageType; // controla si se tiene que dibujar una imagen y que imagen
	
public:	
	WindowButton();
	~WindowButton();
WindowButton::WindowButton()
{
	this->dynamicText = 0;
}
WindowButton::~WindowButton()
{
	Serial.println("destruct");
	if ( this->dynamicText && this->bCanDeleteDynamicText ){ delete [] this->dynamicText; }
	else this->dynamicText = 0;
}

Thank you very much

Window.cpp (1.27 KB)

Window.h (1.21 KB)

WindowButton.cpp (5.8 KB)

WindowButton.h (3.43 KB)

WindowDirectories.cpp (25.4 KB)

WindowDirectories.h (2.76 KB)

Without a sketch that instances those classes, we can't tell why your destructor is not being called.

To test this Im calling this function several times in the setup:

void createWindowDirectories()
{
    printf_P(PSTR("createWindowDirectories freeMemory()= %d\r\n"), freeMemory());
    delete currentWindow;
    printf_P(PSTR("createWindowDirectories  delete currentWindow; freeMemory()= %d\r\n"), freeMemory());
    currentWindow = new WindowDirectories();
    currentWindow->init(&lcd, &showDirMemory, &copyMemory);
    currentWindow->reset();
    printf_P(PSTR("createWindowDirectories  end freeMemory()= %d\r\n"), freeMemory());    
}

`

void setup() {
        while(!Serial);
        Serial.begin(115200);
        delay(10000);

         lcd.init();
          lcd.start();
          lcd.baudChange(BAUD6); 
          lcd.SDFopenDir("Utils");
        
        Serial.print("freeMemory()=");
          Serial.println(freeMemory());
          
        currentWindow = new WindowDirectories();
        currentWindow->init(&lcd, &showDirMemory, &copyMemory);
        currentWindow->reset();

        
        for (int i = 0; i < 4; i++)
          createWindowDirectories();
}

It's the result:

createWindowMain freeMemory()= 2968
createWindowMain  delete currentWindow; freeMemory()= 2979
Constructor
WindowMain::init
Window::init
WindowMain::reset
WindowMain::reset END
createWindowMain  end freeMemory()= 2712
createWindowMain freeMemory()= 2720
createWindowMain  delete currentWindow; freeMemory()= 2731
Constructor
WindowMain::init
Window::init
WindowMain::reset
WindowMain::reset END
createWindowMain  end freeMemory()= 2464
createWindowMain freeMemory()= 2477
createWindowMain  delete currentWindow; freeMemory()= 2488
Constructor
WindowMain::init
Window::init
WindowMain::reset
WindowMain::reset END
createWindowMain  end freeMemory()= 2221

Perhaps I should have been clearer. Without THE WHOLE SKETCH, you are wasting our time.

I don't understand why you say that, I have a return to the beginning of the loop and the only code that is running is the setup. You can see there is a loss of memory with that code, in the first post are the class files, maybe you mean that?

You show where you delete the components of WindowDirectories, but not where you construct them.

I can't copy the code here because it exceeds the maximum number of characters (9000).
You can see it in WindowDirectories.cpp this in the first post.
The functions which have used dynamic memory are: constructor, init, copy and setFiles.

Thanks.

I can't copy the code here because it exceeds the maximum number of characters (9000).

If you use the Reply button, instead of this stupid Quick reply field, you'd see that there is a link below the text window, where you can attach your code.

You can see it in WindowDirectories.cpp this in the first post.

No you can't. You don't have ANY code in that display which allocates any memory which you would need a destructor to destroy.

You probably don't even need a destructor. You don't seem to actually know what it is.