Сбой сегментации Delphi FMX OSX 11 - PullRequest
0 голосов
/ 23 октября 2018

Существует несколько сообщений о Segmentation Fault 11, но ни один из них не отвечает на мой вопрос:

Ранее я разместил код для простого тестового приложения, но он приведен для полноты.Windows 10, Delphi 10.2.3 с OSX High Sierra 10.13.6 и Xcode 9.4.1.

ВЕДУЩИЙ:

unit uDylibTest1;

interface

uses
  System.SysUtils,
  System.Types,
  System.Diagnostics,
  System.UITypes,
  System.Classes,
  System.Variants,

  FMX.Types,
  FMX.Controls,
  FMX.Forms,
  FMX.Graphics,
  FMX.Dialogs,
  FMX.StdCtrls,
  FMX.Platform,
  FMX.Controls.Presentation;


const
  // Windows DLL Names
  {$IFDEF MSWINDOWS}
  TestDLL = 'pTestDLL.dll';
  {$ENDIF MSWINDOWS}

  // macOS DYLIB Names
  {$IFDEF MACOS}
  //TestDLL = 'libpTestDLL.dylib';
    TestDLL = 'libpTestDLL.dylib';
  {$ENDIF MACOS}

type
  TfDylibTest = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  TuDylibTest = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

{$IFDEF MSWINDOWS}
function say_Hello(Hello: string; out ReturnString: string): boolean; stdcall; external TestDLL Delayed;
{$ENDIF MSWINDOWS}
{$IFDEF MACOS}
function _say_Hello(Hello: string; out ReturnString: string): boolean; cdecl; external TestDLL;
{$ENDIF MACOS}

function local_say_hello(Hello: string): boolean; forward;

var
  fDylibTest: TfDylibTest;

implementation

function local_say_hello(Hello: string): boolean;
begin
  Result := True;
  showmessage(Hello);
end;

{$R *.fmx}
{$R *.Macintosh.fmx MACOS}
{$R *.Windows.fmx MSWINDOWS}

procedure TfDylibTest.Button1Click(Sender: TObject);
var
 b:boolean;
 sType: string;
 sDLLString: string;

begin
  b := False;
  sType := '';
  // Call the DLL Function
  {$IFDEF MSWINDOWS}
  sDLLString := 'The string passed to the Windows DLL';
  b := say_Hello(sDLLString, sType);
  {$ENDIF MSWINDOWS}
  {$IFDEF MACOS}
  sDLLString := 'The string passed to the macOS DYLIB';
  b :=  _say_Hello(sDLLString, sType);
  {$ENDIF MACOS}

  if b then
    showmessage('Returned From: ' + sType + ': TRUE')
  else
    showmessage('Retur was: FALSE');
end;

procedure TfDylibTest.Button2Click(Sender: TObject);
var
  b: boolean;
 iTime2Auth: integer;
 Opened: boolean;
 i: integer;

begin
  Opened := False;

  // test the Waitnofreeze
  iTime2Auth := 0;

  b := False;
  // Call the local Function
  b := local_say_Hello('The string passed to the LOCAL function');

  if b then
    showmessage('Say Hello OK - LOCAL')
  else
    showmessage('Say Hello Failed - LOCAL');
end;

procedure TfDylibTest.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  showmessage('[Test App] onClose');
end;

procedure TfDylibTest.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  canClose := True;
  showmessage('[Test App] onCloseQuery');
end;

procedure TfDylibTest.FormCreate(Sender: TObject);
begin
  showmessage('[Test App] onCreate');
end;

initialization
  showmessage('[Test App] Initialization');  <<<<-- THIS IS EXECUTED

finalization
  showmessage('[Test App] Finalization');    <<<<-- THIS IS NOT EXECUTED

end.

И DYLIB:

unit uTestDLL;

interface

uses
  System.Diagnostics,
  System.Classes;

// External functions and procedures
{$IFDEF MSWINDOWS}
function say_Hello(Hello: string; out ReturnString: string): boolean; stdcall; forward;
{$ENDIF MSWINDOWS}
{$IFDEF MACOS}
function _say_Hello(Hello: string; out ReturnString: string): boolean; cdecl; forward;
{$ENDIF MACOS}

exports
  {$IFDEF MSWINDOWS}
  say_Hello;
  {$ENDIF MSWINDOWS}
  {$IFDEF MACOS}
  _say_Hello;
  {$ENDIF MACOS}

Implementation

{$IFDEF MSWINDOWS}
function say_Hello(Hello: string; out ReturnString: string): boolean; stdcall; export;
{$ENDIF MSWINDOWS}
{$IFDEF MACOS}
function _say_Hello(Hello: string; out ReturnString: string): boolean; cdecl; export;
{$ENDIF MACOS}
begin
  {$IFDEF MSWINDOWS}
  ReturnString := 'Windows DLL, Received: ' + Hello;
  {$ENDIF MSWINDOWS}
  {$IFDEF MACOS}
  ReturnString := 'macOS DYLIB, Received: ' + Hello;
  {$ENDIF MACOS}
  Result := True;
end;

end.

В основном функции в DYLIB передается строковый параметр, он возвращает TRUE с переменной OUT другой строки, которая доказывает, что данные были переданы в функцию DYLIB и возвращены.

У меня есть простые операторы showmessage () в вызывающем приложении для таких вещей, как onCloseQuery, onClose и т. Д., Все они показывают, что все работает, как должно, до закрытия приложения (выполнение onCloseQuery, выполнение onClose и примерно полсекунды)до секунды после того, как все выключается, появляется сообщение о проблеме).Обратите внимание, что раздел инициализации выполнен, но раздел финализации НЕ выполнен (хотя он выполняется при работе в режиме Win32).Я поместил ссылку на диск Google в журнал ошибок. Отчет о проблемах OSX

Когда я смотрю на трассировку стека, ни один из двух моих файлов (DylibTest и libpTesDLL.DYLIB) не упоминается, это все RTL / системные блоки.Я гуглил Seg Fault 11 и искал форум, но ничего не похоже.

Буду признателен за совет, как мне поступить?

1 Ответ

0 голосов
/ 23 октября 2018

string НЕ является переносимым типом данных для взаимодействия через границы библиотеки.Вместо этого используйте PChar, и тогда вам нужно решить, кто ВОЗ выделяет и освобождает память для строк - библиотеки или вызывающего приложения - и КАК они выделяются и освобождаются, и делают это совместимым способом взаимодействия.Вы не передаете строки через границу библиотеки безопасным способом, поэтому неудивительно, что вы разбиваете свой код.

Вместо этого попробуйте что-то вроде этого:

ВЕДУЩИЙ:

// Windows DLL
{$IFDEF MSWINDOWS}
const
  TestDLL = 'pTestDLL.dll';

function say_Hello(Hello: PChar; out ReturnString: PChar): boolean; stdcall; external TestDLL Delayed;
procedure free_String(Mem: PChar); stdcall; external TestDLL Delayed;
{$ENDIF}

// macOS DYLIB
{$IFDEF MACOS}
const
  TestDLL = 'libpTestDLL.dylib';

function say_Hello(Hello: PChar; out ReturnString: PChar): boolean; cdecl; external TestDLL name '_say_Hello';
function free_String(Mem: PChar); cdecl; external TestDLL name '_free_String';
{$ENDIF}

...

procedure TfDylibTest.Button1Click(Sender: TObject);
var
 b: boolean;
 sType: PChar;
 sDLLString: string;
begin
  b := False;
  sType := nil;

  // Call the DLL Function
  {$IF DEFINED(MSWINDOWS) OR DEFINED(MACOS)}
  sDLLString := 'The string passed to the ' + {$IFDEF MSWINDOWS}'Windows DLL'{$ELSE}'macOS DYLIB'{$ENDIF};
  b := say_Hello(PChar(sDLLString), sType);
  if b then
  begin
    try
      ShowMessage('Returned From: ' + string(sType) + ': TRUE');
    finally
      free_String(sType);
    end;        
    Exit;
  end;
  {$IFEND}

  ShowMessage('Retur was: FALSE');
end;

DYLIB:

unit uTestDLL;

interface

{$IF DEFINED(MSWINDOWS) OR DEFINED(MACOS)}

// External functions and procedures
function say_Hello(Hello: PChar; out ReturnString: PChar): boolean; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; forward;
procedure free_String(S: PChar); {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; forward;

exports
  say_Hello{$IFNDEF MSWINDOWS} name '_say_Hello'{$ENDIF},
  free_String{$IFNDEF MSWINDOWS} name '_free_String'{$ENDIF};

{$IFEND}

implementation

uses
  System.Diagnostics,
  System.Classes;

{$IF DEFINED(MSWINDOWS) OR DEFINED(MACOS)}

function say_Hello(Hello: PChar; out ReturnString: PChar): boolean; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; export;
var
  s: string;
begin
  try
    s := 'FROM: ' + {$IFDEF MSWINDOWS}'Windows DLL'{$ELSE}'macOS DYLIB'{$ENDIF} + ', Received: ' + string(Hello);
    ReturnString := StrAlloc(Length(s));
    try
      StrPCopy(ReturnString, s);
    except
      StrDispose(ReturnString);
      throw;
    end;
    Result := True;
  except
    Result := False;
  end;
end;

procedure free_String(S: PChar); {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; export;
begin
  StrDispose(S);
end;

{$IFEND}

end.

В качестве альтернативы:

ВЕДУЩИЙ:

// Windows DLL
{$IFDEF MSWINDOWS}
const
  TestDLL = 'pTestDLL.dll';

function say_Hello(Hello: PChar): PChar; stdcall; external TestDLL Delayed;
procedure free_String(Mem: PChar); stdcall; external TestDLL Delayed;
{$ENDIF}

// macOS DYLIB
{$IFDEF MACOS}
const
  TestDLL = 'libpTestDLL.dylib';

function say_Hello(Hello: PChar): PChar; cdecl; external TestDLL name '_say_Hello';
function free_String(Mem: PChar); cdecl; external TestDLL name '_free_String';
{$ENDIF}

...

procedure TfDylibTest.Button1Click(Sender: TObject);
var
 sType: PChar;
 sDLLString: string;
begin
  sType := nil;

  // Call the DLL Function
  {$IF DEFINED(MSWINDOWS) OR DEFINED(MACOS)}
  sDLLString := 'The string passed to the ' + {$IFDEF MSWINDOWS}'Windows DLL'{$ELSE}'macOS DYLIB'{$ENDIF};
  sType := say_Hello(PChar(sDLLString));
  if sType <> nil then
  begin
    try
      ShowMessage('Returned From: ' + string(sType) + ': TRUE');
    finally
      free_String(sType);
    end;        
    Exit;
  end;
  {$IFEND}

  ShowMessage('Retur was: FALSE');
end;

DYLIB:

unit uTestDLL;

interface

{$IF DEFINED(MSWINDOWS) OR DEFINED(MACOS)}

// External functions and procedures
function say_Hello(Hello: PChar): PChar; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; forward;
procedure free_String(S: PChar); {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; forward;

exports
  say_Hello{$IFNDEF MSWINDOWS} name '_say_Hello'{$ENDIF},
  free_String{$IFNDEF MSWINDOWS} name '_free_String'{$ENDIF};

{$IFEND}

implementation

uses
  System.Diagnostics,
  System.Classes;

{$IF DEFINED(MSWINDOWS) OR DEFINED(MACOS)}

function say_Hello(Hello: PChar): PChar; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; export;
var
  s: string;
begin
  try
    s := 'FROM: ' + {$IFDEF MSWINDOWS}'Windows DLL'{$ELSE}'macOS DYLIB'{$ENDIF} + ', Received: ' + string(Hello);
    Result := StrAlloc(Length(s));
    try
      StrPCopy(Result, s);
    except
      StrDispose(Result);
      throw;
    end;
  except
    Result := nil;
  end;
end;

procedure free_String(S: PChar); {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; export;
begin
  StrDispose(S);
end;

{$IFEND}

end.
...