title: Segmentation fault when calling class from matlab
body: <p>I am getting a segmentation fault with the following code, and I really dont know what is happening here. It seems that it has something to do with the pointer in the Master-class, but I am not sure how to solve this. I have the following code:</p>
  
  <pre><code>class Shape
  {
  public:
      Shape(){}
      ~Shape(){}
  
      virtual void draw() = 0;
  };
  
  class Circle : public Shape
  {
  public:
      Circle(){}
      ~Circle(){}
  
      void draw()
      {
          printf("circle");
          // code for drawing circle
      }
  };
  
  class Line : public Shape
  {
  public:
      Line() {}
      ~Line() {}
  
      void draw()
      {
          printf("line");
          // code for drawing line
      }
  };
  
  class Master
  {
  public:
      Shape* member_shape;
  
  public:
      Master()
      {}
      ~Master()
      {}
  
      void add_shape_circle()
      {
          member_shape = new Circle();
      }
  
      void add_shape_line()
      {
          member_shape = new Line();
      }
  };
  
  Master* master_object;
  </code></pre>
  
  <p>Do you have any clue how to get this code working? Thanks.</p>
  
  <p>EDIT (added main function):</p>
  
  <p>Actually there exists no main-function like the following in my code, because I am using the code in a MATLAB c-mex function. But it should look like this:</p>
  
  <pre><code>//... classes from above here
  int main()
  {
      master_object = new Master();
      master_object-&gt;add_shape_circle();
      master_object-&gt;member_shape-&gt;draw(); // segmentation error here
  
      return 0;
  }
  </code></pre>
  
  <ol start="2">
  <li>EDIT</li>
  </ol>
  
  <p>The error does not occur if I instatiate the <code>Circle</code> object dircetly in the <code>Master</code>-constructor. But then there is no way to choose between <code>Line</code> and <code>Circle</code>. Example: If I change my <code>Master</code> class to the following, then the function call <code>master_object-&gt;member_shape-&gt;draw()</code> does not lead to an error.</p>
  
  <pre><code>class Master
  {
  public:
      Shape* member_shape;
  
  public:
      Master()
      {
          member_shape = new Circle();
      }
      ~Master()
      {}
  
      void add_shape_circle(){}
  
      void add_shape_line(){}
  };
  </code></pre>
  
  <p>So, there is something up with this uninitialized pointer...I think.</p>