Reputation: 29
My problem is simple enough but I'm not able to fix this...
In my header:
QTextStream *in = NULL;
in one method the QTextStream gets initialized:
in = new QTextStream(&file);
then I am trying to parse it in another method:
QString next;
if(in != NULL){
while(!in->atEnd()){
next = in->readLine();
}
}
else{
QMessageBox::critical(this, "Error", "No file to test!");
}
While initializing works fine, the app crashes at the test if in is atEnd(). What am I doing wrong? I need in to be accessible from several methods. I have to use a pointer here (?) because in gets initialized later (AFAIK that's not possible with references)
It might be obvious but I'm fairly new to c++...
Thank you!
Upvotes: 0
Views: 1395
Reputation: 40512
I see that you initialize the text stream with &file
. It looks like file
is a local variable and it is destroyed when initialize function is completed. QTextStream expects the IO device passed to be valid until the stream is destroyed. So you get the segfault. You need to make sure that file
is not destroyed while text stream is used.
Upvotes: 6