SFML community forums

Help => General => Topic started by: Acrobat on December 11, 2012, 04:21:09 pm

Title: SFML and GC on Mac
Post by: Acrobat on December 11, 2012, 04:21:09 pm
MacOS only
With GC (Garbage collector) supported/Required in application. You can't get a valid sf::Context in other created thread => can't load resources in threads and other stuff, how to fix this ? Get error :
Quote
invalid context
Error. Unable to create the context.

and when i try to load texture :
Quote
Failed to create texture, its internal size is too high (512x256, maximum is 1x1)

With unsupported GC everything works.
Title: Re: SFML and GC on Mac
Post by: Hiura on December 12, 2012, 09:28:02 am
As I said in the other thread, Apple's doc on GC might give hints on how to solve this.

(I've currently not the time to dig into this. Also, GC is deprecated so the easiest way to go should be to not use it.)
Title: Re: SFML and GC on Mac
Post by: Acrobat on December 19, 2012, 01:06:54 pm
Bumping this thread, look like all NSObjects that are owned by native classes are destroyed by Garbage Collector.
Title: Re: SFML and GC on Mac
Post by: Acrobat on December 19, 2012, 02:33:22 pm
I think i figured it out, how to solve this issue :

// Owner.h

#import <Foundation/Foundation.h>

@class NSOpenGLContext;

@interface Owner : NSObject
+(void)addContext: (NSOpenGLContext *)context;
+(void)delContext: (NSOpenGLContext *)context;
@end

// Owner.m

#import "Owner.h"

static NSMutableArray * contexts = nil;

@implementation Owner

+(void)addContext: (NSOpenGLContext *)context
{
    if (!contexts)
        contexts = [[NSMutableArray alloc] init];
    [contexts addObject:[context retain]];
}

+(void)delContext: (NSOpenGLContext *)context
{
    if (contexts) {
        [contexts removeObject:context];
        if ([contexts count] == 0)
            [contexts release];
    }
}

@end
 

In SFContex.mm after creating of Context add:
[Owner addContext:m_context];
 

in destructor after releasing m_context :
[Owner delContext:m_context];
 

Let me know what do you think about this solution
Title: Re: SFML and GC on Mac
Post by: Hiura on December 20, 2012, 05:14:24 pm
Hum... Interesting. If your piece of code indeed solve the problem (I didn't test it) it would mean that there is something wrong with the retain count in my code. I quickly went through it again but couldn't find the problem.. I'll have a deader look in a few weeks when I'm on holiday.

BTW, are you sure your code doesn't leak ? because this line  [contexts addObject:[context retain]]; will increment by two the retain count..
Title: Re: SFML and GC on Mac
Post by: Acrobat on December 21, 2012, 09:52:57 am
The problem with Garbage Collector + mixing ObjC with C++ is that native class can't own ObjC object, so you can retain thousands times, and it will not do anything. (When you start simple app, sfml creates 3 contexts, and then GC collects 2 of 3, just store them).
P.S. there also something with input delegate (only when you use sfml window in NSView), i just commented it out for my screensaver. (no problems in simple applications)

Quote
BTW, are you sure your code doesn't leak ? because this line  [contexts addObject:[context retain]]; will increment by two the retain count..
I'm not sure, profiller crashes  :(
Title: Re: SFML and GC on Mac
Post by: Hiura on December 25, 2012, 07:48:36 pm
Quote
The problem with Garbage Collector + mixing ObjC with C++ is that native class can't own ObjC object
Where did you find this information ? I couldn't find any good literature about this with some quick google search..

Quote
P.S. there also something with input delegate (only when you use sfml window in NSView), i just commented it out for my screensaver. (no problems in simple applications)
Can you elaborate a little bit ?

Quote
I'm not sure, profiller crashes
the profiler and not your app ? what happen when you use a debugger on your app ?
Title: Re: SFML and GC on Mac
Post by: Acrobat on December 26, 2012, 10:23:26 am
Quote
The problem with Garbage Collector + mixing ObjC with C++ is that native class can't own ObjC object
Where did you find this information ? I couldn't find any good literature about this with some quick google search..

It was just random post somewhere deep in net (i can't find it anymore, but it works).

Quote
P.S. there also something with input delegate (only when you use sfml window in NSView), i just commented it out for my screensaver. (no problems in simple applications)
Can you elaborate a little bit ?

During working on screensaver,  there was random crashes inside sfml on mouse move event (in WindowImplCocoa::mouseMovedIn(void)). So i just commented line
[m_delegate setRequesterTo:this];
in constructor that takes WindowHandle. ScreenSaverView handle it automatically (just do drawings). Did not tested it in simple NSView, but there is no problem in simple SFML applications.

Quote
I'm not sure, profiller crashes
the profiler and not your app ? what happen when you use a debugger on your app ?
This is something strange, GC Monitor crashes after 10 sec running (every time), but it helped me to solve the first issue.
P.S. I configured the environment to debug screensaver, but i still can't do anything on breakpoint in IDE, i can see local values and call stack.
Title: Re: SFML and GC on Mac
Post by: Hiura on December 27, 2012, 05:40:24 pm
Quote
It was just random post somewhere deep in net (i can't find it anymore, but it works).
Well, without official statement it would be better if we thing another solution.

Can you try to use -[NSGarbageCollector (https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSGarbageCollector_class/Introduction/Introduction.html) disableCollectorForPointer:] on line 212 of SFContext.mm (https://github.com/SFML/SFML/blob/master/src/SFML/Window/OSX/SFContext.mm#L212ext.mm#L212) and -[NSGarbageCollector enableCollectorForPointer:] on line 100 (https://github.com/SFML/SFML/blob/master/src/SFML/Window/OSX/SFContext.mm#L100) ?

Regarding crash of [m_delegate setRequesterTo:this];, could you post a stacktrace and some information ? I can't remove this line of code because it will break more stuff if SFML is used in Qt, Wx or Cocoa.
Title: Re: SFML and GC on Mac
Post by: Acrobat on January 04, 2013, 04:09:19 pm
Yes.
[[NSGarbageCollector defaultCollector] disableCollectorForPointer:pointer]
 
worked (i did this for all ObjC pointers)
don't forget to use NSAutoreleasPool in main function
Title: Re: SFML and GC on Mac
Post by: Hiura on January 04, 2013, 04:36:27 pm
nice to hear that. could you send me your patch ?

Quote
don't forget to use NSAutoreleasPool in main function
what do you mean exactly ? there is no "main" function in SFML's code.
Title: Re: SFML and GC on Mac
Post by: Hiura on January 19, 2013, 01:10:24 am
Up, I guess..

I'll probably have some free time next week so if you can give me your patch I can test it.