I’m trying to upload a .avi file to an FTP server with QFtp:
void MyFTP::recordingDone(QString &fileName){
remainingFiles.push_back(new QString(fileName));
commands[qftp.connectToHost(globalProperties.ftpServer)] = "connect to host";
commands[qftp.login(globalProperties.ftpUsername,globalProperties.ftpPassword)] = "login";
commands[qftp.cd("myapp")] = "cd myapp";
commands[qftp.cd("Videos")] = "cd videos";
QDir().cd(globalProperties.videoStorageLocation);
qDebug()<<"Opening "<<fileName<<endl;
if(QFile::exists(fileName)){
qDebug()<<"File exists"<<endl;
}
else{
qDebug()<<"File does not exist"<<endl;
}
QFile file(fileName);
qDebug()<<"putting"<<endl;
qftp.put(&file,fileName);
qDebug()<<"Closing ftp"<<endl;
qftp.close();
}
I’m connected to the command done signal and using the slot to debug output info:
void TrusionFTP::ftpCommandDone(int id, bool error){
qDebug()<<"command: "<<commands[id]<<": "<<error;
if(error){
qDebug()<<qftp.errorString()<<endl;
if (qftp.hasPendingCommands()){
qDebug()<<"Pending commands"<<endl;
}
}
}
Here’s the output before the crash:
Opening "2012-Mar-06-12-19-57.avi"
File exists
putting
Closing ftp
command: "connect to host" : false
command: "login" : false
command: "cd trusion" : false
command: "cd videos" : false
The crash also occurs if I don’t close the ftp connection. The file never makes it onto the server.
fileresides on stack. The life time offileends as soon as the member functionvoid MyFTP::recordingDone(QString &fileName)returns. But theqftphas still reference to it. This is probably causing you the crash when you are trying to accessfilewhich no longer exists. Allocatefileon heap instead.