Hello!
The program is leaking memory, launched through valgrind and I just can’t understand: it writes possible lost in the following function on the line 'return FALSE':
ReplaceStr(string& str, const string& from, const string& to) { size_t start_pos = str.find(from); if(start_pos == std::string::npos) return FALSE; str.replace(start_pos, from.length(), to); return TRUE; } Please help me, please, have already broken my whole head. What am I doing wrong?
UPDATE
int CameraType::MakeSourceUrls() { string tmp_str; BaseUrlArray *BaseUrls; BaseUrls = new BaseUrlArray; GetCamTypeFromXml(CamRecParam->ModelName, BaseUrls); for (int i = PIPE_MJPEG; i <= PIPE_H264; i++) { if (BaseUrls->Urls[i].murl.length() < 14) { sprintf(str, "ERROR: Base URL for camera '%s' for type = %d is not valid\n", CamRecParam->Name.c_str(), i); AppendLog(str, "CameraType::MakeSourceUrls", ERROR); CamRecParam->UrlVidFormat[i].url = ""; continue; } tmp_str = BaseUrls->Urls[i].murl; ReplaceStr(tmp_str, "IPADDRESS", CamRecParam->IP); CamRecParam-> IP is a regular string
UPDATE # 2
For the code below, valgrind returns 0 errors. Type auto replaced by size_type, tk. I do not have auto support on this platform.
#include <iostream> #include <string> using namespace std; bool ReplaceStr(string& str, const string& from, const string& to) { std::string::size_type start_pos = str.find(from); if(start_pos == std::string::npos) return false; str.replace(start_pos, from.length(), to); return true; } int main() { string haystack = "Soft cat, warm cat, little ball of fur"; string needle = "cat"; string replacement = "kitty"; ReplaceStr(haystack, needle, replacement); ReplaceStr(haystack, needle, replacement); cout << haystack; return 0; } UPDATE # 3
Full valgrind log: http://pastebin.com/UNbR1Jzr
UPDATE # 4
CamRecParam is just a wrapper class for camera properties (type, IP address and other parameters). It is a class variable CameraType. Created when the instance of the CameraType class is initialized. MakeSourceUrls reads the xml configuration file and puts the received camera parameters into the corresponding CamRecParam fields. All this is done from the main program thread. The CameraType class also has an instance variable of the Writer class, which is responsible for connecting to the camera and recording the stream to disk. This is the actual writing cycle of this Writer and is executed in a separate thread. Yes, he takes some fields from CamRecParam, but only reads them ...
UPDATE # 5
Modified the test program and got the same valgrind errors! The intrigue is heating up ...
Program:
#include <iostream> #include <string> #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/time.h> #include <stdlib.h> using namespace std; bool ReplaceStr(string& str, const string& from, const string& to) { std::string::size_type start_pos = str.find(from); if(start_pos == std::string::npos) return false; str.replace(start_pos, from.length(), to); return true; } int main() { time_t t; struct tm tm; struct timeval td; char OutString[254]; t = time(NULL); localtime_r(&t, &tm); gettimeofday(&td, 0); sprintf(OutString + strftime(OutString, 254, "%d.%m.%Y %T", &tm), ".%.3d", (int) (td.tv_usec / 1000)); string haystack = "Soft cat, warm cat, little ball of fur"; string needle = "cat"; string replacement = "kitty"; ReplaceStr(haystack, needle, replacement); ReplaceStr(haystack, needle, replacement); cout << haystack; cout << OutString; return 0; } Valgrind log: http://pastebin.com/HitRG7fJ
CamRecParamclass. Where is it being created? What flows does it create? From what threadCameraType::MakeSourceUrls()? - VladDUsing Valgrind-3.10.0.SVN and LibVEX;). What is your system -uname -a; g++ --versionuname -a; g++ --version? (admin !!! the number of comments for help is not great enough). - avp