Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: OSX Crash in pollEvent()  (Read 2586 times)

0 Members and 1 Guest are viewing this topic.

j.keller51

  • Newbie
  • *
  • Posts: 17
    • View Profile
OSX Crash in pollEvent()
« on: April 03, 2019, 06:31:53 pm »
Hi,

I am experiencing a crash in pollEvent() on Mac OSX using SFML 2.5.1.

When I create the window using Window::create(VideoMode mode, String title), there are no crashes.

However, for my application I have to give SFML an NSView * that is already created, so I use Window::create(WindowHandle handle).

Traceback is pollEvent()->popEvent()->processEvents()->drainThreadPool->destroyPool()->[pool drain];

Here is the code where the window is opened:

void ZivixGraphicsMacVST::OpenWindow(void * parent) {
    m_cocoaGraphics = (ZIVIXGRAPHICS_COCOA *) [[ZIVIXGRAPHICS_COCOA alloc] initWithZivixGraphics: this];
    setWindow(m_cocoaGraphics);

    if (parent) {
        [(NSView *) parent addSubView: (ZIVIXGRAPHICS_COCOA *) m_cocoaGraphics];
    }
}

setWindow():
void setWindow(void * w) {
    m_Window = new sf::RenderWindow(w, m_sfmlSettings);
    m_Window->setFramerateLimit(100);
}

And the update function up to the point of the crash:

                sf::Vector2i mpos = sf::Mouse::getPosition(*m_Window);
                mouseMove(mpos.x, mpos.y);

                sf::Event event;
                while (getWindow()->pollEvent(event)) { //<<<< CRASH
                        switch (event.type) {




Other code that may be useful in diagnosing the problem:
@interface ZIVIXGRAPHICS_COCOA : NSView {
    NSTimer * m_Timer;
    @public
    ZivixGraphics::ZivixGraphicsMac * m_GFX;
   
}
- (id) init;
- (id) initWithZivixGraphics: (ZivixGraphics::ZivixGraphicsMac *) gfx;
- (void) onTimer: (NSTimer *) pTimer;
@end

@implementation ZIVIXGRAPHICS_COCOA
- (id) init {
    m_GFX = 0;
   
    return self;
}

- (id) initWithZivixGraphics:(ZivixGraphics::ZivixGraphicsMac *)gfx {
    m_GFX = gfx;
    NSRect r;
    r.origin.x = 0.0f;
    r.origin.y = 0.0f;
    r.size.width = (float) gfx->getWidth();
    r.size.height = (float) gfx->getHeight();
    self = [super initWithFrame:r];
   
    // init timer
    double sec = 1/100;
    m_Timer = [NSTimer timerWithTimeInterval:sec target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer: m_Timer forMode: (NSString*) kCFRunLoopCommonModes];
   
   
    return self;
}

- (void) onTimer: (NSTimer*) pTimer {
    if (pTimer == m_Timer)
        m_GFX->update();
}
@end

Any suggestions?

Thank you,
Jon

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: OSX Crash in pollEvent()
« Reply #1 on: April 04, 2019, 12:17:31 am »
This sounds kind of similar to this: https://github.com/SFML/SFML/issues/1549

What do you think?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

j.keller51

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: OSX Crash in pollEvent()
« Reply #2 on: April 04, 2019, 06:18:20 pm »
It does look similar, there appears to be some memory "leak" in SFML on Mac. I ran a barebones example and observed the memory using Xcode's Instruments. It didn't show any leaks, however heap allocations increased by about 1 MB/s. I let it run for a while and when I came back it was up to 3 GB's allocated!

This was the code I was running for this test:
int main() {
    sf::RenderWindow window(sf::VideoMode(640,480), "Test App");
   
    while (window.isOpen()) {
        sf::Event event;
       
        // handle events
        while (window.pollEvent(event)) {
            switch (event.type) {
                case sf::Event::Closed:
                    window.close();
                    break;
                   
            }
        }
       
        window.clear(sf::Color(240,240,240,255));
       
        sf::CircleShape circ(150, 100);
        circ.setPosition(100,100);
        circ.setFillColor(sf::Color(80,0,0,255));
        window.draw(circ);
       
        window.display();
    }
   
    return 0;
}

I think this has something to do with my crash problem, but I'm struggling to see what changes I can make to fix it. The thread expl0it3r linked leads to this fix for bgfx: https://github.com/attilaz/bgfx/commit/e5887de9472fe5e50a947ff9c3297a41972dc83b

But their application is a lot different from mine. should I wrap
while (window.pollEvent(event)) { ... }
with
@autoreleasepool { ... }
?