使用MSVC 2013和2015,使用CMake和ExternalProject_Add,通过引用导出的成员函数传递st

Passing std::vector by reference to Exported Member Function using MSVC 2013 and 2015, Using CMake and ExternalProject_Add

本文关键字:使用 st 成员 引用 函数 2015 2013 MSVC CMake ExternalProject Add      更新时间:2023-10-16

我有一个在Linux和Windows上构建的动态库。在两个系统下针对静态库运行单元测试时,我没有遇到任何问题。当在linux中对我的共享库运行单元测试时,一切都很好。在Windows中针对共享库运行单元测试时,出现了问题。

总之,我的堆栈跟踪如下:
UnitTest.exe!_free_dbg_nolock(void * pUserData, int nBlockUse) Line 1424    C++
UnitTest.exe!_free_dbg(void * pUserData, int nBlockUse) Line 1265   C++
UnitTest.exe!operator delete(void * pUserData) Line 54  C++
UnitTest.exe!std::allocator<QuadKey::QuadKey>::deallocate(QuadKey::QuadKey * _Ptr, unsigned __int64 __formal) Line 574  C++
UnitTest.exe!std::_Wrap_alloc<std::allocator<QuadKey::QuadKey> >::deallocate(QuadKey::QuadKey * _Ptr, unsigned __int64 _Count) Line 859 C++
UnitTest.exe!std::vector<QuadKey::QuadKey,std::allocator<QuadKey::QuadKey> >::_Tidy() Line 1629 C++
UnitTest.exe!std::vector<QuadKey::QuadKey,std::allocator<QuadKey::QuadKey> >::~vector<QuadKey::QuadKey,std::allocator<QuadKey::QuadKey> >() Line 946    C++
UnitTest.exe!QuadKey::BINGSYSTEM_GetChildren_Test::TestBody() Line 195  C++

QuadKey::BINGSYSTEM_GetChildren_Test::TestBody()体中调用的导出成员函数接受一个数组的引用,将其传递给另一个类中的非导出函数并返回。如:

void QuadKey::getChildren(std::vector<QuadKey> &outKeys) const
{
    m_Impl->getChildren(outKeys, (*this));
}

,

// m_Impl getChildren System class is not exported.
void System::getChildren(std::vector<QuadKey> &outKeys,
    const QuadKey &self) const
{
    for (std::uint8_t quadrant = 0; quadrant < 4; ++quadrant) {
        QuadKey child =
          getChild(static_cast<QuadKey::Quadrant>(quadrant), self);
        Detail::insertVectorIfValidAndUnique(outKeys, child);
    }
}

在使用MSVC时,是否可以安全地假设这种做法不是标准的?那是在传递一个std::vector到一个导出的函数?如果不是,我该假设什么呢?当向量超出单元测试体的作用域而被释放时,就会发生断言。我试图在矢量上调用clear,但即使在空std::vector上也会发生相同的崩溃。


修改:

改变我的UnitTest和动态库使用MD (MDd调试)而不是MT (MTd调试)我得到一个错误:

Error   LNK2038 mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj  UnitTest    C:UsersmehogganDevelQuadKeysbuildUnitTestlibcpmtd.lib(StlCompareStringA.obj) 1

这是因为gtest在默认情况下是使用针对标准运行时的静态链接编译的。如果我不想改变gtest,那么在编写需要跨越dll边界的标准容器的接口时,什么将是良好的c++实践?

这是因为你静态地链接到C运行时。您需要与DLL版本链接。构建/MD,而不是/MT

请参阅跨DLL边界传递CRT对象的潜在错误,以了解可能导致的问题。