Skip to content

Commit

Permalink
Site updated: 2024-01-11 16:12:00
Browse files Browse the repository at this point in the history
  • Loading branch information
“singheart” committed Jan 11, 2024
1 parent c0bc0dc commit d2b6ce0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
10 changes: 5 additions & 5 deletions 2024/01/11/shared-ptr内存泄漏/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.11.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果:${query}","hits_time":"找到 ${hits} 个搜索结果(用时 ${time} 毫秒)","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

<meta name="description" content="0x00 背景 在写CMU15445project 0时,报了一大堆内存泄漏的错误。经过排查,发现是shared_ptr互相引用导致的,所以这里进行一个简单的说明。 假设有如下的代码: 123456789101112131415161718192021222324252627282930313233#include &lt;iostream&gt;#include &lt;memory&gt;cl">
<meta name="description" content="0x00 背景 在写CMU15445project 0时,报了一大堆内存泄漏的错误。经过排查,发现是shared_ptr互相引用导致的,所以这里进行一个简单的说明。">
<meta property="og:type" content="article">
<meta property="og:title" content="shared_ptr内存泄漏">
<meta property="og:url" content="http://example.com/2024/01/11/shared-ptr%E5%86%85%E5%AD%98%E6%B3%84%E6%BC%8F/index.html">
<meta property="og:site_name" content="singheart&#39;s blog">
<meta property="og:description" content="0x00 背景 在写CMU15445project 0时,报了一大堆内存泄漏的错误。经过排查,发现是shared_ptr互相引用导致的,所以这里进行一个简单的说明。 假设有如下的代码: 123456789101112131415161718192021222324252627282930313233#include &lt;iostream&gt;#include &lt;memory&gt;cl">
<meta property="og:description" content="0x00 背景 在写CMU15445project 0时,报了一大堆内存泄漏的错误。经过排查,发现是shared_ptr互相引用导致的,所以这里进行一个简单的说明。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-01-11T08:10:39.000Z">
<meta property="article:modified_time" content="2024-01-11T08:11:21.340Z">
<meta property="article:modified_time" content="2024-01-11T08:11:50.672Z">
<meta property="article:author" content="singheart">
<meta property="article:tag" content="C++">
<meta name="twitter:card" content="summary">
Expand Down Expand Up @@ -244,7 +244,7 @@ <h1 class="post-title" itemprop="name headline">
<span class="post-meta-item-text">发表于</span>


<time title="创建时间:2024-01-11 16:10:39 / 修改时间:16:11:21" itemprop="dateCreated datePublished" datetime="2024-01-11T16:10:39+08:00">2024-01-11</time>
<time title="创建时间:2024-01-11 16:10:39 / 修改时间:16:11:50" itemprop="dateCreated datePublished" datetime="2024-01-11T16:10:39+08:00">2024-01-11</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
Expand Down Expand Up @@ -274,7 +274,7 @@ <h1 class="post-title" itemprop="name headline">

<div class="post-body" itemprop="articleBody">
<h1 id="x00-背景">0x00 背景</h1>
<p>在写<code>CMU15445</code>project 0时,报了一大堆内存泄漏的错误。经过排查,发现是<code>shared_ptr</code>互相引用导致的,所以这里进行一个简单的说明。</p>
<p>在写<code>CMU15445</code>project 0时,报了一大堆内存泄漏的错误。经过排查,发现是<code>shared_ptr</code>互相引用导致的,所以这里进行一个简单的说明。 <span id="more"></span></p>
<p>假设有如下的代码:</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;memory&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">B</span>;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">A</span> &#123;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">A</span>() &#123;</span><br><span class="line"> std::cout &lt;&lt; <span class="string">&quot;A()\n&quot;</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> ~<span class="built_in">A</span>() &#123;</span><br><span class="line"> std::cout &lt;&lt; <span class="string">&quot;~A()\n&quot;</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> std::shared_ptr&lt;B&gt; p;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">B</span> &#123;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">B</span>() &#123;</span><br><span class="line"> std::cout &lt;&lt; <span class="string">&quot;B()\n&quot;</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> ~<span class="built_in">B</span>() &#123;</span><br><span class="line"> std::cout &lt;&lt; <span class="string">&quot;~B()\n&quot;</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> std::shared_ptr&lt;A&gt; p;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line"> <span class="keyword">auto</span> pa = std::<span class="built_in">make_shared</span>&lt;A&gt;();</span><br><span class="line"> <span class="keyword">auto</span> pb = std::<span class="built_in">make_shared</span>&lt;B&gt;();</span><br><span class="line"> pa-&gt;p = pb;</span><br><span class="line"> pb-&gt;p = pa;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由于pa和pb相互引用,当离开作用域的时候pa和pb所管理的资源都不能被释放。</p>
Expand Down
16 changes: 10 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ <h2 class="post-title" itemprop="name headline">
<span class="post-meta-item-text">发表于</span>


<time title="创建时间:2024-01-11 16:10:39 / 修改时间:16:11:21" itemprop="dateCreated datePublished" datetime="2024-01-11T16:10:39+08:00">2024-01-11</time>
<time title="创建时间:2024-01-11 16:10:39 / 修改时间:16:11:50" itemprop="dateCreated datePublished" datetime="2024-01-11T16:10:39+08:00">2024-01-11</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
Expand All @@ -372,11 +372,15 @@ <h2 class="post-title" itemprop="name headline">

<div class="post-body" itemprop="articleBody">
<h1 id="x00-背景">0x00 背景</h1>
<p>在写<code>CMU15445</code>project 0时,报了一大堆内存泄漏的错误。经过排查,发现是<code>shared_ptr</code>互相引用导致的,所以这里进行一个简单的说明。</p>
<p>假设有如下的代码:</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;memory&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">B</span>;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">A</span> &#123;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">A</span>() &#123;</span><br><span class="line"> std::cout &lt;&lt; <span class="string">&quot;A()\n&quot;</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> ~<span class="built_in">A</span>() &#123;</span><br><span class="line"> std::cout &lt;&lt; <span class="string">&quot;~A()\n&quot;</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> std::shared_ptr&lt;B&gt; p;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">B</span> &#123;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">B</span>() &#123;</span><br><span class="line"> std::cout &lt;&lt; <span class="string">&quot;B()\n&quot;</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> ~<span class="built_in">B</span>() &#123;</span><br><span class="line"> std::cout &lt;&lt; <span class="string">&quot;~B()\n&quot;</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> std::shared_ptr&lt;A&gt; p;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line"> <span class="keyword">auto</span> pa = std::<span class="built_in">make_shared</span>&lt;A&gt;();</span><br><span class="line"> <span class="keyword">auto</span> pb = std::<span class="built_in">make_shared</span>&lt;B&gt;();</span><br><span class="line"> pa-&gt;p = pb;</span><br><span class="line"> pb-&gt;p = pa;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由于pa和pb相互引用,当离开作用域的时候pa和pb所管理的资源都不能被释放。</p>

<p>在写<code>CMU15445</code>project 0时,报了一大堆内存泄漏的错误。经过排查,发现是<code>shared_ptr</code>互相引用导致的,所以这里进行一个简单的说明。
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2024/01/11/shared-ptr%E5%86%85%E5%AD%98%E6%B3%84%E6%BC%8F/#more" rel="contents">
阅读全文 &raquo;
</a>
</div>
<!--/noindex-->


</div>

Expand Down
2 changes: 1 addition & 1 deletion search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4592,7 +4592,7 @@ tips:--prefix选项指定安装的路径</p>
<title>shared_ptr内存泄漏</title>
<url>/2024/01/11/shared-ptr%E5%86%85%E5%AD%98%E6%B3%84%E6%BC%8F/</url>
<content><![CDATA[<h1 id="x00-背景">0x00 背景</h1>
<p>在写<code>CMU15445</code>project 0时,报了一大堆内存泄漏的错误。经过排查,发现是<code>shared_ptr</code>互相引用导致的,所以这里进行一个简单的说明。</p>
<p>在写<code>CMU15445</code>project 0时,报了一大堆内存泄漏的错误。经过排查,发现是<code>shared_ptr</code>互相引用导致的,所以这里进行一个简单的说明。 <span id="more"></span></p>
<p>假设有如下的代码:</p>
<figure class="highlight c++"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;memory&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">B</span>;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">A</span> &#123;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">A</span>() &#123;</span><br><span class="line"> std::cout &lt;&lt; <span class="string">&quot;A()\n&quot;</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> ~<span class="built_in">A</span>() &#123;</span><br><span class="line"> std::cout &lt;&lt; <span class="string">&quot;~A()\n&quot;</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> std::shared_ptr&lt;B&gt; p;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">B</span> &#123;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line"> <span class="built_in">B</span>() &#123;</span><br><span class="line"> std::cout &lt;&lt; <span class="string">&quot;B()\n&quot;</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> ~<span class="built_in">B</span>() &#123;</span><br><span class="line"> std::cout &lt;&lt; <span class="string">&quot;~B()\n&quot;</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> std::shared_ptr&lt;A&gt; p;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line"> <span class="keyword">auto</span> pa = std::<span class="built_in">make_shared</span>&lt;A&gt;();</span><br><span class="line"> <span class="keyword">auto</span> pb = std::<span class="built_in">make_shared</span>&lt;B&gt;();</span><br><span class="line"> pa-&gt;p = pb;</span><br><span class="line"> pb-&gt;p = pa;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由于pa和pb相互引用,当离开作用域的时候pa和pb所管理的资源都不能被释放。</p>
Expand Down

0 comments on commit d2b6ce0

Please sign in to comment.