LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   unexplained c++ pointer behaviour (https://www.linuxquestions.org/questions/programming-9/unexplained-c-pointer-behaviour-242305/)

vmp 10-13-2004 04:37 PM

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
{
    private:
        int m_someint;
        double m_somedouble;
      .....

    protected:
      .....
    public:
      .....
};

By just adding the line in "int * m_pi;" the programm compiles nicely but causes a segmentation fault
Code:

class OwnThreadedWindow : public Gtk::Window, public Thread
{
    private:
        int m_someint;
        double m_somedouble;
        .....
        int * m_pi;
    protected:
      .....
    public:
      .....
};

The program crashes inside Gtk::Main::run() which is called by the Thread::run virtual method of this class. It is the starting point of the thread.

Code:

void OwnThreadedWindow::run ()
{
      gdk_threads_enter ();
      Gtk::Main::run(*this);
      gdk_threads_leave ();
}

I know I am doing something risky here by inheriting from a gtkmm window kai a commonc++ Thread, but if anyone has an explanation to offer, I would realy appriciate it.

Thanks

ta0kira 10-14-2004 02:40 AM

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

Marius2 10-14-2004 06:21 AM

Quote:

Originally posted by ta0kira
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)
[...]
ta0kira

Can't be done, since member variable declarations are only virtual.
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]?

ta0kira 10-14-2004 08:12 AM

So noted. Sorry, it was just a quick glance and I thought I was looking at a function.
ta0kira

Marius2 10-15-2004 01:14 AM

Quote:

Originally posted by ta0kira
So noted. Sorry, it was just a quick glance and I thought I was looking at a function.
ta0kira

I was confused at first, too :-) But now I'm really wondering what has become of
this problem, I really would like to know if he got it solved.

ta0kira 10-15-2004 02:04 AM

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.