0

I am working on cross-platform graphics library which for iOS uses Metal API. Core of the library is in C++, then I'm implementing some derived classes in Objective C++ (.mm) code.

From my derived class I'm calling a swift static function which returns some object to me.

It worked initially, after some days I'm getting following error all of a sudden.

// MetalLibrary.mm

void FooLibrary::CreateNewLibrary(const std::string &source) {
   this.library = [SwiftHelper_MetalLibrary createNewLibraryWithSource: [NSString stringWithUTF8String: source.c_str()]];
}

I've created a mapper header which will define ObjC interface for swift equivalent classes during compile time ref: How to access an internal Swift class in Objective-C within the same framework

// Mapper.h

SWIFT_CLASS("SwiftHelper_MetalLibrary_OBJC")
@interface SwiftHelper_MetalLibrary : NSObject

+ (id<MTLLibrary> _Nonnull) createNewLibraryWithSource: (NSString* _Nonnull) source; 

@end
}

Swift equivalent helper class

// SwiftHelper_MetalLibrary.swift

@objc(SwiftHelper_MetalLibrary_OBJC)
internal class SwiftHelper_MetalLibrary: NSObject {
    @objc internal class func createNewLibraryWithSource(_ source: String) -> MTLLibrary {
        // Some custom logic
        // CRASHES HERE with ERROR:
        // "heap corruption detected, free list is damaged at <ADDRESS>"
    }
}

There is no specially logic here, I just create a library from gpu instance (which I get as a param, not shown in eg) MTLDevice instance is not nil. ALso this happens randomly with any of my Swift methods.

Any idea what could be the reason? Tried Address Sanitizer but it shows crashes somewhere else altoghether.

ImShrey
  • 380
  • 4
  • 12
  • This is not enough information to diagnose this problem. – JustSomeGuy Mar 08 '22 at 19:07
  • And just to get this straight, you are wrapping Swift Metal calls in C++ using `@objc` Swift bridging? Why not just use Objective-C or just Metal-cpp https://developer.apple.com/metal/cpp/ – JustSomeGuy Mar 08 '22 at 19:10
  • Because eventually I'm targeting to make this modular so that we can reuse the all the swift classes in any new libraries that may or may not be in C++/cross platform. Plus, easier for maintenance. – ImShrey Mar 09 '22 at 06:05

0 Answers0