FinalCloud Posted August 31, 2010 Share Posted August 31, 2010 What I'm trying to do is when a user removes/inserts their access card, a log write captures the time and date. So far what I have found, on this board, has worked. The problem is that the program has a massive memory leak. On my PC, for a full day, it got up to 250MB of RAM. Code: expandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_outfile=..\..\..\..\Executables\Logs\Locks\locks.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #Include <File.au3> #include <GUIConstantsEx.au3> Global $sLogFile Global $bLogout Global Const $NOTIFY_FOR_THIS_SESSION = 0x0 Global Const $NOTIFY_FOR_ALL_SESSIONS = 0x1 Global Const $WM_WTSSESSION_CHANGE = 0x2B1 Global Const $WTS_CONSOLE_CONNECT = 0x1 Global Const $WTS_CONSOLE_DISCONNECT = 0x2 Global Const $WTS_REMOTE_CONNECT = 0x3 Global Const $WTS_REMOTE_DISCONNECT = 0x4 Global Const $WTS_SESSION_LOGON = 0x5 Global Const $WTS_SESSION_LOGOFF = 0x6 Global Const $WTS_SESSION_LOCK = 0x7 Global Const $WTS_SESSION_UNLOCK = 0x8 Global Const $WTS_SESSION_REMOTE_CONTROL = 0x9 Global $hGUI = GUICreate(" ", 0, 0, 2000, 2000); GUI to receive notification Global $sMsg = " " ;Get system variables $path = "\\server\folder" $sUsername = EnvGet("USERNAME") $sComputer = EnvGet("COMPUTERNAME") $sLogFile = $path & $sComputer & "_" & $sUsername & "_info.log" $bLogOut = False GUISetState(@SW_HIDE) TraySetState(2) GUIRegisterMsg($WM_WTSSESSION_CHANGE, "_WM_WTSSESSION_CHANGE") DllCall("Wtsapi32.dll", "int", "WTSRegisterSessionNotification", "hwnd", $hGUI, "dword", $NOTIFY_FOR_THIS_SESSION) While 1 If GUIGetMsg() = -3 Then ExitLoop If $bLogout = True Then ExitLoop EndIf TraySetState(2) WEnd DllCall("Wtsapi32.dll", "int", "WTSUnRegisterSessionNotification", "hwnd", $hGUI) Func _WM_WTSSESSION_CHANGE($hWnd, $iMsgID, $wParam, $lParam) $sMsg = "" $bLogout = False Switch $wParam Case $WTS_CONSOLE_CONNECT $sMsg &= $sUsername & " was connected to the console terminal." Case $WTS_CONSOLE_DISCONNECT $sMsg &= $sUsername & " was disconnected from the console terminal." Case $WTS_REMOTE_CONNECT $sMsg &= $sUsername & " was connected to the remote terminal." Case $WTS_REMOTE_DISCONNECT $sMsg &= $sUsername & " was disconnected from the remote terminal." ;Case $WTS_SESSION_LOGON ; $sMsg &= $sUsername & " has logged on to the session." ;Case $WTS_SESSION_LOGOFF ; $sMsg &= $sUsername & " has logged off the session." ; $bLogout = True Case $WTS_SESSION_LOCK $sMsg &= $sUsername & " has locked this computer." Case $WTS_SESSION_UNLOCK $sMsg &= $sUsername & " has unlocked this computer." Case $WTS_SESSION_REMOTE_CONTROL $sMsg &= $sUsername & " has changed its remote controlled status." EndSwitch _FileWriteLog($sLogFile, " - " & $sMsg) EndFunc Need to either find a better way or resolve the memory leak. Glad I finally joined the forum, I've been using it for a bit now, very helpful. Link to comment Share on other sites More sharing options...
Tvern Posted August 31, 2010 Share Posted August 31, 2010 Welcome to the forum. I can't see any obvious memory leak there. I would think it'd be somewhere in the loop, or in _WM_WTSSESSION_CHANGE(). The loop is so basic that I don't believe that can be the cause and so is everything in the switch statement in _WM_WTSSESSION_CHANGE, so that only leaves _FileWriteLog(). You could see if the leak remains when you take out the _FileWriteLog() (just to test), but to me it seems weird that that script would have issues. (Also make $sMsg local, as you seem to be using it in a local scope.) I'll also have a look if I can reproduce the issue. Link to comment Share on other sites More sharing options...
JohnOne Posted August 31, 2010 Share Posted August 31, 2010 Its the constant destruction of the tray icon. Run this code and you can visibly watch it getting higher and higher in task manager Just use #NoTrayIcon if you want to hide it. While 1 TraySetState(2) Sleep(10) WEnd AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Tvern Posted August 31, 2010 Share Posted August 31, 2010 I was just coming back to post that. Completely overlooked it at fist. Too slow. Link to comment Share on other sites More sharing options...
FinalCloud Posted September 1, 2010 Author Share Posted September 1, 2010 Thanks everyone! It was the TraySetState(2) in the While loop that was causing the issue. I have removed that line and it is holding steady at 5188K. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now