Win32: winapi StartService ... Why mem leak ?

BlitzMax Forums/BlitzMax Programming/Win32: winapi StartService ... Why mem leak ?

skn3(Posted 2007) [#1]
Cane anyone figure out why this code is leaking?

StartService
http://msdn2.microsoft.com/en-us/library/ms686321.aspx

OpenSCManager
http://msdn2.microsoft.com/en-us/library/ms684323.aspx

OpenService
http://msdn2.microsoft.com/en-us/library/ms684330.aspx

CloseServiceHandle
http://msdn2.microsoft.com/en-us/library/ms682028.aspx

Strict

Import "-ladvapi32"

Extern
	Function _OpenSCManager:Int(nmachinename$z,ndatabasename$z,ndesiredaccess:Int) = "OpenSCManagerA@12"
	Function _CloseServiceHandle:Int(nhandle:Int) = "CloseServiceHandle@4"
	Function _OpenService:Int(nmananger:Int,nservicename$z,ndesiredaccess:Int) = "OpenServiceA@12"
	Function _StartService:Int(service:Int,serviceargs:Int,serviceargvectors$z) = "StartServiceA@12"
	
	Function _GetLastError:Int() = "GetLastError@0"
End Extern

Const SC_MANAGER_ENUMERATE_SERVICE:Int = 4
Const SERVICE_START:Int = 16
Const SERVICE_STOP:Int = 32
Const SERVICE_QUERY_STATUS:Int  = 4

Type tservice
	Field handle:Int
	
	Method start:Int()
		'starts the service
		
	End Method
	
	Method stop:Int()
		'stops the service
	End Method
	
	Method status:Int()
		'gets the status of the service
	End Method
End Type

Function OpenService:tservice(nname:String)
	'open the service manager
	Local temp_servicemanagerhandle:Int
	Local temp_offset1:Int
	Local temp_offset2:Int
	Local temp_offset3:Int
	Local temp_offset4:Int
	Local temp_servicehandle:Int
	DebugStop()
	
	temp_servicemanagerhandle = _OpenSCManager(Null,Null,SC_MANAGER_ENUMERATE_SERVICE)
	
	If temp_servicemanagerhandle = 0
		Print "lasterror = "+_GetLastError()
	Else
		Print "openend service manager = "+temp_servicemanagerhandle
	
		temp_servicehandle = _OpenService(temp_servicemanagerhandle,nname,SERVICE_QUERY_STATUS | SERVICE_START | SERVICE_STOP)
		
		If temp_servicehandle
			Print "openend service = "+temp_servicehandle
			
			Local temp_service:tservice = New tservice
			temp_service.handle = temp_servicehandle
			
			Local temp_result:Int = _StartService(temp_service.handle,0,Null)
			
			If temp_result = 0 
				Print "error = "+_GetLastError()
			Else
				Print "result = "+temp_result
				
				_CloseServiceHandle(temp_servicehandle)
			End If
		End If
		
		'close teh service manager handle
		_CloseServiceHandle(temp_servicemanagerhandle)
	End If
End Function

OpenService("Messenger")



N(Posted 2007) [#2]
Where is it leaking, exactly?


skn3(Posted 2007) [#3]
Run in debugmode. You will see when it gets to the openservice command it leaks over into the "temp_offset4" value. Those temp_offsets were put there for testing.


N(Posted 2007) [#4]
Can I ask you why the value of temp_result changes throughout steps, when it is only set once? I have a feeling you're doing something here with threads that you shouldn't be.

Edit: That or possibly too many things are getting pushed onto the stack? I'm not sure, but threading is the only thing that comes to mind, considering you can get some pretty odd things happening with them.


skn3(Posted 2007) [#5]
Well this code is just winapi functions for start / stop of services. Unless windows secretly creates a thread when you execute one of these commands then personally I am not doing anything like that.

I noticed the change with each step. And it baffled me too!


skn3(Posted 2007) [#6]
Just realized the problem. For anyone searching the forum for similar bugs. Remember to use "win32" as the calling convention in extern.

Import "-ladvapi32"

Extern "win32"
	Function _OpenSCManager:Int(nmachinename$z,ndatabasename$z,ndesiredaccess:Int) = "OpenSCManagerA@12"
	Function _CloseServiceHandle:Int(nhandle:Int) = "CloseServiceHandle@4"
	Function _OpenService:Int(nmananger:Int,nservicename$z,ndesiredaccess:Int) = "OpenServiceA@12"
	Function _StartService:Int(service:Int,serviceargs:Int,serviceargvectors$z) = "StartServiceA@12"
	
	Function _GetLastError:Int() = "GetLastError@0"
End Extern