Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have an mfc vc++ class which is inherited from CView

C++
#include "NormalClass.h"

class FView : public CView
{
 DECLARE_DYNCREATE (FView)

public:
 
  FView ();
  ~FView ();

   NormalClass *m_pcNormal; 
}


In Constructor of class I am allocating memory to normal cpp class instance

C++
FView::FView ()
{
  m_pcNormal = new NormalClass[20];
}


In Destructor of class i am deleting the memory of cpp class

C++
FView::~FView ()
{
  if (m_pcNormal)
  {
    for (int i = 0; i < 20; i++)
    {
       delete m_pcNormal[i]; // problem is here 
                             // it is showing expression must have pointer or handle
       // it is crashing when i am doing like this delete &m_pcNormal[i]; 
    }
    delete [] m_pcNormal; m_pcNormal = NULL;
  }
}

And my normal cpp class is like this

C++
class NormalClass
{
    private:

       funt1 ();
       int a;

    public:

        NormalClass();
        ~NormalClass();

}


Any solution for this problem
Posted
Updated 24-Apr-12 23:47pm
v5
Comments
Sandeep Mewara 25-Apr-12 2:57am    
Always wrap your code in PRE tag. It makes your question readable.

1 solution

You are calling delete for objects that has not been allocated using new. You may change m_pcNormal to NormalClass** and call new for each element of m_pcNormal[] in the constructor (or later; than initialize the elements to NULL), or use an array class for storage.
 
Share this answer
 
Comments
anushagodavarthi 25-Apr-12 5:45am    
It is crashing while allocating memory for double pointer

for (int i = 0; i < 20; i++)
{
m_pcNormal[i] = new NormalClass; // crashing here for index 0
memset (m_pcNormal[i], 0, sizeof (NormalClass));
}
Jochen Arndt 25-Apr-12 5:56am    
m_pcNormal[i] is of type NormalClass while new returns a pointer to NormalClass. To use the code of your comment, m_pcNormal must be NormalClass** or NormalClass[]* (array of pointers):
/*NormalClass** */ m_pcNormal = new NormalClass*[20];

Additionally, you should not use memset here for classes. Use the constructor of NormalClass to initialize the member variables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900