johnno1962 / InjectionApp

Issue Tracking Repo for Injection as an App
MIT License
111 stars 7 forks source link

EXC_BAD_ACCESS after swizzling completes #10

Open sibljon opened 7 years ago

sibljon commented 7 years ago

After injection (via the Inject Source menu item) on the iOS Simulator, my app immediate terminates with Xcode showing an EXC_BAD_ACCESS on my Application Delegate.

Any ideas as to what I might be doing wrong, or what I can try?

Looking through the GitHub Issues on this project, as well as that of the original injectionforxcode project, the only issue I see is that I may have more than 128 Swift files in my project. Is this still a limitation?

Besides that, we use non-compatible Swift features elsewhere like final class, but I've tried to limit the scope of my test to a single file, SimpleViewController.swift:

import Foundation
import UIKit

class SimpleViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        print("viewDidLoad") 
    }

    func injected() {
        print("injected!")
    }
}

NOTE: I experienced the same issue in #2. I'm not sure whether these two issues have the same root cause or are related. I'm just mentioning that to give a more complete context.

Here's my full console log, for reference:

2017-01-13 14:09:02.415 Messenger[58203:7500986] Injection attempting connection to: 127.0.0.1:31452
2017-01-13 14:09:02.415 Messenger[58203:7500986] Connected to "Injection" plugin, ready to load x86_64 code.
Extracting project parameters...
Compiling /Users/jonsibley/code/1-spruce-ios/messenger/SpruceKit/Views/SimpleViewController.swift
... First time learning of project, one second ...
objc[58203]: Class _TtC9Messenger20SimpleViewController is implemented in both /Users/jonsibley/Library/Developer/CoreSimulator/Devices/327E5D49-B6E5-45EE-AA70-F1D3067B551B/data/Containers/Bundle/Application/172D632F-C6CF-49E2-AFCB-455AE98AF551/Messenger.app/Messenger (0x10f7418f0) and /Users/jonsibley/Library/Developer/Xcode/DerivedData/Messenger-edglwbxoycvsedcyninyotcxpkdt/Logs/iOSInjectionProject/build/Debug-iphonesimulator/InjectionBundle2.bundle/InjectionBundle (0x12ef53610). One of the two will be used. Which one is undefined.
1 injections performed so far.
2017-01-13 14:09:05.693 Messenger[58203:7497973] Ignore any warning, Swizzled Messenger.SimpleViewController 0x12ef53610 -> 0x10f7418f0
johnno1962 commented 7 years ago

Can you remove the injected method or change it to a class method?

sibljon commented 7 years ago

Can you remove the injected method or change it to a class method?

Both of these options work! Thanks! Do you know why? Ideally, I'd like to be able to use an instance injected() method to do things like self.setNeedsLayout() etc.

johnno1962 commented 7 years ago

Instance level injected performs a sweep of al objects i your app which isn’t as reliable as it could be. If you patch the project you’ll be able to see where it crashes. You could also use the INJECTION_BUNDLE_NOTIFICATION.

sibljon commented 7 years ago

Excellent, INJECTION_BUNDLE_NOTIFICATION works. Thank you @johnno1962 for making this possible with this library and app!

quicklywilliam commented 6 years ago

Just want to report that I was seeing this as well and removing the instance method fixes it. In my case, cleaning the build also would fix the issue (until the next time I hit it), but that's not a very practical work around.

Unfortunately, using INJECTION_BUNDLE_NOTIFICATION isn't a great work around either because I prefer to implement injected on an extension to UIViewController rather than having to add it to every subclass. For now, my work around is to disable the injected instance method in my extension, recompile, then re-enable it and recompile again. This clears whatever bad state was causing the crash.