先看看我的结构体,再看看我的链表的函数,有问题!
struct SmallNotion
{
    char *m_strSNotion;//字符串
    double m_fReal;//确信度
    int m_bSNFlag;//访问搜索标志
//初始值,-1则已访问了不匹配,1则访问了匹配,0则没访问
    struct SmallNotion* next;//指向下一个节点
};
{
    char *m_strNotion;//字符串
    int  m_iNFlag;//访问搜索标志
    //初始值,-1则已访问了不匹配,1则访问了匹配,0则没访问
    struct SmallNotion* pSNChain;//子链表的头指针
    struct Notion* next;//指向下一个节点
};
//////
............
//我建立链表时开辟的空间,这没问题!
    pNDBTemp = new struct Notion; 
    pNDBTemp->m_strNotion = new char[80];
..............
    pSNRealTemp = new struct SmallNotion;
    pSNRealTemp->m_strSNotion = new char[80];
..............
///////
我的释放空间函数
void CNOTIONANALYSEDlg::FreeSNB(SmallNotion *snPoint)
{
struct SmallNotion* snp;
while(snPoint)
{
snp = snPoint->next;
delete snPoint->m_strSNotion;
snPoint->m_strSNotion = NULL;
snPoint->next = NULL;
snPoint = snp;
}
}
void CNOTIONANALYSEDlg::FreeNDB(Notion *nPoint)
{
struct Notion* np;
while(nPoint)
{
np = nPoint->next;
delete nPoint->m_strNotion;
nPoint->m_strNotion = NULL;
nPoint->next = NULL;
delete nPoint;
nPoint = np;
}
}
//以下报错.不能正常退出!
         if(pNDBTemp != NULL) FreeNDB(pNDBTemp);
if(pSNRealTemp != NULL)FreeSNB(pSNRealTemp);
高手给点意见吧!我按时给分,不够再加!
我实在搞不清楚!说详细点,先谢了!

解决方案 »

  1.   

    我想是你在建立链表有问题吧,链表最后节点next可能不等于NULL。
    你可以调试看看什么地方出问题啊,或把代码写得详细点。下面的两个函数也有点问题:
    void CNOTIONANALYSEDlg::FreeSNB(SmallNotion *snPoint)
    {
    struct SmallNotion* snp;
    while(snPoint)
    {
    snp = snPoint->next;
    delete snPoint->m_strSNotion;//最好是delete []snPoint->m_strSNotion,养成好习惯。这里不会错是因为char是基本的类型,如果是对象,delete []可以对每一个对象调用析构函数
    snPoint->m_strSNotion = NULL;
    snPoint->next = NULL;
    delete snPoint;  //漏掉了
    snPoint = snp;
    }
    }
    void CNOTIONANALYSEDlg::FreeNDB(Notion *nPoint)
    {
    struct Notion* np;
    while(nPoint)
    {
    np = nPoint->next;
    delete nPoint->m_strNotion;//同上
    if (nPoint->pSNChain)
    {
    FreeNDB(nPoint->pSNChain);
    }
    delete nPoint;
    nPoint = np;
    }
    }
      

  2.   

    链表删除就是从结尾往前删除,操作链表的时候定义phead, ptail,并且修改它。
    while( ptail != phead )
    {
       if( ptail->plast )
       {
           ptail = ptail->plast;
           delete ptail->pnext;
           ptail->pnext = NULL;
       }
    }
      

  3.   

    void CNOTIONANALYSEDlg::OnSure()
    {
       .............//确定一次添加一个节点(Notion).
            pNDBTemp = new struct Notion;
    pNDBTemp->m_strNotion = new char[80];
    memcpy(pNDBTemp->m_strNotion, (LPCTSTR)m_strNotionEdit, m_strNotionEdit.GetLength()+1);
    pNDBTemp->pSNChain = NULL;
    pNDBTemp->m_iNFlag = 0;
    pNDBTemp->next = pNDB;
    pNDB = pNDBTemp;
    int m_iItemCount = m_cSNotionList.GetItemCount();
    if(m_iItemCount)
    {
        for(int m_iListNum = 0; m_iListNum < m_iItemCount; m_iListNum ++)
        { 
       CString m_strTemp1, m_strTemp2;
    pSNRealTemp = new struct SmallNotion;
    pSNRealTemp->m_strSNotion = new char[80];
    m_strTemp1= m_cSNotionList.GetItemText(m_iListNum, 0);
    memcpy(pSNRealTemp->m_strSNotion, (LPCTSTR)m_strTemp1, m_strTemp1.GetLength()+1);
    pSNRealTemp->m_bSNFlag = 0;
    m_strTemp2 = m_cSNotionList.GetItemText(m_iListNum, 1);
    pSNRealTemp->m_fReal = atof(m_strTemp2);//字符转为数
    pSNRealTemp->next = pNDBTemp->pSNChain;
    pNDBTemp->pSNChain = pSNRealTemp;
          }
               }//添加到链表
          .............
    }
    请帮我看看!
      

  4.   

    添加函数没问题,确认一下pNDB在定义时是不是初始化为NULL.
    如果想释放掉整个连表就直接用sunyard()的方法FreeNDB(PNDB)
    void CNOTIONANALYSEDlg::FreeNDB(Notion *nPoint)
    {
    struct Notion* np;
    while(nPoint)
    {
    np = nPoint->next;
    delete nPoint->m_strNotion;//同上
    if (nPoint->pSNChain)
    {
    //FreeNDB(nPoint->pSNChain);//他误写了
                              FreeSNB(nPoint->pSNChain);//修改
    }
    delete nPoint;
    nPoint = np;
    }
    }
    其中FreeSNB为 sunyard()修改后的方法
      

  5.   

    CNOTIONANALYSEDlg::CNOTIONANALYSEDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CNOTIONANALYSEDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CNOTIONANALYSEDlg)
             ...........
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    ............
        struct SmallNotion* pSNConclusion = NULL;//结论链表的头指针
        struct SmallNotion* pSNReal = NULL;//输入的已知事实(元概念链表)的头指针
        struct Notion* pNDB = NULL;//概念库(概念链表)的头指针
    struct Notion* pNUsed = NULL;//已使用的概念链表的头指针
    struct SmallNotion* pSNRealTemp = NULL;
    struct Notion* pNDBTemp = NULL;
    .............
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    初始化为NULL.应该没问题.谢谢 tiankong(sd).再帮我看看.我等了一上午了!
    跟帖呀!
      

  6.   

    1。你的FreeSNB和FreeSNB函数只是用来释放掉链表,对一个链表接点进行删除操作的函数应该没调用到这2个函数吧?
    2。用修改后函数释放掉链表只要
    if(pNDBTemp != NULL) FreeNDB(pNDBTemp);一句就会把整个连表释放(包括连表接点和连表接点为头的子连表都释放掉),
    不要在写if(pSNRealTemp != NULL)FreeSNB(pSNRealTemp);来释放子连表了
      

  7.   

    答 tiankong(sd)的:
    1.我还没有对节点删除,只添加!
    2.我的pSNRealTemp只是用来生成主链表时的交换,可以为局部变量的.
    所以没有循环来删除!
    谢谢 tiankong(sd).!
    我再综合的试试!
      

  8.   

    我的还是不行.我已经改正了几点上述的错误!
    再看看下面:
    void CNOTIONANALYSEDlg::FreeNDB(Notion *nPoint)
    {
    struct Notion* np;
    while(nPoint)
    {
    np = nPoint->next;//F5后,什么也没做,就点退出,光标最终停在此,
                                        //报告显示c0000005:Access Violation
                       //我可是什么也没做一执行就按OK退出的
    delete []nPoint->m_strNotion;
    if (nPoint->pSNChain)
    {
    FreeNDB(nPoint->pSNChain);
    }
    nPoint->m_strNotion = NULL;
    nPoint->next = NULL;
    delete nPoint;
    nPoint = np;
    }
    }
    高手请再看看,给点提示.
      

  9.   

    这个错误说明这个连表的最后一个接点的next指向不是NULL而是随机的,你在找找看
      

  10.   

    很显然nPoint使用了未申请内存的空间
    你注意一下调用FreeNDB的指针是否有误
      

  11.   

    FreeNDB(nPoint->pSNChain);//你没改
    应为:
    FreeSNB(nPoint->pSNChain);//修改
      

  12.   

    都改了,还是有问题.
    我认为刚初始化.
    struct SmallNotion* pSNConclusion = NULL;//结论链表的头指针
    struct SmallNotion* pSNReal = NULL;//输入的已知事实(元概念链表)的头指针
    struct Notion* pNDB = NULL;//概念库(概念链表)的头指针
    struct Notion* pNUsed = NULL;//已使用的概念链表的头指针
    struct SmallNotion* pSNRealTemp = NULL;
    struct Notion* pNDBTemp = NULL;
    再点退出,应该不执行下面的!
    if(pNDB != NULL)   FreeNDB(pNDB);//error
    if(pNDBTemp != NULL) FreeNDB(pNDBTemp);
    if(pNUsed != NULL) FreeNDB(pNUsed);
    if(pSNReal != NULL)FreeSNB(pSNReal);
    if(pSNRealTemp != NULL)FreeSNB(pSNRealTemp);
    if(pSNConclusion != NULL)  FreeSNB(pSNConclusion);
    再看看吧,我实在没办法了.
    还有两天就检查了.
    这个问题对这个程序来说太大了.
    救救我!
      

  13.   

    单步调试,看哪边修改了pNDB使它不是NULL了。而且if(pNDB != NULL)   FreeNDB(pNDB);就行了,后面的
    if(pNDBTemp != NULL) FreeNDB(pNDBTemp);
    if(pSNReal != NULL)FreeSNB(pSNReal);
    if(pSNRealTemp != NULL)FreeSNB(pSNRealTemp);都是没必要的
      

  14.   

    F5后,什么也没做,就点退出,光标最终停在此,
    报告显示c0000005:Access Violation
    我可是什么也没做,一执行就按退出的
    我认为刚初始化.接着马上就退出,应该不执行下面的!
    if(pNDB != NULL)   FreeNDB(pNDB);
    怎么回事?
      

  15.   

    举个范例:
    char *p=new char[10];
    //process code
    ...
    delete []p;  //delete之后,p指向的内存被释放了,但指针p还在,而且p也不等于NULL,
                 //此时,p成了野指针。
    while(p)     //所以,执行{ }内代码。
    {
     ....
    }在看看你的while语句:
    while(snPoint)
    {
    snp = snPoint->next;
    delete snPoint->m_strSNotion;
    snPoint->m_strSNotion = NULL;
    snPoint->next = NULL;
    snPoint = snp;
    }
    snPoint始终不等于NULL,所以死循环。
      

  16.   

    谢谢了!有点懂了,
    但运行后,什么也没做,就点退出,直接退出了.
    我认为刚初始化.接着马上就退出,应该不执行下面的!
    if(pNDB != NULL)   FreeNDB(pNDB);
    这时,pNDB = NULL,怎么回事?
    但还是好像执行了FreeNDB(pNDB);
      

  17.   

    不好意思,我搞错了,
    snPoint最后会是NULL,
    给你误导,很抱歉!
      

  18.   

    感谢,感谢!看了上面的帖子,我才知道原来有个F5的功能,我以前查错误全是看代码的或者用断言的。这下方便我啦!THANK
      

  19.   

    谢谢各位!60分太少,下次多给吧!
    特感谢 sunyard()  tiankong(sd) devzhao(疯狂IT) 
    大哥 ColderRain(莫走重辄路) 帮我解决了实质的问题
    也感谢其他的友情关注!