<html>
<head>
<base href="http://llvm.org/bugs/" />
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW --- - basic_string leaks memory when move-constructed with unequal allocator"
href="http://llvm.org/bugs/show_bug.cgi?id=20335">20335</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>basic_string leaks memory when move-constructed with unequal allocator
</td>
</tr>
<tr>
<th>Product</th>
<td>libc++
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</td>
</tr>
<tr>
<th>Hardware</th>
<td>All
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>All Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>tkoeppe@google.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvmbugs@cs.uiuc.edu, mclow.lists@gmail.com
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=12780" name="attach_12780" title="Demonstrates memory leak in std::basic_string">attachment 12780</a> <a href="attachment.cgi?id=12780&action=edit" title="Demonstrates memory leak in std::basic_string">[details]</a></span>
Demonstrates memory leak in std::basic_string
I believe there's a memory leak in the current (213208) libc++ implementation
of basic_string when a string is move-constructed from another string but with
an unequal allocator. Briefly, here's how to produce it:
using astring = std::basic_string<char, std::char_traits<char>,
StatefulAllocator<T>>;
int main()
{
StatefulAllocator<void> a(STATE1), b(STATE2); // assume a != b
astring s("looooooooooooooooooooooooooooooong", a); // no SSO
astring t(std::move(s), b); // leak: data of s is not released
}
I'm attaching an example (run it through valgrind to see the leak).
I believe that the problem lies in "string:2139":
if (__a == __str.__alloc() || !__str.__is_long())
__r_.first().__r = __str.__r_.first().__r;
else
__init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()),
__str.__get_long_size());
__str.__zero();
If the allocators are not equal, i.e. the first branch isn't taken; then it
seems that __init() will copy the string data with the new allocator, but never
release the old memory. The pointer to the old memory is wiped immediately by
__str.__zero();.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>