Hi Guys I am trying to create a global object instance and am currently running into a segmentation fault while accessing a member variable.

I have a capture.h file and a capture.cpp file

//In capture.h

struct ringinfo 

{

// a simple struct

};

class capture_manager{public: 

                                   func1();

                                   func2();

                                    ringinfo ringinf ;

                                      };

extern capture_manager* capture_man ;


//in capture.cpp 

{ 

 capture_manager* capture_man = NULL ;

// all function definitions including a default constructor that initializes the ringinf = NULL ; 

}

I include the capture.h file in a different .h/.cpp file and I can use the capture_man object and all of its functions, but whenever I try to access ringinf I get a segmentation fault

Anything obvious that I am doing wrong. ??

Thanks for the help
Akshay

I can use the capture_man object and all of its functions

Very often, just calling a member function of an object doesn't actually require the object to be valid or even existing at all, it is only when you access the data members (either within the member function, or from the outside) that you'll get a seg-fault if the object is not valid (e.g., the pointer is NULL). Technically, you can do this without an error:

struct Greeting {
  void say_hello() const {
    std::cout << "Hello!" << std::endl;
  };  // notice how there is no access of a data member (via the 'this' pointer).
};

int main() {
  Greeting* p = NULL;
  p->say_hello();
  return 0;
};

So, being able to call member functions successfully isn't enough information to say that the object actually exists, because a seg-fault would only occur when you try to access a data member.

but whenever I try to access ringinf I get a segmentation fault

Case in point.

Anything obvious that I am doing wrong. ??

Well, the devil is in the details, there aren't enough to know for sure. Where do you actually create the capture_man object (the pointee)? From where and when do you try to access it? Could you have fallen prey to the static initialization fiasco? This would be my guess.

The kind of thing you are writing, where you have a class with a unique instance of that class accessible globally, is called a "singleton". The correct way to implement a singleton in C++ (avoiding the static initialization fiasco, and guaranteeing a single, global instance) is as follows:

// in my_singleton.h:

class MySingleton {
  public:
    void some_function1();
    void some_function2();
    //...
    // then a static instance-access function:
    static MySingleton& instance();
  private:
    // then a private constructor:
    MySingleton();
};


// in my_singleton.cpp:

void MySingleton::some_function1() {
  //..
};

// ...

MySingleton& MySingleton::instance() {
  static MySingleton inst;  // use a static local variable for the instance.
  return inst;  // return it by reference.
};

MySingleton::MySingleton() : /* ... */ { /* ... */ };

Now, with that, outside code is prohibited from creating an instance of MySingleton (due to private constructor), and can access the single global instance of the class through the static instance() function.

I just start using the access to member functions in a different file. How do I create the pointee (capture_man) :??
In a different file

In liber.h
{
#include capture.h

liber_func1() ;
liber_func2() ;

}


in liber.cpp
{#include liber.h

liber_func1(){

 capture_man->func1() ;
 //other stuff
 }


 liber_func2(){
  //other stuff
    capture_man->func2() ;
    }

}   

I thought initializing it to NULL was the creation of the instance capture_man !!
How do I make capture_man a valid instance ??

And thanks for the singleton advice, but I would like to try and fix this and then try a singleton implementation :)

I thought initializing it to NULL was the creation of the instance capture_man !!

No, on the contrary, setting the pointer to NULL is equivalent to saying "this pointer points no-where, to nothing at all". Using a NULL pointer (i.e., dereferencing it) is exactly what causes a segmentation fault. That's why I thought you didn't post the actual code where the pointer was initialized, but now I know that the problem is that you don't know how to initialize it, period.

Just a couple of things before:

First of all, if you are going to post code, please post your actual code (or an exerpt from it), do not post weird pseudo-code things like you did, especially when the problem is technical and not theoretical. I assumed that the initial code that you posted was not real code (because it is riddled with problems that would never compile, let alone run up to a segfault). And so, I assume the latest code you posted is also not your real code (again, because it is not even remotely compilable). How can you expect us to help you resolve a technical issue with the code if you only provide very weird looking code fragments of non-C++ code?

Second, for everyone's sake, please indent your code properly. Some of us take quite a bit of time writing quality replies (without spelling mistakes, with compilable examples, and proper indentation), common courtesy dictates you ought to make a similar effort.

How do I make capture_man a valid instance ??

The typical way is with new and delete. The new operator allocates memory for an object and calls its constructor (to initialize object data members). The delete operator does the reverse (calls the destructor and deallocates the memory). new will output a pointer to the newly created object, which you can store. Anything that you allocate with new, you have to eventually deallocate with delete. That's it, in a nutshell. Here is an example:

#include <iostream>

// Say, I have some custom class:
class Foo {
  public:
    // default constructor:
    Foo() {
      std::cout << "Constructor called!" << std::endl;
    };
    // destructor:
    ~Foo() {
      std::cout << "Destructor called!" << std::endl;
    };
    // some function:
    void Bar() {
      std::cout << "Hello!" << std::endl;
    };
};

int main() {
  // now I can allocate an object of Foo class with new:
  Foo* p_obj = new Foo();
  // then, I can call the Bar function on it:
  p_obj->Bar();
  // and finally, delete it:
  delete p_obj;
  return 0;
};

The above code should output:

Constructor called!
Hello!
Destructor called!

Before you have allocated a new object, anything that you try to do with the pointer is going to be either a crash (segfault) or a corruption of your program. The same goes after you have deleted the object.

Quoted Text Here
please post your actual code (or an exerpt from it), do not post weird pseudo-code things like you did, especially when the problem is technical and not theoretical. I assumed that the initial code that you posted was not real code (because it is riddled with problems that would never compile, let alone run up to a segfault)
please post your actual code (or an exerpt from it), do not post weird pseudo-code things like you did, especially when the problem is technical and not theoretical. I assumed that the initial code that you posted was not real code (because it is riddled with problems that would never compile, let alone run up to a segfault)

The reason I dont post real code is most of the times oversmart guys just rewrite the code for you, which sucks cause you don't get to learn. Also many times, instead of answering the question some people start showing off
alternate implementation's of the code without explaining the current issues. I am only interested in correcting my concepts not code .

Another issue is the real code is about 10000 lines in each file and highly confidential. Which is why I wrote the best possible representation of the problem conceptually.

Anyways I am glad you could help me find the problem. Thanks for the help !

This question has already been answered. Start a new discussion instead.