• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Missuses</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../boost_dll.html" title="Chapter 14. Boost.DLL">
10<link rel="prev" href="mangled_import.html" title="Mangled Import">
11<link rel="next" href="reference.html" title="Reference">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="mangled_import.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_dll.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="boost_dll.missuses"></a><a class="link" href="missuses.html" title="Missuses">Missuses</a>
29</h2></div></div></div>
30<div class="warning"><table border="0" summary="Warning">
31<tr>
32<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../doc/src/images/warning.png"></td>
33<th align="left">Warning</th>
34</tr>
35<tr><td align="left" valign="top"><p>
36        Typical errors and missuses are located in this section. Please read carefully,
37        this will save a lot of debugging time!
38      </p></td></tr>
39</table></div>
40<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
41<li class="listitem">
42          <span class="bold"><strong>Issue:</strong></span> Program crashes on <code class="computeroutput"><span class="keyword">delete</span></code> or <code class="computeroutput"><span class="identifier">free</span><span class="special">()</span></code>.
43        </li>
44<li class="listitem">
45          <span class="bold"><strong>Fix:</strong></span> Your plugin and program must use
46          the same Standard C++ and C libraries, both must be linked dynamically.
47          For Visual Studio use <a href="https://msdn.microsoft.com/library/2kzt1wy3%28v=vs.110%29.aspx" target="_top">/MD
48          or /MDd compiler switches</a>.
49        </li>
50</ul></div>
51<pre class="programlisting"></pre>
52<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
53<li class="listitem">
54          <span class="bold"><strong>Issue:</strong></span> Program crashes in <code class="computeroutput"><span class="keyword">catch</span><span class="special">(...)</span></code>
55          block.
56        </li>
57<li class="listitem">
58          <span class="bold"><strong>Example:</strong></span>
59<pre class="programlisting"><span class="keyword">try</span> <span class="special">{</span>
60    <span class="keyword">auto</span> <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">dll</span><span class="special">::</span><span class="identifier">import</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">()&gt;(</span><span class="identifier">path_to_pugin</span><span class="special">,</span> <span class="string">"function"</span><span class="special">);</span>
61    <span class="identifier">f</span><span class="special">();</span>
62    <span class="comment">// `f` goes out of scope</span>
63<span class="special">}</span> <span class="keyword">catch</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span> <span class="special">{</span>
64    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cerr</span> <span class="special">&lt;&lt;</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">();</span>
65<span class="special">}</span>
66</pre>
67        </li>
68<li class="listitem">
69          <span class="bold"><strong>Fix:</strong></span> Exception was generated inside the
70          plugin, so it refers to the exception code in plugin. When <code class="computeroutput"><span class="identifier">f</span></code> goes out of scope, the plugin gets
71          unloaded and the reference to the exception code is broken. Any attempt
72          to use the exception variable may use the dangling reference, leading to
73          a segmentation fault. Fix your code:
74<pre class="programlisting"><span class="keyword">auto</span> <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">dll</span><span class="special">::</span><span class="identifier">import</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">()&gt;(</span><span class="identifier">path_to_pugin</span><span class="special">,</span> <span class="string">"function"</span><span class="special">);</span>
75<span class="keyword">try</span> <span class="special">{</span>
76    <span class="identifier">f</span><span class="special">();</span>
77    <span class="comment">// `f` goes out of scope</span>
78<span class="special">}</span> <span class="keyword">catch</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span> <span class="special">{</span>
79    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cerr</span> <span class="special">&lt;&lt;</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">();</span>
80<span class="special">}</span>
81</pre>
82        </li>
83</ul></div>
84<pre class="programlisting"></pre>
85<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
86<li class="listitem">
87          <span class="bold"><strong>Issue:</strong></span> Thread local storage seems to be
88          corrupted.
89        </li>
90<li class="listitem">
91          <span class="bold"><strong>Fix:</strong></span> Some platforms have no out-of-the-box
92          support for plugins that use TLS, for example <a href="https://support.microsoft.com/en-us/kb/118816" target="_top">Windows</a>.
93          Use platform specific workarounds or just do not use TLS in plugins.
94        </li>
95</ul></div>
96<pre class="programlisting"></pre>
97<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
98<li class="listitem">
99          <span class="bold"><strong>Issue:</strong></span> Attempt to call a loaded function
100          crashes or function returns incorrect result.
101        </li>
102<li class="listitem">
103          <span class="bold"><strong>Fix:</strong></span> Boost.DLL does not guarantee ABI
104          stability. If you compile plugin and program with different compilers or
105          different compiler switches the function ABI may change and you'll end
106          up with incorrect code.
107        </li>
108</ul></div>
109<pre class="programlisting"></pre>
110<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
111<li class="listitem">
112          <span class="bold"><strong>Issue:</strong></span> Program crashes after plugin unload.
113        </li>
114<li class="listitem">
115          <span class="bold"><strong>Example:</strong></span>
116<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">foo</span><span class="special">()</span> <span class="special">{</span>
117    <span class="identifier">shared_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">p</span><span class="special">;</span>
118    <span class="keyword">try</span> <span class="special">{</span>
119        <span class="keyword">auto</span> <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">dll</span><span class="special">::</span><span class="identifier">import</span><span class="special">&lt;</span><span class="identifier">shared_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;()&gt;(</span><span class="identifier">path_to_pugin</span><span class="special">,</span> <span class="string">"function"</span><span class="special">);</span>
120        <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">f</span><span class="special">();</span>
121        <span class="comment">// `f` goes out of scope</span>
122    <span class="special">}</span> <span class="keyword">catch</span> <span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span> <span class="special">{</span>
123        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cerr</span> <span class="special">&lt;&lt;</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">();</span>
124    <span class="special">}</span>
125    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="special">*</span><span class="identifier">p</span><span class="special">;</span>
126    <span class="comment">// crashes here</span>
127<span class="special">}</span>
128</pre>
129        </li>
130<li class="listitem">
131          <span class="bold"><strong>Fix:</strong></span> In that particular example the problem
132          is within <code class="computeroutput"><span class="identifier">shared_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span></code>.
133          It keeps a type erased deleter, code for that deleter is located in plugin.
134          On destruction of <code class="computeroutput"><span class="identifier">p</span></code>, <code class="computeroutput"><span class="identifier">shared_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span></code>
135          attempts to call that deleter, however the plugin was already unloaded
136          and the code for deleter is not available any more.
137        </li>
138<li class="listitem">
139          <span class="bold"><strong>Rule of thumb:</strong></span>: if your plugin method
140          returns any C++ class variable, then make sure that the plugin is loaded
141          until that variable and any of its copies are in scope.
142        </li>
143<li class="listitem">
144          <span class="bold"><strong>Typical classes that cause such errors are:</strong></span>
145          <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; ">
146<li class="listitem">
147                <code class="computeroutput"><span class="identifier">any</span></code>
148              </li>
149<li class="listitem">
150                <code class="computeroutput"><span class="identifier">function</span></code>
151              </li>
152<li class="listitem">
153                <code class="computeroutput"><span class="identifier">shared_ptr</span></code>
154              </li>
155<li class="listitem">
156                any container with polymorphic allocator
157              </li>
158<li class="listitem">
159                <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">type_index</span></code>
160              </li>
161<li class="listitem">
162                <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">type_info</span></code>
163              </li>
164<li class="listitem">
165                <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception_ptr</span></code>
166              </li>
167<li class="listitem">
168                <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span><span class="special">&lt;</span><span class="identifier">Base</span><span class="special">&gt;</span></code>
169                holding a <code class="computeroutput"><span class="identifier">Derived</span></code>
170                type from plugin
171              </li>
172<li class="listitem">
173                exception classes thrown from plugin
174              </li>
175</ul></div>
176        </li>
177</ul></div>
178<pre class="programlisting"></pre>
179</div>
180<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
181<td align="left"></td>
182<td align="right"><div class="copyright-footer">Copyright © 2014 Renato Tegon Forti, Antony Polukhin<br>Copyright © 2015 Antony Polukhin<br>Copyright © 2016 Antony Polukhin, Klemens Morgenstern<br>Copyright © 2017-2019 Antony Polukhin<p>
183        Distributed under the Boost Software License, Version 1.0. (See accompanying
184        file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
185      </p>
186</div></td>
187</tr></table>
188<hr>
189<div class="spirit-nav">
190<a accesskey="p" href="mangled_import.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_dll.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
191</div>
192</body>
193</html>
194