Overriding global new and delete is not working.

We developing Native App with C++17 for iOS. We override global new and delete operators. This App deallocate all allocated memories correctly by Run on Xcode (Command + R), but exception occurs launch from xcrun or App icon on iPhone.

I debugged the exception. Overriding new operation was called correctly, but overriding delete operation was not called. The default delete was called. I'm not sure why is that.

STEPS TO REPRODUCE

  1. Build xcode project.
  2. Run "xcrun devicectl device install app --device <device-id> <path-to-app>"
  3. Run "xcrun devicectl device process launch --console --device <device-id> <bundle-id>"

PLATFORM AND VERSION iOS Development environment: Xcode 16.4, macOS macOS Sequoia 15.5 Run-time configuration: iOS 18.5

main.cpp I attached is sample code to reproduce this problem.

Answered by DTS Engineer in 854569022
The same source code is included in the Xcode project attached to the email I sent

Got it. Thanks.

I ran your project and was immediately able to reproduce the problem. Specifically:

  • I’m using Xcode 16.4 targeting the iOS 18.5 simulator.
  • I ran the app from Xcode and it exited as expected.
  • I then ran the same app from the simulator’s Home screen and it crashed.

Here’s the backtrace from the resulting crash report:

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib   … __pthread_kill + 8
1 libsystem_pthread.dylib  … pthread_kill + 264
2 libsystem_c.dylib        … abort + 100
3 libsystem_malloc.dylib   … malloc_vreport + 896
4 libsystem_malloc.dylib   … malloc_report + 60
5 libsystem_malloc.dylib   … ___BUG_IN_CLIENT_OF_LIBMALLOC_POINTER_BEING_FREED_WAS_NOT_ALLOCATED + 28
6 cccccccccccc.debug.dylib … __debug_main_executable_dylib_entry_point + 268 (main.cpp:103)
7 ???                      … ???
8 dyld                     … start + 6076

Frame 6 is interesting because it shows that your code is using a debug dynamic library. It’s easy to imagine how this might interfere with the weak definition mechanism uses by a global operator new override [1]. So, I turned off the Enable Debug Dylib Support build setting and retested. This time the app didn’t crash when I ran it from the Home screen.

I think it’s worth you filing a bug about this. Either the debug dynamic library support should support global operator new overrides correctly, or our tools should do something to warn you about this setup being not supported.

Please post your bug number, just for the record.

As to the workaround, turning off the Enable Debug Dylib Support build setting seems like a reasonable option. Please try it out and let me know how you get along.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] For more on weak definitions, and how they are different from weak imports, see my Understanding Mach-O Symbols post.

Unfortunately your main.cpp attachment got munged by the forums, so I can’t try this for myself. Please upload the file to a file sharing service and then post the link here.

ps For advice on how to post links, see tip 14 of Quinn’s Top Ten DevForums Tips.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Due to our internal company rules, I cannot upload the file to a file-sharing service at this time.

The same source code is included in the Xcode project attached to the email I sent, so could you please try using that instead?

Thank you for your understanding.

The same source code is included in the Xcode project attached to the email I sent

Got it. Thanks.

I ran your project and was immediately able to reproduce the problem. Specifically:

  • I’m using Xcode 16.4 targeting the iOS 18.5 simulator.
  • I ran the app from Xcode and it exited as expected.
  • I then ran the same app from the simulator’s Home screen and it crashed.

Here’s the backtrace from the resulting crash report:

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib   … __pthread_kill + 8
1 libsystem_pthread.dylib  … pthread_kill + 264
2 libsystem_c.dylib        … abort + 100
3 libsystem_malloc.dylib   … malloc_vreport + 896
4 libsystem_malloc.dylib   … malloc_report + 60
5 libsystem_malloc.dylib   … ___BUG_IN_CLIENT_OF_LIBMALLOC_POINTER_BEING_FREED_WAS_NOT_ALLOCATED + 28
6 cccccccccccc.debug.dylib … __debug_main_executable_dylib_entry_point + 268 (main.cpp:103)
7 ???                      … ???
8 dyld                     … start + 6076

Frame 6 is interesting because it shows that your code is using a debug dynamic library. It’s easy to imagine how this might interfere with the weak definition mechanism uses by a global operator new override [1]. So, I turned off the Enable Debug Dylib Support build setting and retested. This time the app didn’t crash when I ran it from the Home screen.

I think it’s worth you filing a bug about this. Either the debug dynamic library support should support global operator new overrides correctly, or our tools should do something to warn you about this setup being not supported.

Please post your bug number, just for the record.

As to the workaround, turning off the Enable Debug Dylib Support build setting seems like a reasonable option. Please try it out and let me know how you get along.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] For more on weak definitions, and how they are different from weak imports, see my Understanding Mach-O Symbols post.

Thank you for replying.

I'll try turning off the "Enable Debug Dylib Support", and post bug number after that.

Overriding global new and delete is not working.
 
 
Q