After leaving an application idle for a long time it usually crashed when I clicked on a form.
I am leaving it every day idle to see if I find the error and finally this morning I started using it and after a few clicks I got:
system error 1158 current process has used all of its system allowance of handle
So somehow I use all resources and luckily this morning I started using the app when it was almost out of resources and i used it until all handles were not available anymore. While on all the other days I was too late and no more handles were available (this is just what I suspect). Of course in this case the application was crashing as I clicked on it.
Now could you please suggest a solution? I use threads that regularly check for new messages or for counting how many users are online. But I double checked and I free everything after every thread. (but may be there is something more that I don't know about threads). The Application is always connected to the DB (I use DevArt SDAC), anyway there were no disconnections, because in case of a disconnection I have a warning and i prompt the user for trying to reconnect.
You can write a function to monitor the open handles for your application using the GetProcessHandleCount
function and then debug your application and show the results using something like OutputDebugString
.
uses
Windows,
SysUtils;
function GetProcessHandleCount(hProcess: THandle; var pdwHandleCount: DWORD): BOOL; stdcall; external 'Kernel32.dll' name 'GetProcessHandleCount';
function GetOpenHandles : DWORD;
begin
if not GetProcessHandleCount(GetCurrentProcess,Result) then
RaiseLastOSError;
end;
ans using like this
OutputDebugString(PAnsiChar(IntToStr(GetOpenHandles)));
Task1;
OutputDebugString(PAnsiChar(IntToStr(GetOpenHandles)));
Task2;
OutputDebugString(PAnsiChar(IntToStr(GetOpenHandles)));
Task3;
OutputDebugString(PAnsiChar(IntToStr(GetOpenHandles)));
If you are running out of handles, it can only mean one thing: you are allocating them and not releasing them. Use a tool such as the FastMM memory manager in full debug mode to give you more information on what is not being freed.
To avoid this kind of thing, you should always use your memory managers in full debug mode when developing...
Update As far as I can determine, FastMM does indeed report memory leaks and not resource handle leaks. The best way to detect handle leaks seems to be the PerfMon tool that is bundled with Windows itself: http://www.codeguru.com/cpp/v-s/debug/memoryissues/article.php/c4411
Samples of how to use the PerfMon tool can be found on MSDN: http://msdn.microsoft.com/en-us/library/aa645516(v=vs.71).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With