I have the the following:
void Class1::method()
{
QStringList* file_list;
collect_file_paths(file_list); //Sends pointer to the method below
}
void Class1::collect_file_paths(QStringList* file_list)
{
//Gather file paths
DirectorySearch ds;
connect(&ds, SIGNAL(updateStatus(QString)), this, SLOT(onStatusUpdate(QString)));
file_list = ds.get_file_names(_strPath); //Returns a pointer of QStringList
}
QStringList* DirectorySearch::get_file_names(QString path)
{
QStringList *file_names = new QStringList;
traverse(path, file_names);
compare_existing(file_names);
return file_names; //returning pointer address
}
What is happening is that the memory address returned from get_file_names()
is lost/deleted when I leave the scope of Class1::collect_file_paths()
. It was my understanding that QStringList *file_names = new QStringList;
in the DirectorySearch class in now on the memory heap. So it should be left on the heap until I call delete
i.e. it is never out of scope. However, as mentioned above, the address/values of the QStingList
are lost when I return to Class1::method()
from collect_file_paths(file_list)
.
Can some explain what is happening?
Thanks.
When you do file_list = ds.get_file_names(_strPath); //Returns a pointer of QStringList
you are changing the value of the file_list
(a 32/64b memory location) NOT the value of the data that memory location represents.
You need to pass a reference to your pointer (or a pointer to a pointer)
void Class1::method()
{
QStringList* file_list;
collect_file_paths(&file_list); //Sends pointer to pointer to the method below
}
void Class1::collect_file_paths(QStringList** file_list)
{
//Gather file paths
DirectorySearch ds;
connect(&ds, SIGNAL(updateStatus(QString)), this, SLOT(onStatusUpdate(QString)));
*file_list = ds.get_file_names(_strPath); //set value of pointer in caller to value returned by below function
}
...
or so
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With