EXC_BAD_ACCESS crashes when loading a texture on MacOS

Hi there,
I’m throwing a bottle at the sea about an issue that’s been blocking us for a few days.

A crash when loading textures

We’re developing the MacOS version of a tool that uses the C SDK. It’s nearly ready for release but we’re suffering from crashes when loading a texture.

This happens when calling SUImageRepLoadFile (same thing happens with SUTextureCreateFromFile). Here’s some debug code with a stack trace that ends in the FreeImage_Validate function.

Given the EXC_BAD_ACCESS and the semi-random nature of the crash (happens ~ half the time), we suspected that we’re suffering from undefined behaviours due to some unholy memory usage from our side.

However, the last few days we tried the following steps, to no avail:

  • Reduced the code to a minimum (removed any parallelism and only left the essential parts).
  • Used some analysis tools (XCode static analysis + runtime instrumentation tools) to check for undefined behaviours & invalid memory usage but there seems to be nothing wrong on that side.

Also, this tool has had a Windows version for 10 months with no such issues.

So maybe the crash comes from elsewhere?

libstdc++ vs libc++

The SDL documentation does mention that:

SketchUp 2017 for OS X is built using libstdc++, so plugins should be built with the same library. Using libc++ will likely cause the plugin to fail to load and cause SketchUp to crash."

Since we’re using XCode 11, we use libc++. One important detail is that our tool is a standalone application that uses the SketchUp SDK, not a plugin. However, at this point, the difference in C++ library seems worth investigating.

XCode does allow to switch to a deprecated version of libstdc++ but it does not support C++11 and later. This is a big issue for us as we have a lot of code that uses modern features of the language. Even if we instead used Boost to support some of those features, we have third-party dependencies that need C++11 and that do not allow that.

My setup:

  • MacOS Catalina 10.15.1
  • Xcode 11.2.1 (also tried with 11.1.0)
  • SketchUP SDK 2019

In summary

  1. Has anyone experienced a similar issue, EXC_BAD_ACCESS, when manipulating textures?
  2. Any thoughts about libc++/libstdc++ on MacOS when developing a standalone application?

I’ll continue fiddling with our code to look for bad stuff on our side, but in the meantime any insights about those two points could be very precious!

Thanks

1 Like

I hope you realize that the SketchUp office is in Colorado, and doesn’t have any coasts? But, @tt_su lives near the sea I think, and may have ideas.

@colin I know I should have sent you guys a pigeon instead :wink:

Actually, we found the cause of our crashes. It’s as simple as: the SketchUp SDK was not properly initialized, shame on us!

(Strangely, it causes no issues on Windows though. And on MacOS, only when manipulating textures)


The long version, because it’s a bit funny

We have a C++ class that represents a work session. It looks like this:

class Session
{
    Session()
    {
         SUInitialize();
         // ...
    }

    ~Session()
    {
         // ...
         SUTerminate();
    }

    // ...
};

It seems reasonnable to initialize/terminate the SDK in the constructor/destructor of this class because there is always only a single Session active in our app, right?

Ahem, when we reset the state of the app. We do something like this:

// SessionPtr is a unique_ptr
sessionPtr.reset(new Session());

If we decompose what happens:

  • First, new Session() leads to SUInitialize()
  • Then, sessionPtr.reset() leads to the old session getting destructed, which leads to SUTerminate()

Oops.

2 Likes

We’re at more than 1 mile above sea level, an albatross might have been better. Glad you figure it out.

Thanks for the follow up on why you were seeing these issues.
(Though strange it doesn’t crash on Windows…)