Page 1 of 1

OpenLibrary, CloseLibrary and Rich Edit Control IMA

Posted: Tue May 28, 2013 11:08 am
by Techie42
Hi all,

The following code generates an "Invalid Memory Access" error on the "End" statement. However, if I comment-out the "CloseLibrary( lib )" statement, then the error does not occur. Strangely, if I leave the "CloseLibrary( lib )" statement uncommented, but comment-out the "mainEditor = CreateWindowEx_( ... )" statement, then the IMA also disappears. It feels as if there is some sort of clash with the built-in Edit control and my instance of a rich edit control when the "msftedit" library is closed. But please let me know what I'm doing wrong :-)

I initially thought it may be something related to the following posts:

http://www.purebasic.fr/english/viewtop ... oselibrary
http://www.purebasic.fr/english/viewtop ... oselibrary

I believe that "CloseLibrary" should not cause any issues, especially as I'm checking that "lib" is an open library. Maybe the instance of my rich edit control is not being released in time for the "CloseLibrary" statement to execute, hence the error...but I'm guessing in the dark :?

Any comments / suggestions are much appreciated.

Code: Select all

;	Windows XP SP3 x86 32-bit
;	Purebasic 5.11 (x86)
;	Create unicode executable, Create threadsafe executable, Enable debugger

EnableExplicit

Procedure		main()
	Protected		lib.i
	Protected		hInstance.i
	Protected		mainWindow.i
	Protected		mainEditor.i
	Protected		appQuit.i
	Protected		eventId.i

	hInstance			= GetModuleHandle_( 0 )
	
	mainWindow		= OpenWindow( #PB_Any, 0, 0, 650, 650, "Editor", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered )

	If ( IsWindow( mainWindow ) )
		lib		= OpenLibrary( #PB_Any, "msftedit.dll" )

		If ( IsLibrary( lib ) )
			; Comment-out the next line and the IMA disappears
			mainEditor		= CreateWindowEx_( #WS_EX_STATICEDGE, "RichEdit50W", "", #WS_VISIBLE | #WS_CHILDWINDOW | #ES_AUTOHSCROLL | #WS_VSCROLL | #ES_MULTILINE | #ES_WANTRETURN | #ES_NOHIDESEL, 16, 64, 580, 540, WindowID( mainWindow ), 200, hInstance, 0 )
		EndIf

		appQuit		= #False
		
		Repeat
			eventId		= WaitWindowEvent()
			
			Select		( eventId )
			
				Case	#PB_Event_CloseWindow
					appQuit		= #True
			
			EndSelect
		
		Until  ( appQuit )

		If ( mainEditor )
			; Next statement added just in case the rich editor control instance is not being released before CloseLibrary...
			SendMessage_( mainEditor, #WM_DESTROY, 0, 0 )
		EndIf

		If ( IsLibrary( lib ) )
			; Comment-out the next line and the IMA disappears
			CloseLibrary( lib )
		EndIf
	EndIf

EndProcedure

main()

End


Re: OpenLibrary, CloseLibrary and Rich Edit Control IMA

Posted: Tue May 28, 2013 11:33 am
by Techie42
Just a quick update...

If I replace the statement

Code: Select all

			SendMessage_( mainEditor, #WM_DESTROY, 0, 0 )
with the following statement,

Code: Select all

			DestroyWindow_( mainEditor )
then the IMA disappears.

I'd still like to know if this is the correct approach, or if there is some sort of clash. Thanks.

Re: OpenLibrary, CloseLibrary and Rich Edit Control IMA

Posted: Tue May 28, 2013 10:21 pm
by IdeasVacuum
One of the experts here will have a definative answer to your question, I'm just impressed that a RichEdit50W control can be used! I think the version used by PB is 20W.

Re: OpenLibrary, CloseLibrary and Rich Edit Control IMA

Posted: Wed May 29, 2013 9:20 am
by Techie42
Hi IdeasVacuum,

Thanks for replying. One of the most impressive strengths of the Purebasic community and these forums is the enthusiasm and depth of knowledge of the members. I only wish I could give back more than I ask; all I can do is post anything and everything I can find related to my query :)

I've made numerous searches to try and learn about the different versions of the Rich Edit Control. An interesting link is this one http://blogs.msdn.com/b/murrays/archive ... ageIndex=3.

Using Winspector Spy I found that Purebasic is indeed using 20W, but I want to use the newer features in more recent versions, especially given the fact that it appears that version 4.1 (50W) handles larger rich text files much better than previous versions (and loads them more quickly too, apparently). It appears that Windows XP SP1 and above already have this version installed; my minimum OS requirement is Windows XP SP1, which works just nicely!

As I've found a way to make it work, I suppose I'm just after clarification as to whether there is a bug with the "...Library()" routines, or if I'm missing something (which is the more likely explanation). Then, my query can remain to help others along their journey if they come across similar issues.

Re: OpenLibrary, CloseLibrary and Rich Edit Control IMA

Posted: Wed May 29, 2013 11:59 am
by IdeasVacuum
Given that 50W is compatible with XP, it would be good if we could have a flag to use it as the PB EditorGadget. Probably 20W is compatible with earlier version Windows and maybe the other OS (Linux/Mac) add complications too, but a Windows XP+ flag would still be a good thing since so many PB Users are coding for Windows (and not cross platform).

Re: OpenLibrary, CloseLibrary and Rich Edit Control IMA

Posted: Wed May 29, 2013 12:34 pm
by Techie42
I agree. The number of benefits and improvements that appear to have been made between the two versions of the rich edit control are too large to be ignored. I don't know how much of a priority this would be for the development team, nor how much of a headache.

I only develop for Windows, so I don't know what underlying technology is used on Mac and Linux platforms, but a compile-time switch would seem a decent compromise. I know we can already perform a simple degradation check for different versions of the control on Windows, but this does not ensure that a specific version would be available. I'm assuming this is why Purebasic currently uses 20W as it should be very widely adopted (and tested) by now.

Maybe a quick poll on who uses the control (and if they use different versions) would be helpful to establish some sort of need?