unexplained c++ pointer behaviour
I am having a realy strange problem with a cpp class I am creating:
A mere declaration of any kind of pointer member variable causes segmentation fault. I don't even use the pointer. I have a working program with a typical class like the following: Code:
class OwnThreadedWindow : public Gtk::Window, public Thread Code:
class OwnThreadedWindow : public Gtk::Window, public Thread Code:
void OwnThreadedWindow::run () Thanks |
Try initializing it to NULL when you declare it:
int * m_pi(NULL); //this int * m_pi = NULL; //or this int * m_pi = int*(); //or this (may be non-standard) That is good practice regardless because a pointer is POD and therefore is not initialized; it starts with whatever value was in that mem location at the time. ta0kira |
Quote:
Instead, the variable should be initialized in OwnThreadedWindow::OwnThreadedWindow(), but that may be past the segfault. @vmp: I'm wondering void OwnThreadedWindow::run () { gdk_threads_enter (); Gtk::Main::run(*this); gdk_threads_leave (); } shouldn't it be Gtk::Main::run(this); //what exactly is *this? The adress you //find at DWORD PTR[this]? |
So noted. Sorry, it was just a quick glance and I thought I was looking at a function.
ta0kira |
Quote:
this problem, I really would like to know if he got it solved. |
So m_pi isn't dereferenced at any point? The only thing I can think of is a bad pointer to OwnThreadedWindow followed by a dereference to m_pi from that. Sometimes you can get away with accessing an int or double member of a bad pointer, but accessing a pointer member uusually gives you a problem. Is there any place you reinterpret_cast to OwnThreadedWindow? Basically this error does not seem to be caused by adding the pointer; it just goes unnoticed until you add it. You should look elsewhere in your code for C casts, reinterpret_casts, uninitialized pointers, and other weird stuff.
Something else you can do is std::cout << 1, 2, 3 etc. in different places so you can see where exactly the fault stops execution. ta0kira |
All times are GMT -5. The time now is 04:18 AM. |