Skip to content

[SR-2371] Run-time crash in ObjC code accessing date added to NSMutableDictionary by Swift code #44978

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

Open
swift-ci opened this issue Aug 17, 2016 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-2371
Radar rdar:///27875914
Original Reporter curt (JIRA User)
Type Bug

Attachment: Download

Environment

Xcode 8b6

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

md5: 277dd98cf382487b488e4fb3c9d336e4

Issue Description:

  1. In the attached code, an NSMutableDictionary instance is created in an Obj-C method, +[OldSchool dictionary], that is called by Swift code, NewSchool.getDict().

The Swift code updates the dictionary to map a String to a Date, then attempts to pass that dictionary back to a C function, stringValueFromDictionary(), that expects an NSDictionary.

The compiler in Xcode 8b6 surfaces the C function as taking an argument of type [AnyHashable: Any], per #116. To provide the right type, we copy the NSMutableDictionary instance and cast it to [AnyHashable: Any].

In the implementation of stringValueFromDictionary(), we retrieve the date from the dictionary, expecting it to be an NSDate object. Calling any NSDate method on the value crashes.

Steps to Reproduce:

Compile and run the attached project.

Expected Results:

The bridging of the [AnyHashable: Any] value in Swift back to NSDictionary in C should bridge the Date object to NSDate.

Actual Results:

Crash:

2016-08-16 15:51:25.232 AnyWhatNow[62058:772408] -[_SwiftValue dateByAddingTimeInterval:]: unrecognized selector sent to instance 0x100d02420
2016-08-16 15:51:25.234 AnyWhatNow[62058:772408] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue dateByAddingTimeInterval:]: unrecognized selector sent to instance 0x100d02420'

      • First throw call stack:
        (
        0 CoreFoundation 0x00007fff8a6064f2 __exceptionPreprocess + 178
        1 libobjc.A.dylib 0x00007fff916c8f7e objc_exception_throw + 48
        2 CoreFoundation 0x00007fff8a6701ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
        3 CoreFoundation 0x00007fff8a576571 _forwarding_ + 1009
        4 CoreFoundation 0x00007fff8a5760f8 _CF_forwarding_prep_0 + 120
        5 AnyWhatNow 0x0000000100001f5b stringValueFromDictionary + 91
        6 AnyWhatNow 0x000000010000228d _TFC10AnyWhatlibc++abi.dylib: terminating with uncaught exception of type NSException

Notes:

It's entirely possible that I'm just "holding it wrong", but I've found no way to insert an date object into a dictionary in Swift and successfully make that date usable in Objective-C code that receives the dictionary.

@swift-ci
Copy link
Contributor Author

Comment by Curt Clifton (JIRA)

Replacing the Date instance in the code with a literal string and trying to call an NSString method on it in the ObjectiveC code crashes in the same way. It appears that the problem is with the boxing, as the value type appears to be a "Swift.Optional<Any>" in the debugger:

(lldb) p (char *)swift_getTypeName((void *)[date _swiftTypeMetadata], 1)
(char *) $2 = 0x0000000100b03920 "Swift.Optional<Any>"

I'm not sure where the optionality is coming from, except in the boxing.

@jckarter
Copy link
Contributor

Trying a fix: #4339

@swift-ci
Copy link
Contributor Author

Comment by Curt Clifton (JIRA)

Replacing:

d["date"] = Date()

with:

d.setObject(Date(), forKey: "date" as NSString)

in NewSchool.getDict() works around the problem.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 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 itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

3 participants