Results 1 to 4 of 4

Thread: Is this a memory leak?

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Is this a memory leak?

    Code:
    MyClass *myclass;
    
    ...
    
    while (blah blah){
     * * *...
     * * *myclass = new MyClass (blah blah);
     * * *...
    }
    
    delete (myclass);
    Each time the loop runs, a new object of myclass is created and the pointer is reassigned to the new class. *But the old class is still in memory somewhere, right? *Should the [tt]delete (myclass);[/tt] statement go inside the loop?

  2. #2

    Re: Is this a memory leak?

    Probably. I assume this is C++ (looks like Java psuedo code :P) in which case you need to explicitly delete the object so memory is not being sucked up by the loop. Reall I think it depends how many times the loop runs. You could easily define another class that does the job of cleaning up the dead objects from the loop.

  3. #3
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re: Is this a memory leak?

    Java has automatic garbage collection, so this wouldn't be an issue. Yes, it is in C++.

  4. #4

    Re: Is this a memory leak?


    Butt the old class is still in memory somewhere, right? *Should the [tt]delete (myclass);[/tt] statement go inside the loop?
    Yes and yes.

Similar Threads

  1. Squid memory leak problems
    By comet in forum Linux - Hardware, Networking & Security
    Replies: 0
    Last Post: 01-20-2012, 04:26 PM
  2. Memory????
    By comtux in forum Linux - General Topics
    Replies: 10
    Last Post: 09-25-2004, 12:41 AM
  3. Memory leak?
    By t048 in forum Linux - Software, Applications & Programming
    Replies: 10
    Last Post: 09-28-2002, 10:37 PM
  4. Web leak of Linux lets Hat out of the bag
    By trickster in forum Linux - General Topics
    Replies: 1
    Last Post: 09-25-2002, 11:50 PM
  5. Another "Valentine Memo"  leak...
    By pbharris in forum General Chat
    Replies: 12
    Last Post: 01-05-2002, 07:40 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
  •