Menu

Memory leak detected only once on duplicated code

2016-05-20
2016-05-23
  • Rei Angelus

    Rei Angelus - 2016-05-20

    Hello,

    In the code below, I don't understand why cppcheck detects a memory leak on pnodeRight in the first case but not in the second.

    inznode inznodeCreate(short isTypeNode, short isAttr,
    inznode
    pnodeLeft,
    inznode pnodeRight, uint32_t ulStatus,
    olexYaccPos
    plexYaccPosB,
    olexYaccPos plexYaccPosE)
    {
    inznode
    pnodeNew;

    omsgsDebugIn("Type:%i Attr:%i", isTypeNode, isAttr);
    
    pnodeNew = calloc(1, sizeof(inznode));
    if(pnodeNew)
    {
        pnodeNew->isTypeNoeud = isTypeNode;
        pnodeNew->isAttr = isAttr;
        pnodeNew->left.pnode = pnodeLeft;
        pnodeNew->right.pnode = pnodeRight;
    
        pnodeNew->ulStatus = ulStatus;
    
        if(plexYaccPosB != NULL)
        {
            pnodeNew->iColStart = plexYaccPosB->iFirstColumn;
            pnodeNew->iLineStart = plexYaccPosB->iFirstLine;
        }
        if(plexYaccPosE != NULL)
        {
            pnodeNew->iColEnd = plexYaccPosE->iLastColumn;
            pnodeNew->iLineEnd = plexYaccPosE->iLastLine;
        }
    }
    omsgsDebugOut("%p", pnodeNew);
    return pnodeNew;
    

    }

    int inznodeTranslateTemporal(inznode pnode, ochaine pchaineColTable,
    int16_t iColStart, int16_t iColEnd)
    {
    inznode pnodeColStart;
    inznode
    pnodeColEnd;
    inznode pnodeLeft = NULL;
    inznode
    pnodeRight = NULL;
    inzcol pcolStart;
    inzcol
    pcolEnd;
    ochar pcharColStart = NULL;
    ochar
    pcharColEnd = NULL;
    int iRc = INZNODE_NO_ERROR;

    assert(pnode->isTypeNoeud == NODE_OPRT);
    
    omsgsDebugIn("(0x%x, 0x%x)", pnode->isTypeNoeud, pnode->isAttr);
    
    iRc = inznodeCheckExpression(pnode, pchaineColTable);
    
    if (SEVERITY(iRc) <= SEVERITY_WARNING)
    {
    inzcol*         pcol;
    ochaineCursor   chaineCursor;
    
        ochaineCursorInit(&chaineCursor, pchaineColTable, OCHAINE_FROM_TOP);
        /* >RELAX<NOASGCOND> : cursor */
        while((pcol = (inzcol*)ochaineCursorGet(&chaineCursor)) != NULL)
        {
            if (pcol->iNumeroDb2 == iColStart)
            {
                pcharColStart = ocharClone(pcol->pcharName);
            }
    
            if (pcol->iNumeroDb2 == iColEnd)
            {
                pcharColEnd = ocharClone(pcol->pcharName);
            }
    
            ochaineCursorNext(&chaineCursor);
        }
        ochaineCursorDestroy(&chaineCursor);
    
        assert((pcharColStart != NULL) && (pcharColEnd != NULL));
    
        pcolStart = inzcolCreate(pcharColStart, 0, NULL, 0);
        pcolEnd = inzcolCreate(pcharColEnd, 0, NULL, 0);
    
        pnodeColStart = inznodeCreate(NODE_VAR, NODE_CTUNK, (inznode*)pcolStart,
                                      NULL, 0, NULL, NULL);
        pnodeColEnd = inznodeCreate(NODE_VAR, NODE_CTUNK, (inznode*)pcolEnd,
                                    NULL, 0, NULL, NULL);
    
        switch(pnode->isAttr)
        {
        case NODE_FROM_TO_OP:
            pnodeLeft = inznodeCreate(NODE_OPRT, NODE_INF_OP, pnodeColStart,
                                      inznodeClone(pnode->right.pnode), 0,
                                      NULL, NULL);
            pnodeRight = inznodeCreate(NODE_OPRT, NODE_SUP_OP, pnodeColEnd,
                                       inznodeClone(pnode->left.pnode), 0,
                                       NULL, NULL);
            pnodeRight = inznodeCreate(NODE_OPRT, NODE_AND_OP, pnodeLeft,
                                       pnodeRight, 0, NULL, NULL);
            pnodeLeft = inznodeCreate(NODE_OPRT, NODE_INF_OP,
                                      inznodeClone(pnode->left.pnode),
                                      inznodeClone(pnode->right.pnode),
                                      0, NULL, NULL);
            break;
    
        case NODE_BETWEEN_AND_OP:
            pnodeLeft = inznodeCreate(NODE_OPRT, NODE_IEG_OP, pnodeColStart,
                                      inznodeClone(pnode->right.pnode), 0,
                                      NULL, NULL);
            pnodeRight = inznodeCreate(NODE_OPRT, NODE_SUP_OP, pnodeColEnd,
                                       inznodeClone(pnode->left.pnode), 0,
                                       NULL, NULL);
            pnodeRight = inznodeCreate(NODE_OPRT, NODE_AND_OP, pnodeLeft,
                                       pnodeRight, 0, NULL, NULL);
            pnodeLeft = inznodeCreate(NODE_OPRT, NODE_IEG_OP,
                                      inznodeClone(pnode->left.pnode),
                                      inznodeClone(pnode->right.pnode),
                                      0, NULL, NULL);
            break;
    
        default:
            assert(FALSE);
            break;
        }
    
        if (pnode->left.pnode != NULL)
        {
            inznodeDestroy(pnode->left.pnode);
        }
    
        if (pnode->right.pnode != NULL)
        {
            inznodeDestroy(pnode->right.pnode);
        }
    
        pnode->isAttr = NODE_AND_OP;
        pnode->left.pnode = pnodeLeft;
        pnode->right.pnode = pnodeRight;
        pnode->ulStatus = 0;
    }
    
    omsgsDebugOut("=0x%x",iRc);
    return iRc;
    

    }

    There's any reason for this?

    Thanks

     
  • Daniel Marjamäki

    I didn't look at the code.. it's hard to see the problems in that.

    Cppcheck analyse each variable and tries to find a leak.

    if you fix the first leak will the second leak be discovered then?

    when leaks are reported, there might be further leaks that cppcheck can detect that are not reported.

    when no leaks are reported, every leak that cppcheck can detect has been reported.

     

    Last edit: Daniel Marjamäki 2016-05-22
  • Rei Angelus

    Rei Angelus - 2016-05-23

    You are right, I fixed the first leak and the second appeared.

    I didn't know that the detection worked like that.

    Thanks for your answer.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.