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.