Memory Leak While using ACE_Thread

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 1
Reputation: Shreeravi is an unknown quantity at this point 
Solved Threads: 0
Shreeravi Shreeravi is offline Offline
Newbie Poster

Memory Leak While using ACE_Thread

 
0
  #1
Sep 5th, 2009
I have a situation where I have to scan some FTP Servers every 5 seconds to check for some specific files and download them if they exist. I use a Shell script to do FTP download operation and call it using C++ system call in a separate thread. (I am on Linux)
Here is how the code skeleton looks like:

  1. //Main thread
  2. handle_timeout //called every 5 seconds
  3. {
  4. HandleFTPOper();
  5. }
  6.  
  7. HandleFTPOper()
  8. {
  9. //Allocate memory in heap here
  10. FTPLoginDetails pFTPLogin = new FTPLoginDetails();
  11.  
  12. //fill the allocated memory (FTPLoginDetails) here
  13. ....
  14.  
  15. //Create a child thread and pass the FTP Login details to the thread as input argument
  16. ACE_Thread::spawn((ACE_THR_FUNC)&Download, pFTPLogin);
  17.  
  18. //return to main thread, no need to join/wait for the created thread.
  19. return;
  20. }
  21.  
  22. //child thread
  23. static void* Download(FTPLoginDetails *pData)
  24. {
  25. //Pass the FTP Login Details to shell script which will download some files
  26. system("Download.sh -a arguments");
  27.  
  28. //delete the input data
  29. delete [] pData;
  30. return 0;
  31. }
This code is working but it is leading to a memory leak. (the main process restarts after about an hour (i.e. after calling the download function around 720 times) owing to lack of memory for other variables in the system. I tried to reason as follows:

1) The pData is not being deleted somehow and getting leaked.
But this was not the case. I removed the pData heap allocation and declared it as a (stack) variable inside the child-thread, hard coded the log in details. But, there was still a memory leak.

2) The created threads are not getting destroyed.
This is the most likely possibility. Note that I deliberately left out any join or wait condition in the main thread as I do not want the main thread to wait for the child thread's FTP Operation to complete.

To sum up, I am creating a new thread every 5 seconds and leaving them on their own to do the FTP Operation and wind up. I don't bother to wait in the main thread after creating them and go on doing my things. I know it doesn't sound like a good design, but I am working on a very restrictive base code.

Now, the reason for memory leak could be because of the created child threads not destroyed.
But am I assuming anything wrong here? Isn't a ACE thread supposed to destory itself and free its resources when it "falls through" the function? What could be the problem?
Last edited by ~s.o.s~; Sep 5th, 2009 at 12:27 pm. Reason: Added code tags, please learn to use them.
Reply With Quote Quick reply to this message  
Reply

Tags
ace_thread, memoryleak

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC