DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 7 of 7

Thread: memory leak

Hybrid View

  1. #1
    Join Date
    Apr 2008
    Posts
    23

    memory leak

    hello,

    what do you suggest me to find out if my code has memory leaks or not? how can i determine this?
    thanks

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi,
    one way to find out whether you got a memory leak is to monitor your process's size over time. If the process grows and and does not shrink again, then that's a strong indication that you got a leak somewhere.
    The other method is plainly look at the code. That sounds daunting, but is not so bad after you know what you have to look for.
    Leaks only occur when you allocate memory dynamically with "new" (or malloc,...) and never "delete" (free) it.
    Typical places to look for these operations are Constructors and Destructors.
    There are also tools out there that compile your code and find places where memory is allocated and not deallocated or that monitor the run-time, but most of them are not free and their usefulness is very much object of discussion.

    But To be honest nowadays you can get pretty far without ever using new/delete or malloc/free. The STL allows you to use dynamic containers that take away the headache of memory management. You should use them whereever possible (in my opinion) instead of new/delete'ed objects. There are occasions where you do need to use dynamic allocation (like when you want to exploit polymorhism), but in the majority of cases STL containers give you what you need plus the safety of automatic memory mgt.

    Hope that helps,
    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  3. #3
    Join Date
    Apr 2008
    Posts
    23
    Thanks very much for your suggesttions...Actually, sometimes a light is needed on the way of learning programming.
    Here's some piece of my code:
    Code:
    pthread_mutex_t writebuf_mutex=PTHREAD_MUTEX_INITIALIZER;
    int* write_buf=NULL;
    void* writer(void* arg){
        while(1){
            pthread_mutex_lock(&writebuf_mutex);
            if (write_buf!=NULL){
                for (int j=0;j<1000*1000*100;j++);//write duration
                int val = *write_buf;
                delete write_buf;
                write_buf=NULL;
                printf("******************write:%d\n", val);
            }
            pthread_mutex_unlock(&writebuf_mutex);
        }
    }
    I wonder if
    Code:
                int val = *write_buf;
                delete write_buf;
                write_buf=NULL;
    these lines causes memory leak or not? IMHO, there's not a problem but i'm not sure...
    Thanks again.

  4. #4
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    These rows are defo dodgy!
    Code:
    int val = *write_buf;
    This line leaves your pointer write_buf pointing into the random void. deleting it is disastrous: not a memory leak, but you almost certainly attempt to delete memory that is not yours...

  5. #5
    Join Date
    Apr 2008
    Posts
    23
    sorry,I didn't send the whole code. write_buf is new'ed in another function.so, if it is new'ed, I can delete it safely,don't I?
    here's the more of the code:
    Code:
    void* writer(void* arg){
        while(1){
            pthread_mutex_lock(&writebuf_mutex);
            if (write_buf!=NULL){
                for (int j=0;j<1000*1000*100;j++);//write duration
                int val = *write_buf;
                delete write_buf;
                write_buf=NULL;
                printf("******************write:%d\n", val);
            }
            pthread_mutex_unlock(&writebuf_mutex);
        }
    }
    void* processor(void* arg){
        while(1){
            if (read_buf!=NULL){//data ready to be read
                for (int j=0;j<1000*1000*100;j++);//processing duration
                pthread_mutex_lock(&readbuf_mutex);
                int val=(*read_buf);
                delete read_buf;
                read_buf=NULL;
                pthread_mutex_unlock(&readbuf_mutex);
                int res = val+1;
                write_buf=new int;
                *write_buf=res;
                printf("********process:%d--->%d\n", val, res);
            }
        }
    }
    as you see, write_buf is new'ed in the void* processor() function and is deleted in the void* writer() function.so , i can safely delete the int* write_buf and there's not memory leak.am i right?

    thanks very muchfor your great help.i got sure now...

  6. #6
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    but you declare a local int * write_buf !!!.
    Your global write_buf will not be visible inside your if - block.

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    It should be simple, but it isn't: locate every new and new[], and match them with exactly one unconditional delete and delete[] respectively. The problem is that in man cases, the function that allocated the resource isn't the one that releases it, and there is also branching that might create so many code paths that it isn't always easy to guarantee that every object allocated is also deleted. Which is why people use smart pointers in C++, isn't it?
    Danny Kalev

Similar Threads

  1. Possible Memory Leak in Java application
    By esi-eric in forum Java
    Replies: 3
    Last Post: 02-24-2005, 06:58 PM
  2. Announcing a memory leak in javax.comm
    By Jason Jakob in forum Java
    Replies: 0
    Last Post: 06-30-2002, 01:14 PM
  3. help memory
    By kathy in forum Java
    Replies: 0
    Last Post: 11-02-2001, 04:22 PM
  4. Interface leaks in C#?
    By Kevin Burton in forum .NET
    Replies: 12
    Last Post: 10-09-2000, 10:29 AM
  5. Re: MS Provider For Oracle : Memory Leak ?
    By John Grandy in forum VB Classic
    Replies: 0
    Last Post: 04-08-2000, 08:35 PM

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