【问题标题】:Microsoft C++ exception: std::bad_array_new_length at memory location in function from DLLMicrosoft C++ 异常:来自 DLL 的函数中内存位置的 std::bad_array_new_length
【发布时间】:2021-10-02 19:41:14
【问题描述】:

我使用 Visual Studio 2019。我编写了两个测试项目:exe 和 dll。我的 dll 包括 noexported 类和导出函数:

#pragma once

#include <vector>

#ifdef DLL_EXPORT
#define DLL __declspec(dllexport)
#else
#define DLL __declspec(dllimport)
#endif


struct dll
{
public:
    std::vector<int> a;
    std::vector<int> b;
    std::vector<int> c;
    int d;

};
extern "C" DLL dll * _data1();

及实现功能:

#include "dll.h"

dll* _data1()
{
    dll* a = new dll;
    return a;
}

然后,我的exe包含main.cpp,调用函数_data1()

#include "../Dll1/dll.h"
#include <windows.h>

#pragma comment(lib, "../Project1/Debug/Dll1.lib")

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int    nCmdShow)
{
    dll* b = _data1();
    b->a.push_back(1);

    return 0;
}

此代码在行 b-&gt;a.push_back(1); 上损坏。我调试了我的代码,并了解到该函数 _data1() 返回正确的值,但在函数 WinMain 中的值已经损坏。 另外,在我的 exe 中为运行时库设置标志 /MD 并删除预处理器定义 _DEBUG。这是一个强制性条件。

在我的 DLL 中,我定义了 DLL_EXPORT。

当前配置:调试 Win32

为什么会这样?

UPD1:

XML 配置。执行:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <ItemGroup>
    <ClCompile Include="main.cpp" />
  </ItemGroup>
  <PropertyGroup Label="Globals">
    <VCProjectVersion>16.0</VCProjectVersion>
    <Keyword>Win32Proj</Keyword>
    <ProjectGuid>{bc6cc284-56fc-4b4a-8d42-9d15887c7155}</ProjectGuid>
    <RootNamespace>ConsoleApplication5</RootNamespace>
    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
    <ProjectName>TestApp</ProjectName>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    <ConfigurationType>Application</ConfigurationType>
    <UseDebugLibraries>true</UseDebugLibraries>
    <PlatformToolset>v142</PlatformToolset>
    <CharacterSet>Unicode</CharacterSet>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ImportGroup Label="ExtensionSettings">
  </ImportGroup>
  <ImportGroup Label="Shared">
  </ImportGroup>
  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <LinkIncremental>true</LinkIncremental>
  </PropertyGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <ClCompile>
      <WarningLevel>Level3</WarningLevel>
      <SDLCheck>true</SDLCheck>
      <PreprocessorDefinitions>WIN32;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <ConformanceMode>true</ConformanceMode>
      <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
      <FloatingPointModel>Fast</FloatingPointModel>
      <RuntimeTypeInfo>true</RuntimeTypeInfo>
    </ClCompile>
    <Link>
      <SubSystem>Windows</SubSystem>
      <GenerateDebugInformation>true</GenerateDebugInformation>
    </Link>
  </ItemDefinitionGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
  </ImportGroup>
</Project>

dll:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <ItemGroup>
    <ClInclude Include="dll.h" />
  </ItemGroup>
  <ItemGroup>
    <ClCompile Include="dll.cpp" />
  </ItemGroup>
  <PropertyGroup Label="Globals">
    <VCProjectVersion>16.0</VCProjectVersion>
    <Keyword>Win32Proj</Keyword>
    <ProjectGuid>{328cdf3e-630e-46c0-9be9-9b838a13873f}</ProjectGuid>
    <RootNamespace>Dll1</RootNamespace>
    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
    <ProjectName>Dll</ProjectName>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    <ConfigurationType>DynamicLibrary</ConfigurationType>
    <UseDebugLibraries>true</UseDebugLibraries>
    <PlatformToolset>v142</PlatformToolset>
    <CharacterSet>Unicode</CharacterSet>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ImportGroup Label="ExtensionSettings">
  </ImportGroup>
  <ImportGroup Label="Shared">
  </ImportGroup>
  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <LinkIncremental>true</LinkIncremental>
  </PropertyGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <ClCompile>
      <WarningLevel>Level3</WarningLevel>
      <SDLCheck>true</SDLCheck>
      <PreprocessorDefinitions>DLL_EXPORT;WIN32;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <ConformanceMode>true</ConformanceMode>
      <PrecompiledHeader>NotUsing</PrecompiledHeader>
      <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
    </ClCompile>
    <Link>
      <SubSystem>Windows</SubSystem>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <EnableUAC>false</EnableUAC>
    </Link>
  </ItemDefinitionGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
  </ImportGroup>
</Project>

【问题讨论】:

  • 如何构建和链接 dll 和 exe?编译器和链接器标志应包含在问题中。 DLL_EXPORT 好像没有在 dll 项目中定义。
  • @S.M.在我的 DLL 中,我定义了 DLL_EXPORT。
  • 看起来你正在混合调试和发布。 std::collections 在 MSVC 中具有不同的内存布局,具体取决于用于调试和发布的#defines。确保两个项目使用相同的构建类型和运行时支持。

标签: c++ visual-studio dll


【解决方案1】:

问题在于使用的运行时库之间的差异: DLL 使用调试 DLL 运行时 (/MTd),而应用程序使用发布 EXE 运行时 (/MD)。您应该将应用程序切换到调试 (/MTd)。 内存管理仅适用于一致的库。

【讨论】:

  • 请参阅comments 上的指南。我建议通过用好的答案回答好的问题来提高代表。发布更适合作为 cmets 的低质量答案会使您很容易被否决,这会花费代表并使您远离获得足够代表的目标
  • @camille,作为一个新用户,我的信誉积分为 0,所以我唯一可以回复的方法是添加我回答代码正确并要求提供非常具体的附加信息的答案已由问题的作者提供。根据更新的输入,我提供了有帮助的解决方案。所以,最后这奏效了,我没有看到任何问题。无论如何,点了。根据 Range 的新输入,答案已更新;现在它应该工作了。人们有不同的问题,所以问题和答案可能会有所不同。
猜你喜欢
  • 2013-08-03
  • 1970-01-01
  • 2013-12-20
  • 2017-08-03
  • 2011-10-20
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多