# iOS shipping build crashes at std::thread exit

**URL:** <https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140>\
**Category:** Mobile\
**Tags:** Shipping, unreal-engine, crash, iOS, CPP, question\
**Created:** [May 31, 2017, 11:53am UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140 "2017-05-31T11:53:37Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_ca1e55b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ca1e55b1](https://forums.unrealengine.com/u/anonymous_user_ca1e55b1)\
**Post date:** [May 31, 2017, 11:53am UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/1 "2017-05-31T11:53:37Z")

</div>

An otherwise vanilla UE Project with the following GameMode code crashes iOS shipping builds:

void ANOGSSDKGameModeBase::StartPlay()  
{  
std::thread({  
std::clog \<\< “THREAD @”  
\<\< **FILE** \<\< “:” \<\< **LINE**  
\<\< std::endl;  
}).join();  
}

call-stack:

> 0 0x00000001922c74bc in \_\_abort ()  
> 1 0x00000001922c740c in abort ()  
> 2 0x0000000192389944 in free ()  
> 3 0x0000000192417f70 in \_pthread\_tsd\_cleanup ()  
> 4 0x0000000192417ce4 in \_pthread\_exit ()  
> 5 0x000000019241985c in \_pthread\_body ()  
> 6 0x0000000192419760 in \_pthread\_start ()  
> 7 0x0000000192416d94 in thread\_start ()

Output:

> 2017-05-30 11:34:13.096921 NOGSSDK[5085:2258376] [DYMTLInitPlatform] platform initialization successful  
> 2017-05-30 11:34:13.626832 NOGSSDK[5085:2258248] Found ue4commandline.txt file  
> 2017-05-30 11:34:13.627670 NOGSSDK[5085:2258248] Combined iOS Commandline: …/…/…/NOGSSDK/NOGSSDK.uproject /Users/beschulz/Documents/Unreal Projects/NOGSSDK/NOGSSDK.uproject -RunConfig=  
> ========= This app is in LANDSCAPE mode  
> 2017-05-30 11:34:13.975549 NOGSSDK[5085:2258403] Project file not found: …/…/…/NOGSSDK/NOGSSDK.uproject  
> 2017-05-30 11:34:14.458866 NOGSSDK[5085:2258248] Metal GPU Frame Capture Enabled  
> 2017-05-30 11:34:14.459737 NOGSSDK[5085:2258248] Metal API Validation Enabled  
> 2017-05-30 11:34:14.500939 NOGSSDK[5085:2258248] ::: Created a UIView that will support Metal :::  
> 2017-05-30 11:34:15.726559 NOGSSDK[5085:2258403] Initializing ULD Communications in game mode  
> THREAD @/Users/beschulz/Documents/Unreal Projects/NOGSSDK/Source/NOGSSDK/NOGSSDKGameModeBase.cpp:21  
> NOGSSDK(5085,0x16e9ff000) malloc: \*\*\* error for object 0x108e5e050: pointer being freed was not allocated  
> \*\*\* set a breakpoint in malloc\_error\_break to debug

Reproduction:

create new vanilla C++ project

- add the code above to StartPlay of the C++ GameMode
- Select your GameMode and an empty default level as the startup map/mode in Maps&Modes
- Set Build configuration to Shipping
- Set-up iOS signing (a development profile will do!)
- package for iOS
- open Intermediate/ProjectFilesIOS/MyProject.xcodeproj
- Press the play button in Xcode

The project files for reproduction are also attached (click).

Remarks:  
The crash does not manifest itself when using a plain pthread, at least not in this exact scenario. It also does not happen in debug builds or when debug symbols are enabled (via patching IOSToolchain.cs). I observed the issue when narrowing down a crash in a somewhat more complex scenario. The crash I narrowed down appears in 4.14, 1.15 and 4.16 - but I’ve only tried the reduced case with 4.16. I’ve noticed, that the stack-size of the game-thread is limited to 1MB in shipping builds while it is 8 MB is testing/debug builds. It does not matter if I log in the thread or not.

There is also this report, that may be related: [Crashes on thread exit in shared library built with libc++ - Programming & Scripting - Epic Developer Community Forums](https://answers.unrealengine.com/questions/536789/external-shared-library-with-libc-threads-crashes.html)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ca1e55b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ca1e55b1](https://forums.unrealengine.com/u/anonymous_user_ca1e55b1)\
**Post date:** [May 31, 2017, 2:29pm UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/2 "2017-05-31T14:29:36Z")

</div>

Update: for what it’s worth, I’m using Xcode 8.3.2 on iOS 10.2.1. And the crash occurs with plain pthreads in my unisolated scenario as well.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [May 31, 2017, 6:25pm UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/3 "2017-05-31T18:25:29Z")

</div>

I’ve never seen anyone trying to open/run the .xcodeproj from inside the Intermediates folder before and I’m not sure if this is something we support even doing. What is supposed to be the point of doing this? Do you still get the crash if you install the game to the iOS device normally?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ca1e55b1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ca1e55b1](https://forums.unrealengine.com/u/anonymous_user_ca1e55b1)\
**Post date:** [May 31, 2017, 6:29pm UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/4 "2017-05-31T18:29:18Z")

</div>

Yes, it also crashes when I install the .ipa on the device and launch it by touching. Using the xcode project this way is a nice way to start the .ipa on the device while having a debugger attached from the start. The crash has nothing to do with the start method.  
Edit: thanks for looking into this!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [June 2, 2017, 6:48pm UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/5 "2017-06-02T18:48:58Z")

</div>

Thank you for the additional information. I’ve placed a bug report in for the issue which can be found here: [UE-45650](https://issues.unrealengine.com/issue/UE-45650) You can use the provided link to track the bug’s progress. Unfortunately I can’t think of a workaround at the moment aside from taking out the code causing this.

Have a nice day!

---

<div class="post-metadata">

**Author:** ![david.noorgames](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@david.noorgames](https://forums.unrealengine.com/u/david.noorgames)\
**Post date:** [October 17, 2017, 1:24pm UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/6 "2017-10-17T13:24:06Z")

</div>

Hi Matthew. Is this bug fixed or still present on engine? I am still using 4.16 version. I have a problem with GameSparks integration because of this bug. Just want to know if it’s worth to do an update.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [October 18, 2017, 1:01pm UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/7 "2017-10-18T13:01:32Z")

</div>

Hello ,

Yes, this still occurs on 4.17. I just tested it manually to make sure. In most cases, if a bug is marked as backlogged, it likely still occurs in the latest version, although there are a few times where an issue gets fixed inadvertently.

Have a nice day!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_71da530a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_71da530a](https://forums.unrealengine.com/u/anonymous_user_71da530a)\
**Post date:** [October 19, 2017, 11:02am UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/8 "2017-10-19T11:02:32Z")

</div>

Hi Matthew,

can you or the other Epic Engineers suggest a workaround for this problem? We really need to get our SDK working again on iOS.

Best,

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [October 19, 2017, 5:55pm UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/9 "2017-10-19T17:55:12Z")

</div>

Hello

Speaking with the developer who was assigned to this bug report originally, the apparent workaround is to avoid using std::thread. This is more of a bug on Apple’s end from his explanation, mentioning that it uses CFAllocator which we cannot easily override with our setup.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_71da530a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_71da530a](https://forums.unrealengine.com/u/anonymous_user_71da530a)\
**Post date:** [October 19, 2017, 6:56pm UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/10 "2017-10-19T18:56:31Z")

</div>

Hello Matthew,

I already tried using pthreads directly, but the same crash occurred. It looks like the engine itself is using FRunnableThreadApple on iOS which is a FRunnableThreadPThread. So I wonder why the engine threads would not crash but a pure pthread (or std::thread) would. Or is it to be expected, that those crash at exit as well?

Best,

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [October 19, 2017, 7:54pm UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/11 "2017-10-19T19:54:08Z")

</div>

As you were said, iOS uses it’s foundation’s allocator (CFAllocator) to allocate the thread memory when you are using the STD::THREAD - which Epic team are (I suppose) unlikely to wrap directly,  
take a look at iOS pthread implementation of epic.

I wouldn’t call it an apple bug though since iOS without UE4 supports std::thread and even custom allocators.

However it is best practice to use Epic provided functionality for threading “FRunnableThread”  
and let the engine to wrap it with whatever it wants.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_76fc025f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_76fc025f](https://forums.unrealengine.com/u/anonymous_user_76fc025f)\
**Post date:** [March 17, 2018, 7:34am UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/12 "2018-03-17T07:34:08Z")

</div>

Hey

Has this issue been addressed at all? I’m running into this issue with GameSparks right now in version 4.17.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_cb54c404](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_cb54c404](https://forums.unrealengine.com/u/anonymous_user_cb54c404)\
**Post date:** [April 19, 2018, 8:57am UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/13 "2018-04-19T08:57:08Z")

</div>

Any more news on this?

---

<div class="post-metadata">

**Author:** ![joeGraf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@joeGraf](https://forums.unrealengine.com/u/joeGraf)\
**Post date:** [May 25, 2018, 6:51pm UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/14 "2018-05-25T18:51:37Z")

</div>

There are incompatibilities between std::thread (really all std libs) and our memory allocators. One of our programmers just recently fixed some Fortnite issues by removing std::stringstream usage from a plugin that Fortnite depended upon. It’s possible that replacing the std allocators with implementations that call our allocators might fix the issue, but your best bet is to avoid using std libs in UE4.

---

<div class="post-metadata">

**Author:** ![CppCode](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CppCode](https://forums.unrealengine.com/u/CppCode)\
**Post date:** [September 6, 2018, 5:30am UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/15 "2018-09-06T05:30:20Z")

</div>

Hi, If I use a third party library, such as RakNet which depend on stl, how to make it run on iOS?  
modify the library ? can I make it as a static link library to avoid std::allocator overrided by FMemory ?

---

<div class="post-metadata">

**Author:** ![Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_e5d5c0bf7d300728867097ff79906dbe](https://forums.unrealengine.com/u/Anonymous_e5d5c0bf7d300728867097ff79906dbe)\
**Post date:** [September 6, 2018, 11:12am UTC](https://forums.unrealengine.com/t/ios-shipping-build-crashes-at-std-thread-exit/395140/16 "2018-09-06T11:12:07Z")

</div>

I am not 100% sure, but making it as a static library will most likely solve it.
