Delphi库内存管理器异常

개발팡*_*발팡야 1 c++ delphi dll

我收到了从Delphi编译的DLL错误,该错误是在多线程中运行的c ++中使用

Delphi(Architect 10.3版本26.0.32429.4364)库代码

library Project1;

uses
  System.SysUtils,
  System.Classes;

{$R *.res}

procedure __test(size: integer); cdecl;
var
  data : AnsiString;
begin
  SetLength(data, size);
end;

exports
  __test;

begin
end.
Run Code Online (Sandbox Code Playgroud)

C ++(Viausl Studio 2019)加载库并使用多线程

// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <Windows.h>
#include <thread>

typedef void(__cdecl *functest)(int);

HINSTANCE hInst = nullptr;

functest test;

void thread_loop() {
    while (1) {
        test(10);
    }
}

int main()
{
    hInst = LoadLibraryA("Project1.dll");

    if (!hInst) {
        return 0;
    }

    test = (functest)GetProcAddress(hInst, "__test");

    if (!test) {
        return 0;
    }

    std::thread t1(thread_loop);
    std::thread t2(thread_loop);

    return 1;
Run Code Online (Sandbox Code Playgroud)

我遇到了一个异常,但它不应获取任何异常,因为这是未共享的过程变量

Ond*_*lle 5

在Delphi DLL的主块中设置IsMultiThreadTrue。默认情况下,Delphi的默认内存管理器假定为单线程模式。

begin
  IsMultiThread := True;
end.
Run Code Online (Sandbox Code Playgroud)