Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: causes of system error 1158 (no more system handles for current process)

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.

like image 382
LaBracca Avatar asked Feb 11 '11 08:02

LaBracca


2 Answers

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)));
like image 141
RRUZ Avatar answered Sep 23 '22 00:09

RRUZ


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

like image 35
Marjan Venema Avatar answered Sep 27 '22 00:09

Marjan Venema