Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MFC VC++
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
Edited 25-Apr-12 0:47am
v5
Comments
Sandeep Mewara at 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 at 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 at 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
0 Zoltán Zörgő 300
1 /\jmot 205
2 Peter Leow 165
3 CHill60 145
4 CPallini 135
0 Sergey Alexandrovich Kryukov 9,383
1 OriginalGriff 6,881
2 Peter Leow 4,607
3 Zoltán Zörgő 4,284
4 CHill60 2,892


Advertise | Privacy | Mobile
Web03 | 2.8.150129.1 | Last Updated 25 Apr 2012
Copyright © CodeProject, 1999-2015
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