【问题标题】:Delphi Library Memory Manager StrangeDelphi 库内存管理器奇怪
【发布时间】:2019-06-01 08:10:30
【问题描述】:

我从 Delphi 编译的 DLL 中得到一个错误,使用 c++ 在多线程中运行

Delphi (Architect 10.3 Version 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.

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;

我遇到了异常,但它不应该出现任何异常,因为那是未共享的过程变量

【问题讨论】:

    标签: c++ delphi dll


    【解决方案1】:

    在 Delphi DLL 的主块中将 IsMultiThread 设置为 True。 Delphi 的默认内存管理器默认采用单线程模式。

    begin
      IsMultiThread := True;
    end.
    

    【讨论】:

      猜你喜欢
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 2011-10-11
      相关资源
      最近更新 更多