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;
  };
5
  • 3
    Does VMIListener have a virtual destructor?
    – mfontanini
    Commented Nov 4, 2013 at 21:27
  • Not that I can see, I just paste in the basic part of the VMI listener.
    – Fylix
    Commented Nov 4, 2013 at 21:30
  • 1
    @Fylix both VMIListener and Listener need virtual destructors
    – Mgetz
    Commented Nov 4, 2013 at 21:31
  • You haven't shown the code necessary to answer this question. What is m_pVMI and how is it declared? Commented Nov 4, 2013 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
    Commented Nov 4, 2013 at 21:52

2 Answers 2

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.

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.

5
  • 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
    Commented Nov 4, 2013 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
    Commented Nov 5, 2013 at 3: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
    Commented Nov 5, 2013 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
    Commented Nov 5, 2013 at 21:05
  • Take a look at gotw.ca/publications/mill18.htm and Effective C++ they explain the details far better than I can. Let it suffice it's undefined behavior and very dangerous. Also stackoverflow.com/questions/408196/…
    – Mgetz
    Commented Nov 5, 2013 at 21:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.