Delphi: causes of system error 1158 (more system descriptors for the current process)

After a long shutdown of the application, it usually crashed when I clicked on the form.

I leave it idle every day to find out if I found a mistake, and finally this morning I started using it, and after a few clicks I got:

system error 1158 current process used all system descriptor credentials

So, somehow I use all the resources and, fortunately, this morning I started using the application when it was almost out of resources, and I used it until all the pens were no longer available. While on all other days I was too late and there were no more pens (this is what I suspect). Of course, in this case, the application crashed when I clicked on it.

Now could you offer a solution? I use threads that regularly check for new messages or count how many users are connected to the network. But I checked twice, and I free everything after each thread. (but maybe there is something else that I don’t know about threads). The application is always connected to the database (I use DevArt SDAC), in any case there were no disconnections, because in case of disconnection I have a warning, and I suggest the user to try to reconnect.

+4
source share
3 answers

If you have run out of pens, this can only mean one thing: you distribute them and do not let go. Use a tool such as FastMM memory manager in full debug mode to provide you with more information about what is not being freed.

To avoid this kind of thing, you should always use memory managers in full debug mode when developing ...

Update As far as I can tell, FastMM does report a memory leak, not a resource descriptor leak. The best way to detect descriptor leaks is the PerfMon tool, which comes with Windows itself: http://www.codeguru.com/cpp/vs/debug/memoryissues/article.php/c4411

Examples of using the PerfMon tool can be found on MSDN: http://msdn.microsoft.com/en-us/library/aa645516(v=vs.71).aspx

+3
source

You can write a function to control the open handles for your application using the GetProcessHandleCount function, and then debug the application and display 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 this method

  OutputDebugString(PAnsiChar(IntToStr(GetOpenHandles))); Task1; OutputDebugString(PAnsiChar(IntToStr(GetOpenHandles))); Task2; OutputDebugString(PAnsiChar(IntToStr(GetOpenHandles))); Task3; OutputDebugString(PAnsiChar(IntToStr(GetOpenHandles))); 
+6
source

You will definitely miss the pens somewhere. Have you controlled the use of the descriptor using the task manager? This may let you know when a leak occurs.

Try disabling portions of the application to see when leaks occur and when they do not, so you can narrow down to the part of the code with the problem.

+2
source

Source: https://habr.com/ru/post/1339326/


All Articles