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. Cannot start X again ....
    By DjiT in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 02-23-2005, 11:51 PM
  2. Random Access Memory (RAM) Guide
    By Waka-Gashira in forum Windows - General Topics
    Replies: 2
    Last Post: 01-05-2005, 10:47 PM
  3. Memory????
    By comtux in forum Linux - General Topics
    Replies: 10
    Last Post: 09-25-2004, 12:41 AM
  4. Memory errors with XP Pro
    By in forum Windows - General Topics
    Replies: 18
    Last Post: 04-15-2004, 10:02 PM
  5. Memory leak?
    By t048 in forum Linux - Software, Applications & Programming
    Replies: 10
    Last Post: 09-28-2002, 10:37 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
  •