Problems accessing a library under MacOS lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- ChPr · 2024-08-31 09:43 Hello everyone, In an application, I use a library (“libarticles.dylib” in the case of MacOS). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit LiensDLL; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils, Dynlibs, Menus, Utils; type  TListSousMenu = array of TMenuItem;  TAddMenu = function(var NbFct: Integer): PChar; stdcall;  TEnvoiDirLib = function(Dir: PChar): Boolean; stdcall;  TArticleEnBdD = function(var Ref: PChar; var IndA, Qte: Integer; var PU: Double): PChar; stdcall;  TMaJStock = function(Ref, Qte, QtePrec: Integer): Integer; stdcall; var  HndLib: THandle;  LibOK: Boolean;              {Indique si la librairie est utilisée et valide}  ListSousMenu: TListSousMenu;  ArticleEnBdD: TArticleEnBdD;  MaJStock: TMajStock; function InstalleLib(FchLib: String): Boolean;procedure ClickMenuLib(Sender: TObject);function LibereLib: Boolean; implementation uses  FntBase; var  AddMenu: TAddMenu;  EnvoiDirLib: TEnvoiDirLib; function InstalleLib(FchLib: String): Boolean;var  Fch: String;  Menu, SousMenu: TMenuItem;  i, N, NbM, NoM, PosM: Integer;begin  Fch:= DirEXE;  Fch:= IncludeTrailingPathDelimiter(Fch+FchLib);  {$IFDEF WINDOWS}    FchLib:= Fch+FchLib+'.dll';  {$ENDIF}  {$IFDEF LINUX}    FchLib:= Fch+'lib'+LowerCase(FchLib)+'.so';  {$ENDIF}  {$IFDEF DARWIN}    FchLib:= Fch+'lib'+LowerCase(FchLib)+'.dylib';  {$ENDIF}  HndLib:= LoadLibrary(PChar(FchLib));  if HndLib <> 0 then  begin    Pointer(AddMenu):= GetProcAddress(HndLib, 'AddMenu');    Pointer(EnvoiDirLib):= GetProcAddress(HndLib, 'EnvoiDirLib');    Pointer(ArticleEnBdD):= GetProcAddress(HndLib, 'ArticleEnBdD');    Pointer(MajStock):= GetProcAddress(HndLib, 'MajStock');    if (@AddMenu <> nil)    and (@EnvoiDirLib <> nil)    and (@ArticleEnBdD <> nil)    and (@MajStock <> nil) then    begin      EnvoiDirLib(PChar(Fch));                     {On envoie le Dir de la Lib}      Menu:= TMenuItem.Create(Base.MenuPrinc);      PosM:= 1;                    {Position d'insertion des menus de la Lib}      N:= 0;{AddMenu(0) retourne le nom du menu à ajouter et le Nb de Sousmenu}      Menu.Caption:= string(AddMenu(N));      SetLength(ListSousMenu, Length(ListSousMenu)+N);      NbM:= Base.MenuPrinc.Items.Count;          {Nombre de menus prédéfinis}      Base.MenuPrinc.Items.Insert(PosM, Menu);              {Insertion du menu}      for i:= 1 to N do                             {Création des N sous-menus}      begin        SousMenu:= TMenuItem.Create(Base.MenuPrinc);        ListSousMenu[High(ListSousMenu)-N+i]:= SousMenu;        NoM:= i;      SousMenu.ImageIndex:= i+11;   {11 = dernier indice des des menus fixes}        SousMenu.Caption:= string(AddMenu(NoM));        SousMenu.Enabled:= True;        SousMenu.Tag:= i;          {Mémo de l'indice du sous-menu dans son Tag}        SousMenu.OnClick:= @Base.ClickMenu;  {Affectation de la fonction Click}        Base.MenuPrinc.Items[PosM].Add(SousMenu);       {Installe le sous- menu}      end;      LibOK:= True;      Result:= True;    end    else      LibOK:= False;  end  else    LibOk:= False;  Result:= LibOK;end; procedure ClickMenuLib(Sender: TObject);var//  Str: String;  Pch: PChar;  i: Integer;  d: Double;begin  case TMenuItem(Sender).Tag of 1: begin                 {Connexion pour consultation, modification de la BdD}      i:= 0;      ArticleEnBdD(Pch, i, i, d);    end;{ 2: Str:= ''; 3: Str:= '';}  end;end; function LibereLib: Boolean;begin  FreeLibrary(HndLib);end; end. On Windows and Linux, this poses no problem. On Mac OS, it crashes on line 66: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i EnvoiDirLib(PChar(Fch)); with the following error code: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i "Process stopped with reason : EXC_BAD_ACCESS (code=1, address=0x0)". Any idea of the problem? Best regards. Pierre. ---------------------------------------------------------------------- cdbc · 2024-08-31 10:26 Hi You could try: setlenght(Fch,1024); or something that fits memorywise, I think the crash comes from the library writing to an empty string ~ nil; a pchar can't reallocate like a string does. with setlength you preallocate a /buffer/ in Fch. Regards Benny ---------------------------------------------------------------------- ChPr · 2024-08-31 17:57 Thank you "cdbc" for this solution. However, it doesn't work. In fact, “Fch” is already allocated (not nil) to lines 43 and 44. Best regards. Pierre. ---------------------------------------------------------------------- cdbc · 2024-08-31 19:40 Oh, yeah I see that now, sorry my bad... As far as the error-message goes, it seems something in that call, references a nil pointer?!? Mysterious... Regards Benny