Pointer gets lost when calling one construction from another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Liam Kurmos
    New Member
    • Feb 2011
    • 8

    Pointer gets lost when calling one construction from another

    A constructor to a class takes some arguments including a pointer which is assigned to a class variable. The constructor then calls a default constructor, but inside the default constructor the pointer is no longer valid.

    some code should illustrate.

    Code:
    Feature::Feature(){
        cout<<"at b "<<oc<<endl;
        cout<<"b is "<<oc->geoSphere->getNumVs()<<endl;
        scoreData=ArrayTools::allocate2DArray<float>(oc->numSteps,oc->geoSphere->getNumVs());
        //some stuff
    }
    Feature::Feature(float weight,OVASControl* o) : weight(weight),oc(o) {
        cout<<"at a "<<oc<<endl;
        cout<<"a is "<<oc->geoSphere->getNumVs()<<endl;
        Feature();
    }
    here i've puts some prints in to demonstrate, the output is below:

    at a 0x1641bb0
    a is 642 <-this is the correct value.
    at b 0x7fadef8c0ded
    Segmentation fault <-trying to access the pointer now segfaults.

    since the flow goes from one constructor directly to the other, i dont understand how the class variable oc gets lost. Can anyone explain?

    best regards,

    Liam
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    What you are trying to do is constructor chaining, calling a constructor inside another constructor, and the current C++ standard doesn't support this. Languages like C# do. From reading about the new standard C++ 0X standard this is one feature that will be implemented.

    Secondly your code has a bug in it assuming your default constructor is declared public. If somebody calls the default constructor the pointer oc won't point to anything and will probably crash when you try to dereference it.

    Comment

    • Liam Kurmos
      New Member
      • Feb 2011
      • 8

      #3
      Many thanks for the clear answer. I had no idea constructor wasn't allowed.

      The presence of the default construct was originally only because i kept getting compile errors demanding one even though i wasn't explicitly calling one. I'll try again to get rid of it.

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        I tried it with printf("this %d\n", this); inserted in both constructors and it shows that your constructor call creates new, another Feature object and then discards it.

        Comment

        • Liam Kurmos
          New Member
          • Feb 2011
          • 8

          #5
          @newb16, you mean that if you use chained constructors like this it can cause a memory leak?

          Comment

          • newb16
            Contributor
            • Jul 2008
            • 687

            #6
            No memory leak, but the Feature(); call doesn't call the Feature() constructor of 'this' object but creates a new Feature object that is destroyed when it leaves the scope.

            Comment

            Working...