Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-1330] Memory Leaked when access a OC-Bridged Instance property from Swift Class #43938

Open
swift-ci opened this issue Apr 27, 2016 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself memory leak bug: Memory leak

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1330
Radar None
Original Reporter chenniaoc (JIRA User)
Type Bug
Environment

Swift2.2 Xcode 7.3

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Leak
Assignee None
Priority Medium

md5: cc8e7706fd0f2c6112bb7ced3b19949e

Issue Description:

I'm migrating a module using swift which wrote by Objc before.
But sadly there was some history problem that I can not write pure-swift language for the module,for the reason that some other modules was too complicated to rewrite.So I try to mixed objc and swift in the new module.

Conditions to leak:
1.A swift class that has a instance variable which type is AnyObject, or a Objc class with type id.
2.a function or method that try to access any property on the instance of AnyObject or id.
result:
3.A objc class that declare a field with the name step(2) accessed.

the property accessed in step2 will be retain+1,and never be released after. So the object leaked.

class SSRootClass {
    
    var anyData:AnyObject!
    
    func helloAnyData() {
        
        print("this object will be leaked:\(anyData.subdata)")
              
    }

}

subdata is a property on a Objc-bridged class,like:

@interface SimpleData : NSObject
@property (nonatomic, strong) SimpleData *subdata;
@end

So the compiler will not show any error.In other word,you can write any property accessing code which was belong to any objc-bridged class.
Such as,the length property on NSString. So you can write without compiler error:

anydata.length

I think this situation is similar to the [NSObject performSelector] call,
When you try to perform a selector on some object,compiler will give you a potential memory leak warning.ARC-auto-inserted can not work well in the compile phase,because of the selector maybe return a object or not.So the compiler will not release the returned value on [performSelector] call.

Suggestion:

  1. it's better to give a warning message in compile phase
    or
    2.auto insert arc code in compile phase.
@belkadan
Copy link
Contributor

@swiftix, we fixed this, right?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself memory leak bug: Memory leak
Projects
None yet
Development

No branches or pull requests

2 participants