Hello everyone
My code compiles and runs but produces an memory leak exception when closing. This is being caused by the line
FileChooser* fileChooser = new FileChooser (“Save presets to file…”, File::getCurrentWorkingDirectory(), “*.settings”);
I know that using std:unique_pointer will work but can’t find any good examples of how. Does anyone know to modify the above code to use std::unique_pointer and any resources that explain this or give an example of this?
Typing on a phone here, so I might get this wrong. But basically you need to declare the unique pointer like this (probably in the private section of your class):
std::unique_ptr <FileChooser> fileChooser;
and then initialize it in the constructor or some other function:
It would appear you are trying to create an instance of FileChooser locally within a function. You don’t necessarily need to create it on the heap at all, assuming you do everything you need with the chooser within that function.
void MyClass::chooseFile()
{
FileChooser chooser("Choose file",File(),"*.txt");
if (chooser.browseForFileToOpen(),"*.myfile")
{
// use chooser result
}
}
You are right LiamG! This is how I will do it from now on. Doing it all in the private section outside of the constructor leads to all sorts of issues. This method right here works great. You have saved me hours!