FreeMemory crash with Registry subkey pointer

Windows specific forum
Hi-Toro
Enthusiast
Enthusiast
Posts: 265
Joined: Sat Apr 26, 2003 3:23 pm

FreeMemory crash with Registry subkey pointer

Post by Hi-Toro »

Hi all,

Can anyone see what's wrong with this code? I'm attempting to parse available Windows Updates, but although I can list the updates (or what I believe to be the updates), it crashes at the end on freeing the memory used to store the Registry key name, and I can't see any good reason for it!

Code: Select all

EnableExplicit

Define subkeyname.s = "SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\ApplicabilityEvaluationCache"
Define key.i

If RegOpenKeyEx_ (#HKEY_LOCAL_MACHINE, @subkeyname, 0, #KEY_READ, @key) = #ERROR_SUCCESS

	Define index.i = 0
	Define size.i = 1024 ; Start value, will be increased on #ERROR_MORE_DATA in loop...
	Define last.i

	Define *subkey = AllocateMemory (size)
	
	Define result.i

	Define valuelength.i
	Define keylength.i

	Repeat

		result = RegEnumKeyEx_ (key, index, *subkey, @size, #Null, #Null, #Null, @last)
	
		Select result
			
			Case #ERROR_SUCCESS
		
				Debug PeekS (*subkey)
			
				; Look through values for ApplicabilityState $112, CurrentState $0 = not installed
			
				index = index + 1

			Case #ERROR_MORE_DATA
			
				; Get length of largest key name...
				
				If RegQueryInfoKey_ (key, #Null, #Null, #Null, #Null, @keylength, #Null, #Null, @valuelength, #Null, #Null, #Null) = #ERROR_SUCCESS
			
	;				Debug size
	;				Debug keylength
	;				Debug valuelength
					
					size = keylength + 1 ; Add null char
					*subkey = ReAllocateMemory (*subkey, size)
				
				Else
					Debug "RegQueryInfoKey failed" ; Doesn't happen...
				EndIf
				
			Case #ERROR_NO_MORE_ITEMS
			
				Break
				
			Default
			
				Debug "Something else!" ; Doesn't happen...
				Break
				
		EndSelect
		
	ForEver
	
	Debug "Freeing *subkey..."
	FreeMemory (*subkey)		; *** CRASH! *** -- works if commented out
	
	Debug "Closing Registry key..."
	RegCloseKey_ (key)
	
	Debug "Finished!"
	
EndIf
I'm guessing I'm doing something stupid with *subkey, but it all looks correct to me -- at 03:30 in the morning, I should add!

(BTW Anyone know why PB closes the Debug Output window on OK'ing "The debugged executable quit unexpectedly"? It makes it really hard to parse the output since you can't access the scrollbar while the dialog is open, but closing it closes Debug Output!)
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: FreeMemory crash with Registry subkey pointer

Post by RASHAD »

Tested with PB 5.41 x86,x64 windows 10 x64
Works fine no crash

Code: Select all

#KEY_WOW64_64KEY=$100
#KEY_WOW64_32KEY=$200

EnableExplicit

Define subkeyname.s = "SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\ApplicabilityEvaluationCache"
Define key.i

If RegOpenKeyEx_ (#HKEY_LOCAL_MACHINE, @subkeyname, 0, #KEY_READ|#KEY_WOW64_64KEY, @key) = #ERROR_SUCCESS

   Define index.i = 0
   Define size.i = 1024 ; Start value, will be increased on #ERROR_MORE_DATA in loop...
   Define last.i

   Define *subkey = AllocateMemory (size*StringByteLength(" ")+1)
   
   Define result.i

   Define valuelength.i
   Define keylength.i

   Repeat

      result = RegEnumKeyEx_ (key, index, *subkey, @size, #Null, #Null, #Null, @last)
   
      Select result
         
         Case #ERROR_SUCCESS
     
            Debug PeekS (*subkey)
         
            ; Look through values for ApplicabilityState $112, CurrentState $0 = not installed
         
            index = index + 1

         Case #ERROR_MORE_DATA
         
            ; Get length of largest key name...
           
            If RegQueryInfoKey_ (key, #Null, #Null, #Null, #Null, @keylength, #Null, #Null, @valuelength, #Null, #Null, #Null) = #ERROR_SUCCESS
         
   ;            Debug size
   ;            Debug keylength
   ;            Debug valuelength
               
               size = keylength*StringByteLength(" ") + 1 ; Add null char
               *subkey = ReAllocateMemory (*subkey, size)
           
            Else
               Debug "RegQueryInfoKey failed" ; Doesn't happen...
            EndIf
           
         Case #ERROR_NO_MORE_ITEMS
         
            Break
           
         Default
         
            Debug "Something else!" ; Doesn't happen...
            Break
           
      EndSelect
     
   ForEver
   ;Debug *subkey
   ;Debug "Freeing *subkey..."
   FreeMemory (*subkey)      ; *** CRASH! *** -- works if commented out
   
   Debug "Closing Registry key..."
   RegCloseKey_ (key)
   
   Debug "Finished!"
   
EndIf
Edit :Modified for Ascii & Unicode
Last edited by RASHAD on Fri Feb 26, 2016 10:06 pm, edited 1 time in total.
Egypt my love
Hi-Toro
Enthusiast
Enthusiast
Posts: 265
Joined: Sat Apr 26, 2003 3:23 pm

Re: FreeMemory crash with Registry subkey pointer

Post by Hi-Toro »

Ah, wasn't aware of #KEY_WOW64_64KEY / #KEY_WOW64_32KEY, will give this a go tonight -- many thanks for having a look, Rashad, really appreciate it!
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: FreeMemory crash with Registry subkey pointer

Post by freak »

If you compile in unicode mode, then your buffer sizes are too small (you need two bytes per character).

The purifier should help you locate the line that causes the problem in such a case.
quidquid Latine dictum sit altum videtur
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: FreeMemory crash with Registry subkey pointer

Post by RASHAD »

Previous post modified for Ascii & Unicode
Egypt my love
Hi-Toro
Enthusiast
Enthusiast
Posts: 265
Joined: Sat Apr 26, 2003 3:23 pm

Re: FreeMemory crash with Registry subkey pointer

Post by Hi-Toro »

Ah, thanks, guys. I had no idea this was something I needed to take care of... I guess I assumed Windows was still using ASCII for all this stuff!

Works perfectly now, of course... will post my code if I get something useful out of it!

Much appreciated.
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Post Reply