Quantcast

Maximum PC

It is currently Mon Dec 29, 2014 6:32 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Is this a memory leak? (use of delete keyword in C++)
PostPosted: Tue Apr 20, 2010 8:26 am 
8086
8086

Joined: Tue May 09, 2006 9:58 am
Posts: 67
Location: Pittsburgh
I feel like I'm missing something fundamental on when to use delete in C++.

I know you're supposed to use 'delete' to release memory when you're done using any object created with 'new'. However, what about objects that are returned by a function and assigned to a variable without the use of 'new'? For example, see the following code that looks for the most recently written file. It searches through every file in a path, checks the time written, and keeps track of the latest.

The function GetFilePath() returns a CString. But it is in a loop that will repeatedly call this function and assign the result to a variable. What happens if this is looped many times? Does the previous CString returned by the function still exist in memory when it is reassigned to a new CString? Is this a memory leak? Do I need to perform a delete on the 'latest_file_name' object every time it is reassigned? Or is there something else going on here that I don't understand?

Every reference I look up on using delete always talks about using it in the context of also using the 'new' keyword, so I don't understand if it applies in this case.

Thanks for any help clearing this up.

Code:
void getFile(char* path){

  CString file_name;
  CString strToFind;
  CTime new_time;
  CTime file_write_time;
  CFileFind finder;

  bool bFound;

  strToFind.Format(_T("%s\\*.*"), path);
  bFound = finder.FindFile(strToFind);

  while(bFound)
  {
    bFound = finder.FindNextFile();
    finder.GetLastWriteTime(new_time);

    //If next file write time is more recent then previous
    if(new_time > file_write_time)
    {
       file_write_time = new_time;
       latest_file_name = finder.GetFilePath();
    }
  }

   //.... more code
}


Top
  Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2010 10:12 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24238
Location: Granite Heaven
It *should* be collected when the variable is no longer in scope, but you should delete the variable when it is no longer in use as a best practice.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2010 10:44 am 
8086
8086

Joined: Tue May 09, 2006 9:58 am
Posts: 67
Location: Pittsburgh
Yeah, but say GetFilePath() gets called 10 times in consecutive loops. The entire time latest_file_name stays within scope. However, ten different CStrings were created. After the 10th time, do all 10 objects still exist in memory, even though latest_file_name is only assigned to the most recent? Or were the previous 9 somehow collected?


Top
  Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2010 11:42 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24238
Location: Granite Heaven
Since they are all assigned to the same variable name, I believe you're only using one object space ... you're writing over it with each loop.

I'm not certain, though .. it would depend on your compiler .. but I think that the behaviour I described is the standard.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2010 9:22 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
There are two types of memory: static and dynamic. Static memory is allocated at compile time. You can't reclaim it. Dynamic memory is allocated at runtime. You need to use delete to reclaim dynamic memory allocated on the heap (memory allocated using new or calloc, realloc, realloc in ansi-c).

JeffV wrote:
Yeah, but say GetFilePath() gets called 10 times in consecutive loops. The entire time latest_file_name stays within scope. However, ten different CStrings were created. After the 10th time, do all 10 objects still exist in memory, even though latest_file_name is only assigned to the most recent? Or were the previous 9 somehow collected?


It depends on how you allocated the memory. If you create 10 "new" objects inside of some function that you call, then yes, you've created 10 items on the heap. OTOH, if you create a buffer (ie memory block) in main on the heap, call some function that fills the buffer, then you should be fine. Rinse, wash, repeat... you should be fine as long as you don't overwrite the data before you use it.

Pointer errors can be very subtle and hard to find... be careful.


Top
  Profile  
 
 Post subject: Regarding static object deletion
PostPosted: Sat May 01, 2010 8:24 pm 
8086
8086

Joined: Sun Apr 12, 2009 9:14 am
Posts: 13
Consider the following:
Code:
#include <iostream>

class myClass{
public:
   myClass();
   ~myClass();
};

myClass::myClass(){
   std::cout << "object being constructed" << std::endl;
}

myClass::~myClass(){
   std::cout << "object being destroyed" << std::endl;
}

void createInstance();

void main(){
   createInstance();
}

void createInstance(){
   myClass theInstance;
}


You will observe that the destructor is called at the close of the called function. Static objects are managed during compile time as mentioned above. The thing to watch out for is whether you have any pointers defined in the classes you are creating, like links in a linked list, as an example. Unless the destructor defined in the static object explicitly deletes any dynamically created objects that are members of the static object's state, those objects will continue to occupy memory in the heap after the static object has been released. Be sure to appropriately implement your destructor if that is the case.


Top
  Profile  
 
 Post subject: Re: Regarding static object deletion
PostPosted: Sun May 02, 2010 11:34 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
jasonfactor wrote:
Consider the following:

Consider the following...

1) Type "[ code ]" without the spaces, hit enter then type "[ /code ]".
2) Copy your... wait for it... "code" from your IDE.
3) Paste your code on the line with the code tag.

;)

jasonfactor wrote:
The thing to watch out for is whether you have any pointers defined in the classes you are creating, like links in a linked list, as an example. Unless the destructor defined in the static object explicitly deletes any dynamically created objects that are members of the static object's state, those objects will continue to occupy memory in the heap after the static object has been released.


There are a couple things worth covering here...

1a) Although your "watch out for this" point is valid, the OOP example sort of muddies the water regarding memory leaks and pointers. Just to be extra clear for the newbies... OOP and memory allocation are two, not necessarily related, topics. You can allocate objects on the stack or heap; however, you don't have to use OOP in order to allocate memory on the heap.

1b) It is NOT a good idea to use OOP in the manner that jasonfactor did here. He is providing an example using some skeleton code. Instantiating and destroying an object in this manner is not a good idea.

1c) Finally, this is also a bad example of garbage collection... jk. =)

2) Again, just to be extra clear for the newbies. Should an abstract data type destroy it's contents upon destruction? For instance, I have a bunch of Foo that I iterate over doing various operations. I also have a heap of the same Foo that keeps things ordered based on one of the fields in Foo. Now I no longer need either the list or the heap... I'm not going to appreciate your ADT destroying all of my objects! What Jasonfactor is saying is that you should also destroy any supporting objects like Nodes that will typically be instantiated by the LinkedList class.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.