Skip to content

[SR-426] Memory leak with enum containing class, downcast to Any.Type #43043

@swift-ci

Description

@swift-ci
Previous ID SR-426
Radar None
Original Reporter austin (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Building on OS X 10.11.2, revision 73872e5.

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug, NotOptimizedOnly, Runtime
Assignee jder (JIRA)
Priority Medium

md5: 66dcf74255f01d0bc1c3c7eded17fdfe

blocks:

  • SR-88 Remove old mirrors

Issue Description:

The following code appears to produce a memory leak. When it is run, the relevant class's `deinit` is never called:

class BasicClass {
    let a : Int
    init(_ a: Int) {
        print("BasicClass being init'ed with value \(a)")
        self.a = a
    }

    deinit {
        print("BasicClass being deinit'ed with value \(a)")
    }
}

enum Bar<T> {
    case First(T)
}

enum Baz<T, U> {
    case First(T), Second(U), Third, Fourth
}

func doSomething() {
    let myThing : Any = Bar.First(BasicClass(10))
    // bad behavior also happens with multi-payload generic enums
    // bad behavior also happens with a tuple, like "(1, 2, BasicClass(10))"
    // bad behavior does not happen with just a bare "BasicClass(10)"

    // A: 
    if let foo = myThing as? Any.Type {
        print("myThing is a metatype")
    }
}

doSomething()

In particular, the thing that causes trouble is the conditional downcast to Any.Type (marked by the comment 'A'). If the if-let statement is removed, the init and deinit run properly. If the if-let statement is present, only the init runs properly.

A downcast to a non-metatype seems to work fine. I have not tried downcasting to other metatypes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.non-optimized onlyFlag: An issue whose reproduction requires non-optimized compilationruntimeThe Swift Runtimestandard libraryArea: Standard library umbrella

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions