I am having trouble using pointers in my program. In my header file I have got the following private variable pointer assignment:
private:
QString *currentFile;
In my program I have got a function that starts by copying the value of the currentFile pointer to another QString variable:
QString fileName = *currentFile;
However this immediately gives me segmentation fault when debugging. I have absolutely no idea what I am doing wrong.
The program runs just fine until I call the function that tries to get the vlue of the pointer. I figured it might be becuase the pointer was empty so I tried adding the following code to my constructer:
*currentFile = QString::null;
To assign a null
value to the pointer value, however this just gave me the segmentation fault as soon as the constructer was called.
EDIT
more code:
notepad.h:
class Notepad : public QMainWindow
{
Q_OBJECT
public:
Notepad();
private slots:
void open();
void save();
void saveAs();
void quit();
private:
QTextEdit *textEdit;
QString *currentFile;
QString *currentContents;
};
the function creating the error (void save()) in notepad.cpp:
void Notepad::save(){
QString fileName = *currentFile;
if(fileName != "")
{
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly))
{
QMessageBox::critical(this, tr("Error"), tr("Could not write to file"));
return;
}
else
{
QTextStream stream(&file);
QString editorContent = textEdit->toPlainText();
currentContents = &editorContent;
stream << editorContent;
stream.flush();
file.close();
}
}
else
saveAs();
}
This is most probably due to the fact, that you never allocated any storage for the string. The pointer just stores a memory address and if its value is initialized to 0 (or even worse to nothing and contains a completely undefined address), then it doesn't point to a valid string object and trying to use the memory it points to as a string object results in undefined behaviour (in your case a segfault).
So you first need to allocate memory for a string and construct the string using (probably in the surrounding object's constructor):
currentFile = new QString;
and later (when not needed anymore, e.g. in the surrounding object's destructor):
delete currentFile;
But as said in the comments, I really doubt that you need a pointer member. Why not just use a QString
object as member. Or if you really need pointers, rather use some smart pointer (like auto_ptr
or the new C++11 unique_ptr
or shared_ptr
).
With QObject
derived types (widgets and the like) it's a different story and you rather should use pointers for them (and Qt handles deallocation for you, if used properly). But QString
is (like strings usually are) a rather value-like type (similar to the builtin types) and in most cases doesn't have to be allocated dynamically.