title: Segfault when calling virtual function of derived class
body: <p>I've been having a problem with segfaults when I call virtual function of a derived class. However, these segfaults do not occur if I change the name of the function to be different from the name of the virtual function in the base class. Here's some code:</p>
  
  <pre><code>//in main
  //initialize scene objects
  //camera
  if((camera = (Camera*)malloc(sizeof(Camera))) == NULL){
    cout &lt;&lt; "Could not allocate memory for camera" &lt;&lt; endl;
  }
  //...code in middle
  //inside file parsing...
  //infile is an ifstream
  //nextString is a char*
  if(!strcmp(nextString,"camera")){
    camera-&gt;parse(infile); //segfault here
  }
  </code></pre>
  
  <p>Here is the base class header (the .cpp only instantiates variables in the constructor):</p>
  
  <pre><code>class WorldObj{
  public:
    WorldObj();
    ~WorldObj();
    virtual void parse(ifstream&amp;) =0;
    vec3 loc; //location
  };
  </code></pre>
  
  <p>And here is the code inside my Camera class I use to write the virtual function:</p>
  
  <pre><code>void Camera::parse(ifstream &amp;infile){
    //do parsing stuff
  }
  </code></pre>
  
  <p>parse() is declared in the header file as virtual void parse(ifstream&amp;);</p>
  
  <p>My problem here is that if I rename parse() inside Camera to something like CameraParse() and completely ignore the fact that there is a virtual function to be implemented, the code works completely fine!</p>
  
  <p>Could you shed some light on why calling the virtual function causes a segfault? I've checked with Valgrind to see if there are any memory issues, and it tells me that there's an invalid read/write of 8 bytes. I understand this means that I haven't allocated memory properly for my objects, but I don't know where I'm going wrong with the allocation.</p>
  
  <p>Any help would be appreciated :)</p>