DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    John Cruz Guest

    How do you prevent memory leaks?



    Can anyone give me hints on how to prevent memory leaks when programming
    in VC++. also, how would you know if your program is causing memory leaks?
    well, anyways thanks for the input.

  2. #2
    Danny Kalev Guest

    Re: How do you prevent memory leaks?



    John Cruz wrote:
    >
    > Can anyone give me hints on how to prevent memory leaks when programming
    > in VC++.

    Simply by not creating leaks in the first place. Seriously, memory leaks
    aren't a Bad Karma; they are always a result of negligent programming.
    Minimize the use of direct memory allocations; use standard container
    classes such as string and vector and think thrice before you allocate
    anything on the free-store -- in most cases you will find that you can
    just as well allocate it on the stack or statically.

    also, how would you know if your program is causing memory leaks?
    > well, anyways thanks for the input.


    There's way to know that for sure, really. In most cases, a leaking
    program that runs for a long time simply leaks so much memory that the
    entire OS crashes. There are commercial tools that seemingly detect
    memory leaks but they are not very reliable so my advice is to simply
    stick to good programming practices and minimize the chances of leaks in
    the first place. Give me any memory leak and I'll show you how it could
    have been prevented by using a standard feature of C++. It's as simple
    as that.

    Danny

  3. #3
    ralph Guest

    Re: How do you prevent memory leaks?


    "John Cruz" <Baller187_69@hotmail.com> wrote:
    >
    >
    > Can anyone give me hints on how to prevent memory leaks when programming
    >in VC++. also, how would you know if your program is causing memory leaks?
    >well, anyways thanks for the input.


    Read up on using DEBUG NEW. Install the CRT source code.

  4. #4
    Dmitrij Guest

    Re: How do you prevent memory leaks?


    > Can anyone give me hints on how to prevent memory leaks when programming
    >in VC++.

    Try use std::auto_ptr template for pointers.
    >also, how would you know if your program is causing memory leaks?
    >well, anyways thanks for the input.

    There are many program that do this work. For example BoundsChecker.


  5. #5
    Djalma Santos Filho Guest

    Re: How do you prevent memory leaks?


    Danny Kalev <dannykk@inter.net.il> wrote:

    Give me any memory leak and I'll show you how it could
    >have been prevented by using a standard feature of C++. It's as simple
    >as that.
    >
    >Danny


    Hi Danny

    I found very interesting what you wrote, but I really cannot figure out a
    way to avoid the memory leak produced by my example code bellow, if the caller
    of my method does not deallocate the char*. Please help me, I really need
    to return a char* the way I did.

    Thanks

    char* CClickBankingErro::MensagemErro()
    {
    char MsgBuffer[1000];
    ostrstream MsgStream(MsgBuffer,1000);
    MsgStream.seekp(0);
    MsgStream << "Erro " << CodigoErro << endl;

    if (strlen(m_ContextoClickBanking) > 0)
    MsgStream << m_ContextoClickBanking << endl;

    if (strlen(m_Descricao) > 0)
    MsgStream << m_Descricao << endl;

    if (strlen(Descricao) > 0)
    MsgStream << m_InformacaoAdicional << endl;

    MsgStream << ends;

    return (_strdup(MsgBuffer));
    }

  6. #6
    Danny Kalev Guest

    Re: How do you prevent memory leaks?

    It's simple. How about:

    void CClickBankingErro::MensagemErro(string & str)
    {
    string MsgBuffer;
    ostrstream MsgStream(MsgBuffer,1000);
    MsgStream.seekp(0);
    MsgStream << "Erro " << CodigoErro << endl;

    if (strlen(m_ContextoClickBanking) > 0)
    MsgStream << m_ContextoClickBanking << endl;

    if (strlen(m_Descricao) > 0)
    MsgStream << m_Descricao << endl;

    if (strlen(Descricao) > 0)
    MsgStream << m_InformacaoAdicional << endl;

    MsgStream << ends;

    str=MsgBuffer;
    }


    (in fact, using a string you can simplify and improve the code of this
    function further, e.g., by eliminating all strlen() calls but I'm
    showing only how to avoid memory leaks).

    Danny

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links