0

//CVMI.cpp

static char* std1[] = {"a","b","c"};
static char* std2[] = {"1","2","3"};

CVMI::CVMI(HWND p)
{
  //Does nothing.
}

//CVMI.h

const int   cMaxIPAddr = 100;
class CVMI : public VMIListener
{
 public:
CVMI(HWND p); 
    VMI vmi;
bool    bOpen;
    char                sVoceraIPAddr[cMaxIPAddr + 1];
long                iMessageID; 
};

Above are the code of a class I'm using in a thread and runs a for loop. If put the following three lines within that for loop, then I notice my memory would shoot through the roof.

m_pVMI = new CVMI(m_hNotifyWnd);
delete m_pVMI;
m_pVMI = NULL;

What am I doing wrong here? I though my delete would handle the memory allocation every time already. Or do I have to specifially free up all the resource in a destructor ~CVMI()? . This is my first attempt of troubleshooting a memory leak and being a C++ beginner doesn't make it easier.

Edit:

 class VMI_API VMIListener : public Listener
 {                                                                                      

 public:

// Message acknowledgement.  iAckCode is one of AC codes.
virtual void    HandleAck(long iMessageID, char* sLoginID, int iAckCode) = 0;   
virtual void    HandleResponse(long iMessageID, char* sLoginID, char* sResponse) = 0;
virtual void    HandleConnectionFailed(void) = 0;
  };
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
Fylix
  • 2,551
  • 6
  • 45
  • 72
  • 3
    Does `VMIListener` have a virtual destructor? – mfontanini Nov 04 '13 at 21:27
  • Not that I can see, I just paste in the basic part of the VMI listener. – Fylix Nov 04 '13 at 21:30
  • 1
    @Fylix both `VMIListener` and `Listener` need [virtual destructors](http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors) – Mgetz Nov 04 '13 at 21:31
  • You haven't shown the code necessary to answer this question. What is `m_pVMI` and how is it declared? – Jonathan Wakely Nov 04 '13 at 21:49
  • BTW, how did you detect that you've got memory leak? Which IDE are you using? If you are using Visual Studio, you run it in debug mode, when program exits, do you have message in the debug window like "Detected memory leaks! Dumping objects ->......."? – milesma Nov 04 '13 at 21:52

2 Answers2

1

You need to check several places:

  1. the caller that uses "CVMI".

    As you described, there is no memory leak because you had "delete"

    m_pVMI = new CVMI(m_hNotifyWnd);
    delete m_pVMI;
    m_pVMI = NULL;
    
  2. The classes "CVMI"

    According to your code, you "new"ed and immediately "delete"ed and your constructor does nothing, then this is not the place has memory leak.

  3. The only fishy place is constructor of "VMIListener" or the base class "Listener" (if that one has base class as well, check the base class as well...), maybe it allocate to a memory variable like: m_foo = new CFoo(); in the constructor; However, if you don't have destructor defined for "VMIListener" (or Listener or super class), then memory leak is guaranteed.

milesma
  • 1,561
  • 1
  • 15
  • 37
0

std1 and std2, your static arrays, are not causing your memory leak. They aren't even used in the code above, so it's not clear where your title even comes from. As for the virtual destructors mentioned in the OP comments, it entirely depends on what Listener's implementation is since neither VMIListener nor CVMI are dynamically allocating memory.

Jim Buck
  • 20,482
  • 11
  • 57
  • 74
  • 1
    Actually they are doing dynamic allocation since they are using virtual methods (allocating the slots for the methods see the link in my comment above), the important question is how is `m_pVMI` declared, if it's declared as `CVMI` then he's safe if not... then it's undefined behavior – Mgetz Nov 04 '13 at 21:53
  • How does using virtual methods imply dynamic allocation? A class can have virtual methods without the class having members that do any dynamic allocation. The virtual table has nothing to do with a potential memory leak. – Jim Buck Nov 05 '13 at 03:50
  • 1
    It has quite a lot to do with a potential memory leak, without a virtual destructor the runtime does not know to release the vtables of the derived classes or any associated allocation they might have done. It will only release the outlay of the vtable and any associated memory of the base class. The question I linked to makes that quite clear. That is why deleting a derived class via a base class interface __without__ a virtual distructor is undefined behavior. – Mgetz Nov 05 '13 at 12:10
  • What is an "outlay of the vtable"? Given the code he posted, he isn't doing any dynamic allocation of his own since his member types are trivial with the exception of `VMI`. Depending on what that class does, then yeah, there would be some leaks if `VMI` does dynamic allocation. – Jim Buck Nov 05 '13 at 21:05
  • Take a look at http://www.gotw.ca/publications/mill18.htm and [Effective C++](http://www.amazon.com/dp/0201924889/?tag=stackoverfl08-20) they explain the details far better than I can. Let it suffice it's undefined behavior and very dangerous. Also http://stackoverflow.com/questions/408196/how-should-overriding-delete-in-c-behave – Mgetz Nov 05 '13 at 21:12