﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-从简单到混乱</title><link>http://www.cppblog.com/psysun/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 09 Jun 2026 17:49:11 GMT</lastBuildDate><pubDate>Tue, 09 Jun 2026 17:49:11 GMT</pubDate><ttl>60</ttl><item><title>在跨模块调用中传递stl容器的问题。</title><link>http://www.cppblog.com/psysun/archive/2005/11/24/1277.html</link><dc:creator>psysun</dc:creator><author>psysun</author><pubDate>Thu, 24 Nov 2005 02:24:00 GMT</pubDate><guid>http://www.cppblog.com/psysun/archive/2005/11/24/1277.html</guid><wfw:comment>http://www.cppblog.com/psysun/comments/1277.html</wfw:comment><comments>http://www.cppblog.com/psysun/archive/2005/11/24/1277.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/psysun/comments/commentRss/1277.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/psysun/services/trackbacks/1277.html</trackback:ping><description><![CDATA[这是在项目中碰到的。<BR>下面是代码：<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT face=Tahoma><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">de.cpp</SPAN></FONT><SPAN style="COLOR: #008000"><BR></SPAN><FONT face=Tahoma><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN></FONT><SPAN style="COLOR: #000000"><BR><FONT face=Tahoma>#include&nbsp;</FONT></SPAN><FONT face=Tahoma><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">iostream</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN></FONT><SPAN style="COLOR: #000000"><BR></SPAN><FONT face=Tahoma><SPAN style="COLOR: #0000ff">using</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">namespace</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">&nbsp;std;<BR></SPAN><SPAN style="COLOR: #0000ff">extern</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">C</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;getString(</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">str);<BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;main(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;argc,</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">**</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">argv)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">&nbsp;str;<BR>&nbsp;&nbsp;&nbsp;&nbsp;getString(str);<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;&lt;</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">&nbsp;str;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN></FONT><SPAN style="COLOR: #000000"><FONT face=Tahoma>;<BR>}</FONT></SPAN></DIV><BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><FONT face=Tahoma><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">dll.cpp</SPAN></FONT><SPAN style="COLOR: #008000"><BR></SPAN><FONT face=Tahoma><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN></FONT><SPAN style="COLOR: #000000"><BR><FONT face=Tahoma>#include&nbsp;</FONT></SPAN><FONT face=Tahoma><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">iostream</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN></FONT><SPAN style="COLOR: #000000"><BR></SPAN><FONT face=Tahoma><SPAN style="COLOR: #0000ff">using</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">namespace</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">&nbsp;std;<BR></SPAN><SPAN style="COLOR: #0000ff">extern</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">C</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;getString(</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">str)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">&nbsp;i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN></FONT><FONT face=Tahoma><SPAN style="COLOR: #000000">str&nbsp;</SPAN><SPAN style="COLOR: #000000">+=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"string modified.mybe cause a exception.</SPAN><SPAN style="COLOR: #000000">"</SPAN></FONT><SPAN style="COLOR: #000000"><FONT face=Tahoma>;<BR>}</FONT></SPAN></DIV><BR>将dll.cpp连接成dll形式，运行de.exe有时候很正常，但有时候会发现有异常出现，异常是delete释放了一个非法的指针。这取决于连接的运行库是动态的还是静态的，他会在静态连接时出问题。<BR>产生这个问题的原因是因为std::string类的内存分配器分配了内存，而在de.cpp里释放，两个模块分别有自己的运行堆，分配和释放没有在同一个堆里进行。<BR>原因很简单，但是有时后就会忽略，尤其在模块很多和频繁使用stl容器的时候，当从别人那里得到一个模块接口时，如果有一个需要stl容器引用的接口时就要注意运行库的问题。<BR><img src ="http://www.cppblog.com/psysun/aggbug/1277.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/psysun/" target="_blank">psysun</a> 2005-11-24 10:24 <a href="http://www.cppblog.com/psysun/archive/2005/11/24/1277.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>