IMA error when dynamically loading a DLL

Windows specific forum
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

IMA error when dynamically loading a DLL

Post by breeze4me »

This is about a bug that has been fixed in the past.
https://www.purebasic.fr/english/viewtopic.php?t=43173

I've been testing that code and found a problem: it works fine when using the lib file, but crashes when dynamically loading the dll.

dll code:

Code: Select all

ProcedureDLL addScrollArea(hWnd)
  oldGadgetList = UseGadgetList(hWnd)
  gadget = ContainerGadget(#PB_Any, 0, 0, 400, 400)
    CloseGadgetList()
    SetGadgetColor(gadget, #PB_Gadget_BackColor, #Red)
  UseGadgetList(oldGadgetList)
EndProcedure
main code(OK):

Code: Select all

Import "dll.lib"
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    addScrollArea(a) As "_addScrollArea@4"
  CompilerElse
    addScrollArea(a)
  CompilerEndIf
EndImport

#win = 16  ;Change to 15 or less to remove the crash!

If OpenWindow(#win, 0, 0, 600, 600, "", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
  addScrollArea(WindowID(#win))
  Repeat 
    event=WaitWindowEvent() 
  Until Event = #PB_Event_CloseWindow
EndIf
main code (IMA in PB x86 / debugger crashes in PB x64):

Code: Select all

#win = 16  ;Change to 15 or less to remove the crash!

Prototype addScrollArea(a)
Define addScrollArea_.addScrollArea

If OpenLibrary(0, "dll.dll")
  addScrollArea_ = GetFunction(0, "addScrollArea")
  If OpenWindow(#win, 0, 0, 600, 600, "", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
    If addScrollArea_
      addScrollArea_(WindowID(#win))
    EndIf
    Repeat 
      event=WaitWindowEvent() 
    Until Event = #PB_Event_CloseWindow
  EndIf
  CloseLibrary(0)
EndIf     ; Invalid memory access. (read error at address 1387796518)
User avatar
mk-soft
Always Here
Always Here
Posts: 6231
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [6.21 b4] IMA error when dynamically loading a DLL

Post by mk-soft »

It doesn't work that way. A DLL has its own object management and is not coupled with the main executable.
Link: Access gadget from libray
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: [6.21 b4] IMA error when dynamically loading a DLL

Post by breeze4me »

Anyway, I found a workaround.
If the CloseLibrary(0) line is removed, there is no error.

Edit:
I tested a complex case and the workaround didn't work. :?
The ".lib" file must be used for this to work properly.

@mk-soft
I read that post a long time ago.
What I'm trying to do is not share the object management system, I'm just researching how to create gadgets from DLLs like the way srod used in the original post.
User avatar
mk-soft
Always Here
Always Here
Posts: 6231
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [6.21 b4] IMA error when dynamically loading a DLL

Post by mk-soft »

breeze4me wrote: Sat Apr 12, 2025 11:39 am @mk-soft
I read that post a long time ago.
What I'm trying to do is not share the object management system, I'm just researching how to create gadgets from DLLs like the way srod used in the original post.
But the gadget is then in the wrong object list. In the DLL and not in the Main ...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: [6.21 b4] IMA error when dynamically loading a DLL

Post by breeze4me »

mk-soft wrote: Sat Apr 12, 2025 2:19 pm But the gadget is then in the wrong object list. In the DLL and not in the Main ...
I know. I plan to have the main code manipulate the gadgets created by DLLs in a way that calls functions inside DLLs.
This is enough to support custom gadgets via DLLs in a form designer app.
Fred
Administrator
Administrator
Posts: 18167
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [6.21 b4] IMA error when dynamically loading a DLL

Post by Fred »

I fixed the crash when the DLL is dynamically loaded and auto-freed at end in 6.21 beta 5 (ie: CloseLibrary() not called). But if you close the DLL before exiting, there is still some callbacks registered from the DLL in the main process and it will crash because the window will try to call the callback when doing its cleanup. So you can either let the program unload the DLL automatically at end or close all the windows before calling CloseLibrary().
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: [6.21 b4] IMA error when dynamically loading a DLL

Post by breeze4me »

Fred wrote: Tue Apr 15, 2025 8:35 am I fixed the crash when the DLL is dynamically loaded and auto-freed at end in 6.21 beta 5 (ie: CloseLibrary() not called). But if you close the DLL before exiting, there is still some callbacks registered from the DLL in the main process and it will crash because the window will try to call the callback when doing its cleanup. So you can either let the program unload the DLL automatically at end or close all the windows before calling CloseLibrary().
Thank you for the explanation. :)
Post Reply