{"action":"created","issue":{"url":"https://api.github.com/repos/cpputest/cpputest/issues/665","labels_url":"https://api.github.com/repos/cpputest/cpputest/issues/665/labels{/name}","comments_url":"https://api.github.com/repos/cpputest/cpputest/issues/665/comments","events_url":"https://api.github.com/repos/cpputest/cpputest/issues/665/events","html_url":"https://github.com/cpputest/cpputest/issues/665","id":85453401,"number":665,"title":"Valgrind detects memory leak in CppUTest","user":{"login":"sbellus","id":436473,"avatar_url":"https://avatars.githubusercontent.com/u/436473?v=3","gravatar_id":"","url":"https://api.github.com/users/sbellus","html_url":"https://github.com/sbellus","followers_url":"https://api.github.com/users/sbellus/followers","following_url":"https://api.github.com/users/sbellus/following{/other_user}","gists_url":"https://api.github.com/users/sbellus/gists{/gist_id}","starred_url":"https://api.github.com/users/sbellus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sbellus/subscriptions","organizations_url":"https://api.github.com/users/sbellus/orgs","repos_url":"https://api.github.com/users/sbellus/repos","events_url":"https://api.github.com/users/sbellus/events{/privacy}","received_events_url":"https://api.github.com/users/sbellus/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-06-05T06:44:45Z","updated_at":"2015-06-09T06:53:47Z","closed_at":null,"body":"We found out memory leak in CppUTest version 3.7.2 in function SimpleString::split.\r\nSee comment how we fix it.\r\n\r\n```cpp\r\nvoid SimpleString::split(const SimpleString& delimiter, SimpleStringCollection& col) const\r\n{\r\n    size_t num = count(delimiter);\r\n    size_t extraEndToken = (endsWith(delimiter)) ? 0 : 1U;\r\n    col.allocate(num + extraEndToken);\r\n\r\n    char* str = buffer_;\r\n    char* prev;\r\n    for (size_t i = 0; i < num; ++i) {\r\n        prev = str;\r\n        str = StrStr(str, delimiter.buffer_) + 1;\r\n        size_t len = (size_t) (str - prev) + 1;\r\n        deallocStringBuffer(col[i].buffer_); // this is fix of memory leak\r\n        col[i].buffer_ = copyToNewBuffer(prev, len);\r\n    }\r\n    if (extraEndToken) {\r\n        col[num] = str;\r\n    }\r\n}\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/cpputest/cpputest/issues/comments/110252772","html_url":"https://github.com/cpputest/cpputest/issues/665#issuecomment-110252772","issue_url":"https://api.github.com/repos/cpputest/cpputest/issues/665","id":110252772,"user":{"login":"sbellus","id":436473,"avatar_url":"https://avatars.githubusercontent.com/u/436473?v=3","gravatar_id":"","url":"https://api.github.com/users/sbellus","html_url":"https://github.com/sbellus","followers_url":"https://api.github.com/users/sbellus/followers","following_url":"https://api.github.com/users/sbellus/following{/other_user}","gists_url":"https://api.github.com/users/sbellus/gists{/gist_id}","starred_url":"https://api.github.com/users/sbellus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sbellus/subscriptions","organizations_url":"https://api.github.com/users/sbellus/orgs","repos_url":"https://api.github.com/users/sbellus/repos","events_url":"https://api.github.com/users/sbellus/events{/privacy}","received_events_url":"https://api.github.com/users/sbellus/received_events","type":"User","site_admin":false},"created_at":"2015-06-09T06:53:47Z","updated_at":"2015-06-09T06:53:47Z","body":"I guess your fix `col[i] = copyToNewBuffer(prev, len);` will not remove memory leak. But may be I got it wrong.\r\n\r\nFunction `col[i] = copyToNewBuffer(prev, len);` allocates memory and returns pointer to allocated memory. Assignment operator `SimpleString& operator=(const SimpleString& other);` will be called. Temporary object `SimpleString` is created from return value of function `copyToNewBuffer(prev, len)`. Temporary object allocates and copy string from return value of function `copyToNewBuffer(prev, len)`. This newly allocated string is then copied to `col[i]` (old `col[i]` is deleted and new one is allocated), temporary object is deleted.\r\nPointer from allocation in function `copyToNewBuffer(prev, len);` in expresion `col[i] = copyToNewBuffer(prev, len);` is definitely lost.\r\n\r\n\r\n"}}