PlanetSquires Forums

Support Forums => PlanetSquires Software => Topic started by: jermy on October 11, 2022, 05:32:50 PM

Title: "segmentation violation" signal
Post by: jermy on October 11, 2022, 05:32:50 PM
Hello dear people, this code just works, but I keep getting this message on exit;
Aborting due to runtime error 12 ("segmentation violation" signal) in WinMain.bas::WNDPROC()
If I want to load the dll from memory.
if I don't load the dll from memory then it just works fine no errors.
Do I have to remove the dll from memory though, or does the system do that for me?
Or am I doing something else wrong?

FUNCTION WndProc (BYVAL hWnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

STATIC as HANDLE m_hRichEditLib 

    SELECT CASE uMsg

       CASE WM_CREATE

            DIM As CREATESTRUCT Ptr cstruct
            cstruct = CAST(CREATESTRUCT Ptr,lParam)

           dim as long dwStyle =  WS_VISIBLE OR WS_TABSTOP OR ES_LEFT OR WS_HSCROLL OR WS_VSCROLL or WS_CHILD OR ES_AUTOHSCROLL OR ES_AUTOVSCROLL OR ES_MULTILINE OR ES_WANTRETURN OR ES_NOHIDESEL OR ES_SAVESEL
           dim as long dwExStyle = WS_EX_CLIENTEDGE
   
           ' // load the library
           m_hRichEditLib = LoadLibrary("MSFTEDIT.DLL")
 
          ' // Create the control
          dim as HWND hCtl = CreateWindowExW(dwExStyle, MSFTEDIT_CLASS, "", dwStyle, 50, 50, 250, 250, hWnd, CAST(HMENU, IDC_EDIT), cstruct->hInstance, NULL)
       EXIT FUNCTION
 
    CASE WM_DESTROY
 
         IF m_hRichEditLib THEN FreeLibrary m_hRichEditLib         
         ' // End the application
         PostQuitMessage(0)
         EXIT FUNCTION
   END SELECT

   ' // Default processing of Windows messages
   FUNCTION = DefWindowProcW(hWnd, uMsg, wParam, lParam)

END FUNCTION

thank you in advance
Title: Re: "segmentation violation" signal
Post by: Paul Squires on October 11, 2022, 06:10:28 PM
You should not be using FreeLibrary in the WM_DESTROY message. Windows still exist during WM_DESTROY (ie, your RichEdit control) so if you unload the RichEdit DLL then you will have trouble. Other messages will fire after WM_DESTROY that expect a valid control handle (eg. WM_NCDESTROY).

The WM_NCDESTROY message is sent after the child windows have been destroyed. In contrast, WM_DESTROY is sent before the child windows are destroyed.

Personally, I would make m_hRichEditLib into global handle variable and call LoadLibrary BEFORE the main message pump, and FreeLibrary AFTER the main message pump exits.
Title: Re: "segmentation violation" signal
Post by: jermy on October 12, 2022, 01:58:05 PM
it's true what you tell I tested it, maybe a construcor / destructor is better in my case.
I have no experience with that yet so that will be fun.