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

Fatal error: Attempted to read an unowned reference in ReaderCardService.fetch #22640

Closed
Tracked by #22711
sentry-io bot opened this issue Feb 20, 2024 · 4 comments · Fixed by #22644
Closed
Tracked by #22711

Fatal error: Attempted to read an unowned reference in ReaderCardService.fetch #22640

sentry-io bot opened this issue Feb 20, 2024 · 4 comments · Fixed by #22644

Comments

@sentry-io
Copy link

sentry-io bot commented Feb 20, 2024

Sentry Issue: JETPACK-IOS-Y5

SIGABRT: Fatal error: Attempted to read an unowned reference but object %p was already deallocated > Fatal error: Attempted to read an unowned reference but object 0x280dcf960 was already deallocated
  File "ReaderCardService.swift", line 35, in ReaderCardService.fetch
  File "ReaderTopicService+FollowedInterests.swift", line 30, in ReaderTopicService.fetchFollowedInterestsLocally
  File "<compiler-generated>", in ReaderTopicService.fetchFollowedInterestsLocally
  File "<compiler-generated>", in thunk for closure
  File "main.swift", line 7, in main
...
(18 additional frame(s) were not displayed)

Investigation Notes

  • I wasn't able to understand the connection between ReaderTopicService+FollowedInterests with ReaderCardService. The fetchFollowedInterestsLocally method does not make any references to ReaderCardService.

  • However, ReaderCardService.fetch gets called on ReaderCardsStreamViewController's viewWillAppear.

  • In the Reader navigation refresh project (internal ref: pcdRpT-506-p2), we no longer keep multiple stream view controllers. With the new design, we always create a new ReaderStreamViewController when switching to a different stream.

    viewModel.didSelectIndex = { [weak self] index in
    guard let self else {
    return
    }
    if let existingFilter = filteredTabs.first(where: { $0.index == index }) {
    if previouslySelectedIndex == discoverIndex {
    addContentToContainerView(index: index)
    }
    viewModel.setFilterContent(topic: existingFilter.topic)
    } else {
    addContentToContainerView(index: index)
    }
    previouslySelectedIndex = index
    }

    private func addContentToContainerView(index: Int) {
    guard let controller = self.next as? UIViewController,
    let childController = viewModel.makeChildContentViewController(at: index) else {
    return
    }

    (The makeChildContentViewController eventually calls into a closure property, which gets populated from WPTabBarController+ReaderTabNavigation.makeReaderTabViewModel.)

  • Since ReaderCardService is retained in the ReaderCardsStreamViewController, it is possible that the view controller gets deallocated after the fetch method completes, which could technically crash due to the usage of unowned:

    followedInterestsService.fetchFollowedInterestsLocally { [unowned self] topics in

Next Steps

The easiest way is to update unowned to weak in ReaderCardService.fetch. It's expected for self to be deallocated since we moved away from a "tabbed" structure where we no longer retain multiple instances of ReaderStreamViewController.

Another way is to apply a fix that simulates the previous behavior of retaining multiple ReaderStreamViewController instances, and this number corresponds to the number of streams available in the Reader navigation menu. But this poses a higher risk in terms of stability (i.e., there could be additional issues) and user experience (e.g., no results experience, sync/refresh issues).

@dangermattic
Copy link
Collaborator

dangermattic commented Feb 20, 2024

Thanks for reporting! 👍

@dvdchr dvdchr changed the title SIGABRT: Fatal error: Attempted to read an unowned reference but object %p was already deallocated > Fatal... SIGABRT: Fatal error: Attempted to read an unowned reference Feb 20, 2024
@dvdchr dvdchr changed the title SIGABRT: Fatal error: Attempted to read an unowned reference Fatal error: Attempted to read an unowned reference Feb 20, 2024
@dvdchr
Copy link
Contributor

dvdchr commented Feb 20, 2024

Currently investigating this issue.

@dvdchr dvdchr linked a pull request Feb 20, 2024 that will close this issue
14 tasks
@dvdchr
Copy link
Contributor

dvdchr commented Feb 22, 2024

This should be addressed and fixed in 24.2.1 through #22644. Closing the issue.

Copy link
Author

sentry-io bot commented Feb 27, 2024

Sentry Issue: WORDPRESS-IOS-47R2

@dvdchr dvdchr changed the title Fatal error: Attempted to read an unowned reference Fatal error: Attempted to read an unowned reference in ReaderCardService.fetch Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants