Board index » delphi » Animated cursors and resource leak

Animated cursors and resource leak

Hello everyone.

Up to now i knew that in order to change the cursor of my application
while a lengthy operation is running without using the cursor support
in Graphics.pas i had to do this:

Windows.SetCursor(LoadCursor(hInstance, 'Some_Cursor'));
try
  <Perform the operation>
finally
  Windows.SetCursor(LoadCursor(0, IDC_ARROW));
end;

MSDN States that "The LoadCursor function loads the cursor resource
only if it has not been loaded; otherwise, it retrieves the handle to
the existing resource", therefore the above code should not cause a
resource leak. However when i tried this code with an animated cursor
the Windows Task Manager showed me that i had introduced a resource
leak because the "GDI Objects" counter would keep increasing everytime
a LoadCursor() was executing. This leak does not happen if i use a
non-animated cursor.

Anyone knows why this happens? Thank you in advance.

PS: Please don't tell me how to fix it using DestroyCursor() or loading
the animated cursor once in an initialization section; i already know
this, i just want to know *why* the leak is there.

 

Re:Animated cursors and resource leak


I have already encountered this issue, and assumed the MS documentation
to be erroneous for custom cursors (or maybe cursors loaded by name,
system cursors seem fine).
If you lookup the latest version at MSDN, you'll see that it is said to
having been superseded by LoadImage, and for LoadImage, you have to Destroy
what you loaded.

Eric

Re:Animated cursors and resource leak


Quote
Eric Grange wrote:
> I have already encountered this issue, and assumed the MS
> documentation to be erroneous for custom cursors (or maybe cursors
> loaded by name, system cursors seem fine).
> If you lookup the latest version at MSDN, you'll see that it is said
> to having been superseded by LoadImage, and for LoadImage, you have
> to Destroy what you loaded.

Yes, but LoadImage() has the LR_SHARED flag that is supposed to make
the GDI layer reuse an already loaded resource. I don't know if it
works with animated cursors, i just wished that MSDN was more clear at
that point about animated cursors so that we wouldn't have to use
...deductive reasoning <g>

Other Threads