Tags: againvoid, array, cmaptestdlgremovepoints, code, cpoint, cptrarray, empty, int, itbefore, m_points, microsoft, msdn, ppoint, software, type, visual

m_Points is of type CPtrArray, I have this code to...

On Microsoft » Microsoft Visual C & C++

6,592 words with 10 Comments; publish: Wed, 06 Feb 2008 17:36:00 GMT; (32578.13, « »)

m_Points is of type CPtrArray, I have this code to empty it

before using the array again:

void CMapTestDlg::RemovePoints()

{

CPoint* pPoint;

for(int x=0; x < m_Points.GetSize(); x++)

{

if(pPoint != NULL)

{

pPoint = (CPoint*)m_Points.GetAt(x);

delete pPoint;

m_Points.SetAt(x, NULL);

}

}

m_Points.RemoveAll();

}

But it crashes every time it goes into this function.

I would go into detail about the code it points to,

but it hangs up my system every time I say

RETRY then DEBUG, so I'll tell you what the dialog

says and choose ABORT.

DEBUG ASSERTION FAILED!

File: dbgheap.c

Line: 1017

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

Thanks for any help...

*DM*

AIM/AOL: EdisonCPP

ICQ: 39184886 - ID EdisonCPP

Email: steven.visual-c.itags.org.noblecodes.com

All Comments

Leave a comment...

  • 10 Comments
    • Without knowing how CPtrArray is defined, I see only one possibility. The following might fix it:void CMapTestDlg::RemovePoints()

      {

      CPoint* pPoint;

      for(int x=0; x < m_Points.GetSize(); x++)

      {

      pPoint = (CPoint*)m_Points.GetAt(x);

      if(pPoint != NULL)

      delete pPoint;

      m_Points.SetAt(x, NULL);

      }

      m_Points.RemoveAll();

      }

      ****************************************************************************************************

      Ratings are unimportant but feedback is. Let the helper and the worldwide community know what works, perhaps with ratings.

      #1; Sat, 10 Nov 2007 05:57:00 GMT
    • pPoint is NULL after you declare it !

      Check for null pointers AFTER you get them out of the array !for (....)

      {

      pPoint=(CPoint*) ...

      if (pPoint!=NULL) delete pPoint;

      ...

      }

      Sven

      (-: Please rate my post :-)

      #2; Sat, 10 Nov 2007 05:58:00 GMT
    • That was a silly mistake, good catch, but it's still doing it.

      Simple declaration in header file, to reply to your first statement:

      CPtrArray m_Points;

      *DM*

      AIM/AOL: EdisonCPP

      ICQ: 39184886 - ID EdisonCPP

      Email: steven.visual-c.itags.org.noblecodes.com

      #3; Sat, 10 Nov 2007 05:59:00 GMT
    • Hi,

      beside the fact that you are using an uninitalized CPoint* point variable when entering the loop nothing seems to be wrong to me.

      Try to find out if every CPoint* you are deleting is allocated correctly .

      An alternative way to empty your array would be like this:

      void CMapTestDlg::RemovePoints()

      {

      for(int x=0; x < m_Points.GetSize(); x++)

      {

      CPoint* pPoint = (CPoint*)m_Points.GetAt(x);

      //the check for null is unnecessary, as

      //in C++, it is safely allowed to delete a nullpointer

      delete pPoint;

      }

      m_Points.RemoveAll();

      }

      clem

      #4; Sat, 10 Nov 2007 06:00:00 GMT
    • Just to test it, I commented out the delete statement, and it didn't crash.

      I was stepping through debugging... TONS of files involved watching

      each step for EACH of the steps, and I decided to come back and make sure

      I wasn't trying to delete more than there were, but I wasn't. But it IS

      in the "delete pPoint;" statement somehow.

      *DM*

      AIM/AOL: EdisonCPP

      ICQ: 39184886 - ID EdisonCPP

      Email: steven.visual-c.itags.org.noblecodes.com

      #5; Sat, 10 Nov 2007 06:01:00 GMT
    • As so many times I've seen with posters...

      It was in another place. I'm still not

      exactly sure why though.

      I was adding pointers in this fasion:

      CPoint* pNew = new CPoint;

      //some things done with pNew here...

      m_Points.Add(pNew);

      When I changed it to this it worked:

      m_Points.Add(new CPoint);

      CPoint* pNew = (CPoints*)m_Points.GetAt(GetSize()-1);

      //some things done with pNew here...

      Thanks for your help...

      *DM*

      AIM/AOL: EdisonCPP

      ICQ: 39184886 - ID EdisonCPP

      Email: steven.visual-c.itags.org.noblecodes.com

      #6; Sat, 10 Nov 2007 06:02:00 GMT
    • I'm not convinced too that the change you indicated was the reason for your crash, as I did the CPoint allocation in the same way you did in your "crashing" version, without any problem.

      clem

      #7; Sat, 10 Nov 2007 06:03:00 GMT
    • I have no idea either, but it IS all I changed.

      I would have worried that it was only a local

      variable that was losing scope, but the 'new' CPoing

      without being 'delete'ed should have maintained

      it beyond scope. But, that's why I opted to try

      the other way, just to make sure. Maybe it's some

      quirk buried I don't understand, but nothing else

      changed but the structure of how I added them, and

      in the way I described it to you above.

      I wish I knew. With everyone pouring over the

      Remove() code, and saying that it looked okay,

      I focused elsewhere, and that's really the only

      other place I could look, if not where removing them,

      then where adding them. The only manipulation

      was at Add-time, besides that, I cycled through

      them in my OnPaint() using GetAt(), and that code

      hasn't changed. It beats me, but the code is working

      now crash and leak free.

      Thanks for your help...

      *DM*

      AIM/AOL: EdisonCPP

      ICQ: 39184886 - ID EdisonCPP

      Email: steven.visual-c.itags.org.noblecodes.com

      #8; Sat, 10 Nov 2007 06:04:00 GMT
    • CPtrArray is a little dangerous and annoying since it will allow you to put a pointer to anything in there... and then when you cast it back to a CPoint you don't know if it really is a CPoint. Try using this instead...

      CTypedPtrArray<CPtrArray, CPoint*> m_Points;

      now SetAt and Add will expect a CPoint* and GetAt will return a CPoint* so there is no need to do any casting...

      CPoint* pPoint = m_Points.GetAt(x);

      instead of

      CPoint* pPoint = (CPoint*)m_Points.GetAt(x);

      #9; Sat, 10 Nov 2007 06:05:00 GMT
    • If I understand what you said, you were:

      (1) allocating an object

      (2) doing some things to it

      (3) storing the pointer

      then you modified the program so it did the following and this worked:

      (1) allocate an object and store the pointer

      (2) do some things to it

      Is that correct? If so, then there is a good chance that you were (and are?) doing something to the pointer (in the part that does things to it) that you are overlooking. For example, is it possible you are using "=" in a logical expresssion instead of "=="?

      I am sure that all of this is not new to you but just in case there is something you are overlooking it might be worthwhile to try to find it. Right?

      ****************************************************************************************************

      Ratings are unimportant but feedback is. Let the helper and the worldwide community know what works, perhaps with ratings.

      #10; Sat, 10 Nov 2007 06:06:00 GMT