Click here to Skip to main content
12,630,410 members (28,769 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: MFC VisualC++
I have an mfc vc++ class which is inherited from CView

#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

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

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

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

class NormalClass
{
    private:
 
       funt1 ();
       int a;
 
    public:
 
        NormalClass();
        ~NormalClass();
 
}

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

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 25 Apr 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100