Bug with Component::getMouseCursor() Mac OSX

Hello Jules,

First some praise:
I’ve been using JUCE for a year now, love it. It’s quite an impressive feat…

Now the bug:
I want to get the mouse to hide when over this new component I’m working on. So, I tried your handy mouse-setting virtual method:

[code]const MouseCursor MyComponent::getMouseCursor()
{
return MouseCursor::NoCursor; // this is filled with leaky pointer warnings

//return MouseCursor::CrosshairCursor;  // this works perfectly

}[/code]

This in fact does what I want, but the console is filled with scary warnings about a leaking NSImage object:

2009-02-28 14:44:43.553 juce_application[85306:10b] *** _NSAutoreleaseNoPool(): Object 0x856950 of class NSImage autoreleased with no pool in place - just leaking Stack: (0x95318adf 0x952251f2 0x122421 0x1228f0 0x200b6f 0x200c52 0x12df61 0x24825e 0x12e42e 0x1c3a5e 0x1c3b3c 0x1c5d5b 0x1c5e6e 0x1c63dc 0x1c709e 0x1d1935 0x1d1ba1 0x2c03 0x252b 0x2459)

I poked around the source code a bit to see where you are doing the OSX specific mouse stuff. (I am using, Juce v1.46) It looks like to hide the cursor, you are creating a blank NSImage as a cursor.

So I think this is where the problem is:

[code]void* juce_createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY) throw()
{
NSImage* im = juceImageToNSImage (image);
[im autorelease];
NSPoint hs;
hs.x = (int) hotspotX;
hs.y = (int) hotspotY;

NSCursor* c = [[NSCursor alloc] initWithImage: im hotSpot: hs];
return (void*) c;

}[/code]

I believe the proper way to do the memory management here would be:

[code]void* juce_createMouseCursorFromImage (const Image& image, int hotspotX, int hotspotY) throw()
{
NSImage* im = juceImageToNSImage (image);
NSPoint hs;
hs.x = (int) hotspotX;
hs.y = (int) hotspotY;

NSCursor* c = [[NSCursor alloc] initWithImage: im hotSpot: hs];
[im release];
return (void*) c;

}[/code]

I’d do the fix myself (and test if this proposal works) but I can’t seem to figure out how to get my project to load my changes to the JUCE library… I’m puzzled over your new-fangled auto-linking amalgamator… but that’s a different issue. Anyway, I can’t test this fix, but I think it should work because the NSCursor class performs it’s own retain on the im object but you have release it yourself after you pass it to NSCursor. No need for autorelease?

But for the case of hiding the cursor, why not just make a flag to hide the cursor and then use the hide method of NSCursor? http://tinyurl.com/bonzd9
My guess is that it would be a little bit cheaper?

For now, I’m going to go with your working cross-hair cursor cursor type…

Thanks Jules!

Hi there, glad you’re enjoying using juce.

Thanks for spending the time to investigate this and do a nice long, clear bug report, but I’m afraid I already fixed it months ago! Always a good idea to check the tip version before getting too deep into a bug!

Yeah, you’re right. I thought I had the tip version…

Thanks though for the reply.

Aaron