<legend id='Yc4DS'><style id='Yc4DS'><dir id='Yc4DS'><q id='Yc4DS'></q></dir></style></legend>

  • <small id='Yc4DS'></small><noframes id='Yc4DS'>

    <i id='Yc4DS'><tr id='Yc4DS'><dt id='Yc4DS'><q id='Yc4DS'><span id='Yc4DS'><b id='Yc4DS'><form id='Yc4DS'><ins id='Yc4DS'></ins><ul id='Yc4DS'></ul><sub id='Yc4DS'></sub></form><legend id='Yc4DS'></legend><bdo id='Yc4DS'><pre id='Yc4DS'><center id='Yc4DS'></center></pre></bdo></b><th id='Yc4DS'></th></span></q></dt></tr></i><div id='Yc4DS'><tfoot id='Yc4DS'></tfoot><dl id='Yc4DS'><fieldset id='Yc4DS'></fieldset></dl></div>

      <tfoot id='Yc4DS'></tfoot>
        <bdo id='Yc4DS'></bdo><ul id='Yc4DS'></ul>

        为什么这个程序会崩溃:在 DLL 之间传递 std::string

        Why does this program crash: passing of std::string between DLLs(为什么这个程序会崩溃:在 DLL 之间传递 std::string)
          <i id='b3uBo'><tr id='b3uBo'><dt id='b3uBo'><q id='b3uBo'><span id='b3uBo'><b id='b3uBo'><form id='b3uBo'><ins id='b3uBo'></ins><ul id='b3uBo'></ul><sub id='b3uBo'></sub></form><legend id='b3uBo'></legend><bdo id='b3uBo'><pre id='b3uBo'><center id='b3uBo'></center></pre></bdo></b><th id='b3uBo'></th></span></q></dt></tr></i><div id='b3uBo'><tfoot id='b3uBo'></tfoot><dl id='b3uBo'><fieldset id='b3uBo'></fieldset></dl></div>
            <tbody id='b3uBo'></tbody>

          <tfoot id='b3uBo'></tfoot>
          • <bdo id='b3uBo'></bdo><ul id='b3uBo'></ul>

              1. <small id='b3uBo'></small><noframes id='b3uBo'>

                <legend id='b3uBo'><style id='b3uBo'><dir id='b3uBo'><q id='b3uBo'></q></dir></style></legend>
                • 本文介绍了为什么这个程序会崩溃:在 DLL 之间传递 std::string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我无法弄清楚以下崩溃的原因 (MSVC9):

                  I have some trouble figuring out why the following crashes (MSVC9):

                  //// the following compiles to A.dll with release runtime linked dynamically
                  //A.h
                  class A {
                    __declspec(dllexport) std::string getString();
                  };
                  //A.cpp
                  #include "A.h"
                  std::string A::getString() {
                     return "I am a string.";
                  }
                  
                  //// the following compiles to main.exe with debug runtime linked dynamically
                  #include "A.h"
                  int main() {
                     A a;
                     std::string s = a.getString();
                     return 0;
                  } // crash on exit
                  

                  显然 (?) 这是由于可执行文件和 DLL 的内存模型不同.会不会是A::getString() 返回的字符串在A.dll 中分配,在main.exe 中释放?

                  Obviously (?) this is due to the different memory models for the executable and DLL. Could it be that the string A::getString() returns is being allocated in A.dll and freed in main.exe?

                  如果是这样,为什么 - 以及在 DLL(或可执行文件,就此而言)之间传递字符串的安全方法是什么?不使用带有自定义删除器的 shared_ptr 之类的包装器.

                  If so, why - and what would be a safe way to pass strings between DLLs (or executables, for that matter)? Without using wrappers like shared_ptr with a custom deleter.

                  推荐答案

                  这实际上并不是由不同的堆实现引起的 - MSVC std::string 实现不会为这么小的字符串使用动态分配的内存(它使用小字符串优化).CRT 确实需要匹配,但这不是你这次的问题.

                  发生的情况是您违反了一个定义规则,从而调用了未定义的行为.

                  What's happening is that you're invoking undefined behaviour by violating the One Definition Rule.

                  发布和调试版本将设置不同的预处理器标志,您会发现 std::string 在每种情况下都有不同的定义.询问您的编译器 sizeof(std::string) 是什么 - MSVC10 告诉我它在调试版本中为 32,在发布版本中为 28(这不是填充 - 28 和 32 都是 4 个字节`边界).

                  The release and debug builds will have different preprocessor flags set, and you'll find that std::string has a different definition in each case. Ask your compiler what sizeof(std::string) is - MSVC10 tells me that it's 32 in a debug build and 28 in a release build (this isn't padding - 28 and 32 are both 4 bytes` boundaries).

                  发生什么事了?变量 s 使用复制构造函数的调试版本进行初始化,以复制 std::string 的发布版本.不同版本的成员变量的偏移量不同,所以你复制垃圾.MSVC 实现有效地存储了开始和结束指针 - 您已将垃圾复制到它们中;因为它们不再是 null,析构函数试图释放它们,你会得到访问冲突.

                  So what's happening? Variable s is initialized using the debug version of the copy constructor to copy a release version of std::string. The offsets of the member variables are different between the versions, so you copy garbage. The MSVC implementation effectively stores begin and end pointers - you've copied garbage into them; because they're no longer null, the destructor tries to free them and you get an access violation.

                  即使堆实现相同,它也会崩溃,因为您正在释放指向从未分配过的内存的垃圾指针.

                  Even if the heap implementations were the same it would crash, as you're freeing garbage pointers to memory that was never allocated in the first place.

                  总而言之:CRT 版本需要匹配,但定义也需要匹配 - 包括标准库中的定义.

                  In summary: the CRT versions need to match but so do the definitions - including the definitions in the standard library.

                  这篇关于为什么这个程序会崩溃:在 DLL 之间传递 std::string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

                  <i id='dd9uU'><tr id='dd9uU'><dt id='dd9uU'><q id='dd9uU'><span id='dd9uU'><b id='dd9uU'><form id='dd9uU'><ins id='dd9uU'></ins><ul id='dd9uU'></ul><sub id='dd9uU'></sub></form><legend id='dd9uU'></legend><bdo id='dd9uU'><pre id='dd9uU'><center id='dd9uU'></center></pre></bdo></b><th id='dd9uU'></th></span></q></dt></tr></i><div id='dd9uU'><tfoot id='dd9uU'></tfoot><dl id='dd9uU'><fieldset id='dd9uU'></fieldset></dl></div>
                    <tbody id='dd9uU'></tbody>

                        <bdo id='dd9uU'></bdo><ul id='dd9uU'></ul>
                      • <small id='dd9uU'></small><noframes id='dd9uU'>

                          <legend id='dd9uU'><style id='dd9uU'><dir id='dd9uU'><q id='dd9uU'></q></dir></style></legend>
                          <tfoot id='dd9uU'></tfoot>