ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.