Memory leaks through stringstream.str()

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

Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Memory leaks through stringstream.str()

 
0
  #1
Jan 8th, 2008
Hi,

I recently read that memory leaks can occur when using the string stream str() member function (http://www.troubleshooters.com/codecorn/memleak.htm). Is this true only for passing pointers or references to the created string object? Is this true at all?

For example, can anyone see if the following code would cause a leak if the first open function is called?

  1. short SerialPort::open(int portNum, const string &settings){
  2. stringstream deviceName;
  3.  
  4. deviceName << "\\\\.\\COM" << portNum; // weird \\.\ allows emulated ports to work
  5.  
  6. return open(deviceName.str(), settings);
  7. }
  8.  
  9. short SerialPort::open(const string &deviceName, const string &settings){
  10. cout << "SerialPort::open()" << endl;
  11.  
  12. hComm = CreateFile(deviceName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
  13.  
  14. DCB dcb;
  15.  
  16. // the following commented lines have been abbreviated
  17. //GetCommState(hComm, &dcb)
  18. //BuildCommDCB(settings.c_str(), &dcb)
  19. //SetCommState(hComm, &dcb)
  20. //SetCommTimeouts(hComm, &timeouts)
  21.  
  22. cout << "Serial Port " << deviceName << " opened OK" << endl;
  23. return 0;
  24. }

Thanks for any insight!
-Doug
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is online now Online
Posting Pro in Training

Re: Memory leaks through stringstream.str()

 
0
  #2
Jan 8th, 2008
The page appears to be over 10 years old, and the information probably older than that. Standard C++ does not have a class called ostrstream, and, to my knowledge, the standard C++ stringstream facility does not leak.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Memory leaks through stringstream.str()

 
0
  #3
Jan 8th, 2008
>I recently read that memory leaks can occur when
>using the string stream str() member function
You're mixing up two different classes: strstream and stringstream. The former is an ancient class that was part of the old iostream library and should be avoided because it was hard to use correctly. The latter is the standard replacement for it which is hard not to use correctly.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Memory leaks through stringstream.str()

 
0
  #4
Jan 8th, 2008
Thanks guys, that's very encouraging indeed.
Reply With Quote Quick reply to this message  
Reply

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