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

  • one
    Yes, something corrected. Now when launching the program, the error glib - free - invalid pointer 0x [] appeared, but I will deal with it. Built a schedule / proc / <pid_programs> / status | grep VmRSS with an interval of 5 minutes - there is a clear increase .. from 25084kB when you start yesterday at 19.00 and already 27528kB today at 14.30 ... And I definitely removed everything, except for one, but there it was not once leaked. There are still possible lost, indirect lost and still reachable, but the latter is probably due to the fact that I just kill the process of the program and no deinitialization takes place. - Xuch
  • one
    No leaks, and where they are there. @Xaruch, I think you are shooting memory. And it can give anything. And if the application is still multi-threaded ... _____ This is when random data is written to random addresses in the memory. The simplest and most typical case is int a [10]; a [10] = 0; Yes, modern compilers are aware of such errors and can add one empty element behind the array, and then check it. But if you write somewhere like this * (a + 100000) = 1 then no one will check. But where it will write down is a difficult matter. - KoVadim
  • one
    @Xaruch: Wait. Tell us again about the “life cycle” of the CamRecParam class. Where is it being created? What flows does it create? From what thread CameraType::MakeSourceUrls() ? - VladD
  • one
    @Xaruch, frankly, I do not know. In the evening I will try at home. This is hardly the case with the valgrind version (I was now chasing Using Valgrind-3.10.0.SVN and LibVEX; ). What is your system - uname -a; g++ --version uname -a; g++ --version ? (admin !!! the number of comments for help is not great enough). - avp
  • one
    (limit !!! therefore delete and transfer ...) I checked the test program on: Linux avp-ubu1 3.13.0-35-generic # 62-Ubuntu SMP Fri Aug 15 01:58:42 UTC 2014 x86_64 x86_64 x86_64 GNU / Linux g ++. real (Ubuntu 4.8.2-19ubuntu1) 4.8.2 valgrind-3.10.0.SVN also no leaks, and others swearing. - Regarding mutex. IMHO here read-only variables initialized in main thread before the main work does not affect . For them, lokas can and not do. Look for non-thread-safe functions (library) that are used in threads. At you adding of locks reduced parallelism and therefore errors in them became less often. - avp

0