LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-13-2004, 04:37 PM   #1
vmp
LQ Newbie
 
Registered: Jul 2003
Posts: 5

Rep: Reputation: 0
Unhappy 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
 
Old 10-14-2004, 02:40 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 10-14-2004, 06:21 AM   #3
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276

Rep: Reputation: 31
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]?
 
Old 10-14-2004, 08:12 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
So noted. Sorry, it was just a quick glance and I thought I was looking at a function.
ta0kira
 
Old 10-15-2004, 01:14 AM   #5
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276

Rep: Reputation: 31
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.
 
Old 10-15-2004, 02:04 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 10-15-2004 at 02:06 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
Unexplained out of memory Issue hiddenbrain Linux - Hardware 4 05-19-2005 05:02 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
unexplained traffic jarod Linux - Security 3 08-11-2003 10:31 AM
unexplained Mandrake 8.2 traffic mr.moto Linux - Networking 6 08-27-2002 01:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration