Repository: KhronosGroup/MoltenVK

Issue #2399: memory management issue in MVKSwapchain::setHDRMetadataEXT
Created: 2024-12-01T04:23:14Z
MVK version 1.2.12, supporting Vulkan version 1.2.296
Build from 26c7b02

mpv community has reported an issue causing program to crash, after investigation, was found to be caused by a memory management issue in MVKSwapchain::setHDRMetadataEXT. mpv-player/mpv#12666 (comment)
According to README:

As a contributor to MoltenVK, avoid the use of the Metal autorelease method, or any object Metal creation
methods that imply internal use of autorelease, (eg- [NSString stringWithFormat: ], etc).


  
    
      MoltenVK/MoltenVK/MoltenVK/GPUObjects/MVKSwapchain.mm
    
    
        Lines 359 to 360
      in
      26c7b02
    
  
  
    

        
          
           NSData* colorVolData = [NSData dataWithBytes: &colorVol length: sizeof(colorVol)]; 
        

        
          
           NSData* lightLevelData = [NSData dataWithBytes: &lightLevel length: sizeof(lightLevel)]; 
        
    
  


However in MVKSwapchain::setHDRMetadataEXT NSData was assigned with type dataWithBytes, which is implied with internal use of autorelease:
https://github.com/apportable/Foundation/blob/master/System/Foundation/src/NSData.m#L744-L747
+ (id)dataWithBytes:(const void *)bytes length:(NSUInteger)length

{
    return [[[self alloc] initWithBytes:(void *)bytes length:length copy:YES freeWhenDone:NO bytesAreVM:NO] autorelease];
}

For manually track and release, it should be
NSData* colorVolData = [[NSData alloc] initWithBytes: &colorVol length: sizeof(colorVol)];

Duplicate issue #2488: Crash when using vkSetHdrMetadataEXT() under my Vulkan implementation on FLTK.
Created: 2025-04-18T10:27:56Z
I am adding Vulkan support to FLTK.  It has been working fine, but I ran into an incompatibility between MoltenVK and FLTK's Cocoa.  Basically, trying to use vkSetHdrMetadataEXT() leads to a crash, on this FLTK's function:
double Fl_Darwin_System_Driver::wait(double time_to_wait)
{
if (dropped_files_list) { // when the list of dropped files is not empty, open one and remove it from list
drain_dropped_files_list();
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
time_to_wait = Fl_System_Driver::wait(time_to_wait);
// the deprecation warnings can be ignored because they run only for macOS < 10.11
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if (fl_mac_os_version < 101100) NSDisableScreenUpdates(); // deprecated 10.11
Fl::flush();
if (fl_mac_os_version < 101100) NSEnableScreenUpdates(); // deprecated 10.11
#pragma clang diagnostic pop
if (Fl::idle) // 'idle' may have been set within flush()
time_to_wait = 0.0;
int retval = do_queued_events(time_to_wait);
Fl_Cocoa_Window_Driver::q_release_context();
[pool release];   /// <---------- THIS CRASHES when vkSetHdrMetadataEXT is used.
return retval;
}
Any ideas how to fix it?  For more info and for an FLTK vk_shape_hdr-shared program that shows the issue:
fltk/fltk#1250

Closing comment (2025-04-22T16:23:35Z): When looking on whether a new release was out or not, I noticed your issue which sounded very similar to one that's fixed and waiting on a release.  You may want to try building main and see if that resolves your issue.
I was encountering similar in MPV when it sets HDR metadata and that was reported in #2399 which had a pair of PRs to fix.  So it should work on main but the fix is not present in any release yet.