从 Objective C 调用 swift 静态函数时由于堆损坏而崩溃(。毫米)
我正在开发跨平台图形库,iOS 使用 Metal API。 该库的核心是 C++,然后我用 Objective C++ (.mm) 代码实现一些派生类。
从我的派生类中,我调用一个快速静态函数,该函数返回一些对象给我。
它最初有效,几天后我突然收到以下错误。
// MetalLibrary.mm
void FooLibrary::CreateNewLibrary(const std::string &source) {
this.library = [SwiftHelper_MetalLibrary createNewLibraryWithSource: [NSString stringWithUTF8String: source.c_str()]];
}
我创建了一个映射器标头,它将在编译时为 swift 等效类定义 ObjC 接口 参考:如何在 Objective 中访问内部 Swift 类-C 在同一框架内
// Mapper.h
SWIFT_CLASS("SwiftHelper_MetalLibrary_OBJC")
@interface SwiftHelper_MetalLibrary : NSObject
+ (id<MTLLibrary> _Nonnull) createNewLibraryWithSource: (NSString* _Nonnull) source;
@end
}
Swift 等效辅助类
// 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>"
}
}
这里没有特殊的逻辑,我只是从 gpu 实例创建一个库(我作为参数获得,未在eg中显示) MTLDevice 实例是不是零。另外,我的任何 Swift 方法都会随机发生这种情况。
知道可能是什么原因吗?尝试过地址消毒剂,但它显示其他地方完全崩溃。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论