title: C++ pure virtual method memory management question
body: <p>I have a base class (<code>AClass</code> here) that has a <code>protected</code> resource (<code>str</code> here) that get's <code>free</code>'d in the <code>AClass</code> destructor. Derived <code>BClass</code> has a pure virtual <code>Init</code> method. Derived <code>CClass</code> implements <code>Init</code> which allocates some memory for the protected resource.</p>
  
  <p>Valgrind is saying that I have 3 allocs and 2 frees. Honestly, I only <strong>explicitly</strong> see 1 alloc and 1 free, but I'll accept that there are some that I don't see (for now, but please someone explain). But, why aren't they <strong>balanced</strong> at least? Does every derived instance also have it's own <code>str</code> and it's not getting <code>free</code>'d?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdarg.h&gt;
  #include &lt;stdlib.h&gt;
  
  class AClass;
  class BClass;
  class CClass;
  
  class AClass
  {
  public:
    AClass() : str(NULL) {
      printf("AClass Constructor with no params.\n");
      str = (char *) malloc(5 * sizeof(char)); 
    }
    AClass(char *foo) {
      printf("AClass Constructor with params, %s.\n", foo);
    }
    virtual ~AClass () {
      printf("AClass Destructor. Getting ready to free %s\n", str);
      free(str);
      printf("\tfree.\n");
    }
  
  protected:
    char *str;
  };
  
  class BClass : public AClass
  {
  public:
    BClass() {
      printf("BClass Constructor with no params.\n");
    };
    BClass(char *foo) : AClass(foo) {
      printf("BClass Constructor with params, %s.\n", foo);
      str = foo;
    };
    virtual void Init() = 0;
    virtual ~BClass () {
      printf("BClass Destructor.\n");
    };
  };
  
  class CClass : public BClass
  {
  public:
    CClass () {
      printf("CClass Constructor with no params.\n");
    };
    void Init() {
      printf("CClass Init method.\n");
      str = (char *) malloc(255 * sizeof(char));
      printf("\tmalloc.\n");
      snprintf(str, 255 * sizeof(char), "Hello, world.");
    };
    virtual ~CClass () {
      printf("CClass Destructor.\n");
    };
  };
  
  int main (int argc, char const *argv[])
  {
    printf("Start.\n");
    BClass *x = new CClass();
    x-&gt;Init();
    delete x;
    printf("End.\n");
    return 0;
  }
  </code></pre>
  
  <p>Here's the Valgrind output.</p>
  
  <pre><code>==6641== Memcheck, a memory error detector
  ==6641== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
  ==6641== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
  ==6641== Command: ./a.out
  ==6641==
  Start.
  AClass Constructor with no params.
  BClass Constructor with no params.
  CClass Constructor with no params.
  CClass Init method.
          malloc.
  CClass Destructor.
  BClass Destructor.
  AClass Destructor. Getting ready to free Hello, world.
          free.
  End.
  ==6641==
  ==6641== HEAP SUMMARY:
  ==6641==     in use at exit: 5 bytes in 1 blocks
  ==6641==   total heap usage: 3 allocs, 2 frees, 268 bytes allocated
  ==6641==
  ==6641== LEAK SUMMARY:
  ==6641==    definitely lost: 5 bytes in 1 blocks
  ==6641==    indirectly lost: 0 bytes in 0 blocks
  ==6641==      possibly lost: 0 bytes in 0 blocks
  ==6641==    still reachable: 0 bytes in 0 blocks
  ==6641==         suppressed: 0 bytes in 0 blocks
  ==6641== Rerun with --leak-check=full to see details of leaked memory
  ==6641==
  ==6641== For counts of detected and suppressed errors, rerun with: -v
  ==6641== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 18 from 7)
  </code></pre>