6

I have the following problem: In myClass I want to default initialize a pointer to yourClass, with a new yourClass adress. Unfortunately, if I want to delete the pointer at any point I get a (core dump).

class myClass
{
      protected:
      yourClass * yc;

      public:
      myClass() { yc = new yourClass(); }

      myClass(yourClass * tyc ) { delete yc; yc = tyc; }

      ~myClass() { delete yc; yc = NULL; }

      void setMyClass (yourClass * tyc) { delete yc; yc = tyc; }

      void print () { yc->print(); }
};

int main()
{
  yourClass b (//parameter);
  myClass * a = new myClass();
  a->print();
  a->setMyClass(&b)
  a->print();

  delete a;
  return 0;
}

The print() of a, should result in two different prints, dependent on //parameters.

I considered yourClass yc; instead of a yourClass* yc, but I want to know if it is possible.

EDIT: I reworked the code in the following way and it works. Still looks complicated, smart pointers seem promising and I still did not apply the "Rule of Three". Here the code. Thanks all.

class myClass
{
      protected:
      yourClass * yc;
      bool dynamic;

      public:
        myClass() { dynamic = true; yc = new yourClass (); }
        myClass (yourClass * tyc ) 
        { 
          // dynamic init (like default)
          if (tyc == NULL ) { dynamic = true; yc = new yourClass (); }
          // static use of yc
          else { dynamic = false; yc = tyc; } 
        }
        // because only if dynamic is true, we need to erase
        ~blu () { if (dynamic) { delete yc; dynamic = false; } } 

        void setMyClass(yourClass* tyc) 
        { 
          // leaving unchanged if new-stuff is NULL or like old-stuff
          if ( tyc == yc || tyc == NULL ) return;
          else // treating dynamic and static differently
          { 
            if (dynamic) // if flag is set, must be deleted 
            {
              delete yc; yc = tyc; dynamic = false;
            }
            else // must not be deleted, dynamic is still false
            {
              yc = tyc;
            }
          }
        }
        void print () { yc->print(); }
};
Alex
  • 175
  • 1
  • 3
  • 9

2 Answers2

8

That's because you are trying to delete too much:

  • you are deleting a non-allocated object in the second constructor (remove delete yc;)
  • you are trying to delete a stack-allocated object, b. delete a; will try to delete a pointer to b, which is an object on the stack; what happens depend on your OS (I expect an exception/core dump/whatever)

EDIT: another problem I spotted.. a->setMyClass(NULL)

I would suggest:

  • this post on smart pointers
  • this blog post on RAII
  • any C/C++ primer explaining stack vs. heap allocation (static vs. dynamic?)
Community
  • 1
  • 1
Lorenzo Dematté
  • 7,638
  • 3
  • 37
  • 77
  • A work around for the second issue may be to store a flag for 'ownsYc' which you could set as false when setMyClass () is called and only delete yc in the destructor if the flag is true – Crog Feb 01 '13 at 13:08
  • @Crog ...ummm... yes, but still it is a very, very dangerous design.. In C++, I believe it is always better to have RIIA; besides, there are smart pointers (http://www.stroustrup.com/) – Lorenzo Dematté Feb 01 '13 at 13:13
1

You're violating the rule of three.

What is The Rule of Three?

Also this is recipe for disaster:

 myClass(yourClass * tyc ) { delete yc; yc = tyc; }

What happens if tyc==yc? Right. Not pretty :)

 myClass(yourClass * tyc ) { if (yc!=tyc) { delete yc; yc = tyc; } }
Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • it is more disaster than that :) in the constructor, yc will always be not initialized: if you are lucky, it's delete NULL (segfault); otherwise it's delete some-random-thing – Lorenzo Dematté Feb 01 '13 at 13:08
  • So the code you are proposing will crash anyway (assume the compiler initialize yc to 0...) – Lorenzo Dematté Feb 01 '13 at 13:14
  • 1
    @dema80 I agree, I didn't have the time to actually read that code. Shame on me. I just thought I'd highlight this classic pitfall, since it is slightly more subtle (even Scott Meyer once published a smartptr with this type of bug, in a book) – sehe Feb 01 '13 at 13:26