| | |
Memory leaks through stringstream.str()
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
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?
Thanks for any insight!
-Doug
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?
C++ Syntax (Toggle Plain Text)
short SerialPort::open(int portNum, const string &settings){ stringstream deviceName; deviceName << "\\\\.\\COM" << portNum; // weird \\.\ allows emulated ports to work return open(deviceName.str(), settings); } short SerialPort::open(const string &deviceName, const string &settings){ cout << "SerialPort::open()" << endl; hComm = CreateFile(deviceName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); DCB dcb; // the following commented lines have been abbreviated //GetCommState(hComm, &dcb) //BuildCommDCB(settings.c_str(), &dcb) //SetCommState(hComm, &dcb) //SetCommTimeouts(hComm, &timeouts) cout << "Serial Port " << deviceName << " opened OK" << endl; return 0; }
Thanks for any insight!
-Doug
>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.
>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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: stream and variable declaration problem
- Next Thread: Use ostringstream to make a string
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






