<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	
	>
<channel>
	<title>
	Comments on: Overview of std::map’s Insertion / Emplacement Methods in C++17	</title>
	<atom:link href="https://www.fluentcpp.com/2018/12/11/overview-of-std-map-insertion-emplacement-methods-in-cpp17/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2018/12/11/overview-of-std-map-insertion-emplacement-methods-in-cpp17/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Wed, 19 Dec 2018 14:00:00 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>
	<item>
		<title>
		By: Walletfox		</title>
		<link>https://www.fluentcpp.com/2018/12/11/overview-of-std-map-insertion-emplacement-methods-in-cpp17/#comment-1543</link>

		<dc:creator><![CDATA[Walletfox]]></dc:creator>
		<pubDate>Wed, 19 Dec 2018 14:00:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4520#comment-1543</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/12/11/overview-of-std-map-insertion-emplacement-methods-in-cpp17/#comment-1540&quot;&gt;Karol Gaida&lt;/a&gt;.

Thanks for the comment! Your example with new is valid and until recently I also thought that was the only way to cause a memory leak with emplace(). Until I read the following SO thread https://stackoverflow.com/questions/37060326/at-which-point-does-mapemplace-create-an-object, where T.C. explains that this could occur even with std::make_unique() due to underspecfied emplace(). Tthis appears to be the case when you are trying to emplace() an object where the key is already present.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/12/11/overview-of-std-map-insertion-emplacement-methods-in-cpp17/#comment-1540">Karol Gaida</a>.</p>
<p>Thanks for the comment! Your example with new is valid and until recently I also thought that was the only way to cause a memory leak with emplace(). Until I read the following SO thread <a href="https://stackoverflow.com/questions/37060326/at-which-point-does-mapemplace-create-an-object" rel="nofollow ugc">https://stackoverflow.com/questions/37060326/at-which-point-does-mapemplace-create-an-object</a>, where T.C. explains that this could occur even with std::make_unique() due to underspecfied emplace(). Tthis appears to be the case when you are trying to emplace() an object where the key is already present.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Karol Gaida		</title>
		<link>https://www.fluentcpp.com/2018/12/11/overview-of-std-map-insertion-emplacement-methods-in-cpp17/#comment-1540</link>

		<dc:creator><![CDATA[Karol Gaida]]></dc:creator>
		<pubDate>Tue, 18 Dec 2018 16:08:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4520#comment-1540</guid>

					<description><![CDATA[At &lt;a href=&quot;https://www.fluentcpp.com/2018/12/11/overview-of-std-map-insertion-emplacement-methods-in-cpp17#crayon-5c188d3437d1b091106798&quot;&gt;https://www.fluentcpp.com/2018/12/11/overview-of-std-map-insertion-emplacement-methods-in-cpp17#crayon-5c188d3437d1b091106798&lt;/a&gt; you described that using emplace() could lead to memory leaks in some cases but could you describe further why it may occur in the example you showed us?

Following the Clang-Tidy documentation as well as Meyers&#039;s book, the memory leak could occur when a temporary object A is created but emplace() could not allocate space for a new item. We do not possess a pointer to the temporary object A so we could not destroy the object A after it is passed to emplace() function and the emplace() fails. The memory leak could occur in such example:

&lt;code&gt;std::map&#060;int, std::unique_ptr&lt;A&gt;&#062; m;
m.emplace(1, new(&quot;Ann&quot;,63));&lt;/code&gt;

Regarding the example code you showed us, I think there will be no memory leak at all because the temporary object is now an std::unique_ptr which has the capability of destroying itself when the program goes out of the scope. So even if the object A is successfully created, stored in std::unique_ptr and the emplace() fails, std::unique_ptr will destroy this object and prevent the memory leak.

Is there anything that I am missing in your example?]]></description>
			<content:encoded><![CDATA[<p>At <a href="https://www.fluentcpp.com/2018/12/11/overview-of-std-map-insertion-emplacement-methods-in-cpp17#crayon-5c188d3437d1b091106798">https://www.fluentcpp.com/2018/12/11/overview-of-std-map-insertion-emplacement-methods-in-cpp17#crayon-5c188d3437d1b091106798</a> you described that using emplace() could lead to memory leaks in some cases but could you describe further why it may occur in the example you showed us?</p>
<p>Following the Clang-Tidy documentation as well as Meyers&#8217;s book, the memory leak could occur when a temporary object A is created but emplace() could not allocate space for a new item. We do not possess a pointer to the temporary object A so we could not destroy the object A after it is passed to emplace() function and the emplace() fails. The memory leak could occur in such example:</p>
<p><code>std::map&lt;int, std::unique_ptr<a>&gt; m;<br />
m.emplace(1, new("Ann",63));</a></code></p>
<p>Regarding the example code you showed us, I think there will be no memory leak at all because the temporary object is now an std::unique_ptr which has the capability of destroying itself when the program goes out of the scope. So even if the object A is successfully created, stored in std::unique_ptr and the emplace() fails, std::unique_ptr will destroy this object and prevent the memory leak.</p>
<p>Is there anything that I am missing in your example?</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
