Menu

(error) Memory leak

2020-01-16
2020-01-17
  • Alberto Pellegrini

    I wrote the following main.cpp program to show a cppcheck behaviour for me unexpected:

    #define NULL (char*)0
    
    int a (void)
    {
        char *buff=NULL;
        if (NULL == (buff=new char(2)))
            return -1;
        delete buff;
    
        return 0;
    }
    
    int b (void)
    {
        char *buff=nullptr;
        if (nullptr == (buff=new char(2)))
            return -1;
        delete buff;
    
        return 0;
    }
    
    int main (int argc, char**argv)
    {
        a();
        b();
    }
    

    The following compile instruction benerate no message

    > g++ -std=c++11 main.cpp
    

    When I invoke cppcheck i receive an unexpectec memory leak error only in case I use nullptr.

    > cppcheck --enable=all main.cpp  
    Checking main.cpp ...
    [main.cpp:17]: (error) Memory leak: buff
    > cppcheck --version
    Cppcheck 1.82
    > 
    

    I don't understand if the cppcheck message is correct and, if yes, why.
    Does anyone can help me?
    Thanks

     
  • versat

    versat - 2020-01-16

    As far as I remember this was a false positive that has been fixed in one of the last released versions.
    When the allocation, assignment and comparision for null pointer was in the same if clause then Cppcheck produced false positives.
    I would try using a newer Cppcheck version. Cppcheck 1.90 has been released a few weeks ago.
    There were many things fixed and enhancements added since 1.82.

     
  • rikard

    rikard - 2020-01-16

    I tested your example and from cppcheck 1.87 and forward, there is no warning.

     
    • Alberto Pellegrini

      Thanks for the support!

      Il presente messaggio e-mail e ogni suo allegato devono intendersi indirizzati esclusivamente al destinatario indicato e considerarsi dal contenuto strettamente riservato e confidenziale. Se non siete l'effettivo destinatario o avete ricevuto il messaggio e-mail per errore, siete pregati di avvertire immediatamente il mittente e di cancellare il suddetto messaggio e ogni suo allegato dal vostro sistema informatico. Qualsiasi utilizzo, diffusione, copia o archiviazione del presente messaggio da parte di chi non ne è il destinatario è strettamente proibito e può dar luogo a responsabilità di carattere civile e penale punibili ai sensi di legge.
      Questa e-mail ha valore legale solo se firmata digitalmente ai sensi della normativa vigente.


      The contents of this email message and any attachments are intended solely for the addressee(s) and contain confidential and/or privileged information.
      If you are not the intended recipient of this message, or if this message has been addressed to you in error, please immediately notify the sender and then delete this message and any attachments from your system. If you are not the intended recipient, you are hereby notified that any use, dissemination, copying, or storage of this message or its attachments is strictly prohibited. Unauthorized disclosure and/or use of information contained in this email message may result in civil and criminal liability. “
      This e-mail has legal value according to the applicable laws only if it is digitally signed by the sender
      Da: Rikard Falkeborn [mailto:rikard@users.sourceforge.net]
      Inviato: giovedì 16 gennaio 2020 21:02
      A: [cppcheck:discussion]
      Oggetto: [cppcheck:discussion] (error) Memory leak

      I tested your example and from cppcheck 1.87 and forward, there is no warning.


      (error) Memory leakhttps://sourceforge.net/p/cppcheck/discussion/general/thread/fa631a6456/?limit=25#537d


      Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/cppcheck/discussion/general/

      To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/

       

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.