[Solved]Member variables don't seem to exist inside class
-
I'm trying a access a pointer inside one of my class' functions. This variable was initialized in the constructor (it happens to be a pointer to a widget that gets created with new), so I know it's there. However, when I try to access it from another function, I get a seg fault. When I run the debugger and look under the 'this' pointer, it says "unavailable synchronous data". Any member variable should be available in any of that class' functions. What's going on here?
-
Well, with the information you provide, probably nobody but yourself will be able to provide assistance.
Maybe you have overwritten the pointer either on purpose or by accident.
If you can provide some short example code showing the problem, you will give others the chance to help you.
-
Well, the variable isn't being overwritten. It simply no longer exists.
In the constructor, I have:
@MainWindow::MainWindow()
{
//create the central area and add it to the main window
m_CentralArea = new QMdiArea;
setCentralWidget(m_CentralArea);//create the notebook and add it to the dock area m_Notebook = new Notebook; QDockWidget *dockWidget = new QDockWidget(tr("Notebook"), this); dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea); dockWidget->setWidget(m_Notebook); addDockWidget(Qt::LeftDockWidgetArea, dockWidget); m_Notebook->show();
}@
Then, later in another function:
@void MainWindow::addNewNote()
{
NewNoteWindow *newNote = new NewNoteWindow(m_Notebook);
m_CentralArea->addSubWindow(newNote);
}@At this point, m_Notebook no longer exists and I get a seg fault.
-
One thing came to my mind when looking in more detail to the supplied code.
I guess Qt typically takes over the responsibilities of memory you supply with pointers. Possibly it is internally moved.Check if it is still in its location after you have called setWidget and addDockWidget in your constructor.
-
Here's what my header file looks like
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QtGui>#include "notebook.h"
class Notebook;
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow();
~MainWindow();void addNewNote();
private:
Ui::MainWindow *ui;
QMdiArea *m_CentralArea;
Notebook *m_Notebook;
};#endif // MAINWINDOW_H@
I can't think of any other code that would be relevant. One other interesting thing to note is that I couldn't build until I forward declared the Notebook class at the beginning of the header file. Otherwise it wasn't recognizing Notebook as a type, even though I was including notebook.h. I'm not sure if that's important, but it might be.
I ran the debugger and m_Notebook is accessible all the way through the end of the constructor, so setWidget and addDockWidget don't seem to be the culprits.
-
Hi,
can you post a full, compilable example?
I think, there is a bug in notebook.h, otherwise the type would be known.
Second, it should live, until you don't delete the dock.Did you try to use QPointer<Notebook> which will be set to 0 if the object is destroyed?
-
[quote author="mrplainswalker" date="1313163508"]One other interesting thing to note is that I couldn't build until I forward declared the Notebook class at the beginning of the header file. Otherwise it wasn't recognizing Notebook as a type, even though I was including notebook.h. I'm not sure if that's important, but it might be.
[/quote]Please post notebook.h
Which OS and compiler are you using?
-
First off, I'm using Windows 7, Qt 4.7.3 and mingw.
I think I found the problem. The problem is that my parent pointer in my notebook class was incorrect. It was pointing at something other than a mainwindow object.
However, I'm unclear about how to solve the problem. Because Qt manages pointers by itself once you hand over control to something like a QMdiArea, I can't count on the pointers always remaining the same. However, I need to know what the pointers are in order to be able to communicate between classes.
I have a main window with many subwindows that come in and out of existence. The subwindows need to be able to pass objects back and forth between each other. What is the best way to manage that? Every method seems to require the objects to have pointers to each other.
-
[quote author="mrplainswalker" date="1313166857"]However, I need to know what the pointers are in order to be able to communicate between classes.
I have a main window with many subwindows that come in and out of existence. The subwindows need to be able to pass objects back and forth between each other. What is the best way to manage that? Every method seems to require the objects to have pointers to each other.[/quote]
Sounds like it might be a problem that signals and slots could solve.
-
Well, after tinkering with it, I was able to get signals and slots to essentially do what I want. I'll mark this as solved.