是这样的。我为一个图形系统定义了一个封闭多边形类CPoly,其基类是CDraw,
在其构造函数里,初始化它的二个数据成员:UINT m_uVertexNumber//封闭多边形的顶点数目;CPoint* m_pPointList//存储顶点坐标的数组指针;在其析构函数里,删除在堆中动态分配的内存。源代码清单如下所示:
CPoly(UINT uID,UINT uVertexNumber,CPoint* pPointList):
     CDraw(uID)
{
  m_uVertexNumber=uVertexNumber;
  m_pPointList=new CPoint[uVertexNumber+1];
  if(uVertexNumber>0)
  {
for(UINT i=0;i<uVertexNumber+1;i++)
{
  m_pPointList[i]=pPointList[i];
}
  }
}
virtual ~CPoly()
{
if(m_uVertexNumber>0)
{
delete []m_pPointList;
}
}
可是,我在Debug模式下调试运行,调试器提示检测出内存泄露(detect memory leak!),如果把这部分注释掉了,就不存在此问题了。换句话说,确实是因为上述的原因产生了内存泄露问题,希望各位热心的大侠指点一二,分析错误原因,提出解决方案,承蒙帮助。

解决方案 »

  1.   

    不会吧,连CSDN都这么冷清!还是五一假期高手们不顾可能被感染非典的危险,全都出去旅游了!
      

  2.   

    1. 不知道你的uVertexNumber代表的什么意思,如果是顶点数目的话,为什么下面要多加个1(
    m_pPointList=new CPoint[uVertexNumber+1];
    i<uVertexNumber+1;
    )?
    2. CPoly的析构函数到底有没有得到调用执行?
      

  3.   

    3. 是“内存泄漏”而非“内存泄露(detect memory leak!)”。
      

  4.   

    1. UINT m_uVertexNumber//封闭多边形的顶点数目,多加1的原因:连接首末点,组成一个封闭的多边形(不是开放的多边形!)。比如:我想定义一个封闭单位四边形:m_uVertexNumber等于4,而m_pPointList指向一个长度为5(而不是4)的顶点坐标数组:CPoint lpPoints[5]={CPoint(0,0),CPoint(1,0),CPoint(1,1),CPoint(0,1),CPoint(0,0)};
    2. 我是通过类的嵌套把CPoly定义在另一类CMyDoc中的,嵌套关系结构如下:
    CMyDoc
    {
    //some member
    public:
        class CPoly:public CDraw
        {
         CPoly(UINT uID,UINT uVertexNumber,CPoint* pPointList):
             CDraw(uID)
        {
           m_uVertexNumber=uVertexNumber;
           m_pPointList=new CPoint[uVertexNumber+1];
           if(uVertexNumber>0)
           {
       for(UINT i=0;i<uVertexNumber+1;i++)
       {
            m_pPointList[i]=pPointList[i];
       }
           }
         }
         virtual ~CPoly()
         {
    if(m_uVertexNumber>0)
    {
    delete []m_pPointList;
    }
         }
         protected:
    UINT m_uVertexNumber;
    CPoint* m_pPointList;
         }m_Poly;
    //other memer
    };
    你说析构函数有没有执行啊?
    3.调试器提示信息:   
       First-chance exception in ICAD.exe (KERNEL32.DLL): 0xC0000005: Access Violation.
    Detected memory leaks!
    Dumping objects ->
    strcore.cpp(118) : {289} normal block at 0x003461B8, 31 bytes long.
     Data: <                > 01 00 00 00 12 00 00 00 12 00 00 00 C8 E7 D0 E8 
    strcore.cpp(118) : {262} normal block at 0x00346108, 18 bytes long.
     Data: <            ICAD> 01 00 00 00 05 00 00 00 05 00 00 00 49 43 41 44 
    D:\PROGRAMMING\VC++\&Iacute;&frac14;&ETH;&Icirc;&Iacute;&frac14;&Iuml;&ntilde;&acute;&brvbar;&Agrave;í\ICAD\ICADDoc.cpp(18) : {251} client block at 0x00346230, subtype 0, 260 bytes long.
    a CICADDoc object at $00346230, 260 bytes long
    strcore.cpp(118) : {242} normal block at 0x00345EC0, 31 bytes long.
     Data: <                > 01 00 00 00 08 00 00 00 12 00 00 00 D7 EE BD FC 
    strcore.cpp(118) : {127} normal block at 0x00344AE8, 18 bytes long.
     Data: <            ICAD> 01 00 00 00 05 00 00 00 05 00 00 00 49 43 41 44 
    plex.cpp(31) : {124} normal block at 0x00345D90, 124 bytes long.
     Data: <             \4 > 00 00 00 00 00 00 00 00 00 00 00 00 D8 5C 34 00 
    D:\PROGRAMMING\VC++\&Iacute;&frac14;&ETH;&Icirc;&Iacute;&frac14;&Iuml;&ntilde;&acute;&brvbar;&Agrave;í\ICAD\ICADView.cpp(19) : {123} client block at 0x00345CD8, subtype 0, 112 bytes long.
    a CICADView object at $00345CD8, 112 bytes long
    D:\Programming\Vc++\&Iacute;&frac14;&ETH;&Icirc;&Iacute;&frac14;&Iuml;&ntilde;&acute;&brvbar;&Agrave;í\ICAD\ChildFrm.cpp(18) : {118} client block at 0x00345B48, subtype 0, 200 bytes long.
    a CChildFrame object at $00345B48, 200 bytes long
    plex.cpp(31) : {117} normal block at 0x00345A90, 124 bytes long.
     Data: <     Z4     HY4 > 00 00 00 00 A0 5A 34 00 00 00 00 00 48 59 34 00 
    D:\PROGRAMMING\VC++\&Iacute;&frac14;&ETH;&Icirc;&Iacute;&frac14;&Iuml;&ntilde;&acute;&brvbar;&Agrave;í\ICAD\ICADDoc.cpp(18) : {116} client block at 0x00345948, subtype 0, 260 bytes long.
    a CICADDoc object at $00345948, 260 bytes long
    array_p.cpp(110) : {98} normal block at 0x003458F0, 20 bytes long.
     Data: <     J4         > 00 00 00 00 04 4A 34 00 00 00 00 00 CD CD CD CD 
    array_p.cpp(71) : {97} normal block at 0x003458A8, 4 bytes long.
     Data: <    > 00 00 00 00 
    winfrm2.cpp(66) : {96} client block at 0x003457C0, subtype 0, 168 bytes long.
    a CDockBar object at $003457C0, 168 bytes long
    array_p.cpp(71) : {95} normal block at 0x00345778, 4 bytes long.
     Data: <    > 00 00 00 00 
    winfrm2.cpp(66) : {94} client block at 0x00345690, subtype 0, 168 bytes long.
    a CDockBar object at $00345690, 168 bytes long
    array_p.cpp(71) : {93} normal block at 0x00345648, 4 bytes long.
     Data: <    > 00 00 00 00 
    winfrm2.cpp(66) : {92} client block at 0x00345560, subtype 0, 168 bytes long.
    a CDockBar object at $00345560, 168 bytes long
    winfrm2.cpp(66) : {90} client block at 0x00345478, subtype 0, 168 bytes long.
    a CDockBar object at $00345478, 168 bytes long
    bardock.cpp(735) : {89} normal block at 0x00345230, 176 bytes long.
     Data: <  K_            > 9C 01 4B 5F CD CD CD CD CD CD CD CD CD CD CD CD 
    strcore.cpp(118) : {88} normal block at 0x00345420, 17 bytes long.
     Data: <                > 01 00 00 00 04 00 00 00 04 00 00 00 B9 F6 B6 AF 
    strcore.cpp(118) : {87} normal block at 0x003453C8, 17 bytes long.
     Data: <                > 01 00 00 00 04 00 00 00 04 00 00 00 CA FD D7 D6 
    strcore.cpp(118) : {86} normal block at 0x00345370, 17 bytes long.
     Data: <                > 01 00 00 00 04 00 00 00 04 00 00 00 B4 F3 D0 B4 
    {82} normal block at 0x00345110, 80 bytes long.
     Data: <                > 00 00 00 00 00 01 00 00 00 01 00 08 00 00 00 00 
    plex.cpp(31) : {79} normal block at 0x00345058, 124 bytes long.
     Data: <    hP4      J4 > 00 00 00 00 68 50 34 00 00 00 00 00 04 4A 34 00 
    strcore.cpp(118) : {67} normal block at 0x00344B40, 17 bytes long.
     Data: <            ICAD> 01 00 00 00 04 00 00 00 04 00 00 00 49 43 41 44 
    D:\PROGRAMMING\VC++\ICAD\ICAD.cpp(85) : {65} client block at 0x003448C0, subtype 0, 484 bytes long.
    a CMainFrame object at $003448C0, 484 bytes long
    plex.cpp(31) : {64} normal block at 0x00344808, 124 bytes long.
     Data: <            @F4 > 00 00 00 00 00 00 00 00 00 00 00 00 40 46 34 00 
    {63} client block at 0x003447A0, subtype 0, 32 bytes long.
    a CDocManager object at $003447A0, 32 bytes long
    strcore.cpp(118) : {62} normal block at 0x00344718, 74 bytes long.
     Data: <    =   =    ICA> 01 00 00 00 3D 00 00 00 3D 00 00 00 0A 49 43 41 
    D:\PROGRAMMING\VC++\ICAD\ICAD.cpp(77) : {61} client block at 0x00344640, subtype 0, 144 bytes long.
    a CMultiDocTemplate object at $00344640, 144 bytes long
    strcore.cpp(118) : {59} normal block at 0x003445E8, 19 bytes long.
     Data: <            File> 01 00 00 00 06 00 00 00 06 00 00 00 46 69 6C 65 
    strcore.cpp(118) : {58} normal block at 0x00344580, 29 bytes long.
     Data: <            Rece> 01 00 00 00 10 00 00 00 10 00 00 00 52 65 63 65 
    {57} normal block at 0x00344528, 28 bytes long.
     Data: <      L_  L_  L_> 06 00 00 00 14 CB 4C 5F 14 CB 4C 5F 14 CB 4C 5F 
    {56} normal block at 0x003444C0, 32 bytes long.
     Data: <  K_    ,E4  E4 > 8C 8D 4B 5F 06 00 00 00 2C 45 34 00 8C 45 34 00 
    Object dump complete.
    The thread 0x788 has exited with code 3 (0x3).
    The program 'D:\PROGRAMMING\VC++\ICAD\Debug\ICAD.exe' has exited with code 3 (0x3).
    4.尽管可以运行可执行程序,可是当执行命令ID_FILE_CLOSE和ID_CLOSE时,出现一个消息框,
    提示Debug Assertion Failed!  文件:dbgheap.c 行:1044 表达式: _CrtIsValidheapPointer(pUserData)
    5.如果必要,我可以把源工程发给您!