topical media & game development

talk show tell print

professional-program-13-Leaky-Leaky.c

? / professional-program-13-Leaky-Leaky.c


  include <iostream>
  
  using namespace std;
  
  class Simple 
  {
  public:
    Simple() { mIntPtr = new int(); }
    ~Simple() { delete mIntPtr; }
  
    void setIntPtr(int inInt) { *mIntPtr = inInt; }
  
  protected:
    int* mIntPtr;
  };
  
  void doSomething(Simple*& outSimplePtr)
  {
    outSimplePtr = new Simple(); // BUG! Doesn.t delete the original
  }
  
  int main(int argc, char** argv)
  {
    Simple* simplePtr = new Simple(); // allocate a Simple object
  
    doSomething(simplePtr);
  
    delete simplePtr; // only cleans up the second object
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.