Swinject: Using weak scope is leaking objects - ios

I'm using Swinject for DI in my swift project. I think using weak object scope is leading to leaks.
I have this dummy object
protocol DevTestProtocol: class {}
class DevTest: DevTestProtocol {}
which is registered like follows (I'm using an assembly)
container.register(DevTestProtocol.self) { _ in
return DevTest()
}
.inObjectScope(.weak)
For testing purposes I added a property in my app delegate
var devTest: DevTestProtocol?
and in applicationDidFinishLaunchingWithOptions I resolve the object then forget about it
self.devTest = DI.resolve(DevTestProtocol.self)!
self.devTest = nil
Is the DevTest instance supposed to be gone by now?
Because it's not. It still shows in the debug memory graph as a leak.
see screenshot
I guess this isn't expected behaviour? Am I missing something?
It looks like a bug to me and it's messing up my whole setup.
What can we do about this?
GitHub issue

There is no memory leak, it's just a bug in Xcode 8 memory profiling instruments. See discussing at GitHub issues tracker

Related

Realm crash on iOS 10 with 'String'

I have recently released a new version of our app and during beta testing, it's crashing on all iOS 10 devices but not other versions. Since we have Crashlytics, we found a strange crash message in the backend that we can confirm is the reason all iOS 10 crashing since it's 100% iOS 10 and there's like 40 of them.
It reads as follows:
Fatal Exception: RLMException
Property Article.id is declared as String, which is not a supported managed Object property type. If it is not supposed to be a managed property, either add it to ignoredProperties() or do not declare it as #objc dynamic. See https://realm.io/docs/swift/latest/api/Classes/Object.html for more information.
And here's the object:
class Article: Object {
#objc dynamic var id: String = UUID().uuidString
// others...
override static func primaryKey() -> String? {
return "id"
}
}
As you can see, this is perfectly nomral and runs fine on other iOS. In Realm's doc, it LITERALLY SAYS to use String with #objc dynamic and there's no way it's unsupported. I suspect there's nothing special about Article.id, and since Article starts with A, it happens to be the first String property of all realm Objects. Maybe somehow all Strings stopped working on iOS 10?
Can anyone offer some advice or insights?(Please don't say things like drop iOS 10 support. For now, we need it.)
We ran into the same issue a couple of times, trying to drag Realm fully into Swift. This is not really the answer but more of a workaround we've had success with when needing backward compatibility.
It's an ObjC object, not Swift.
There's something going on with the bridging, perhaps conforming to NSCopy'ing or something along those line, so just change it to read
#objc dynamic var id = NSUUID().uuidString
See the Getting Started Guide in the Models section which calls for using NSUUID
NSUUID: An object representing a universally unique value that bridges
to UUID; use NSUUID when you need reference semantics or other
Foundation-specific behavior.
Turns out it was a Realm's bug. We happen to have another app that runs just fine on iOS 10, and after some inspection we realized that it was using Realm 4.3.2, instead of 4.4.1. After we downgraded Realm to 4.3.2, this problem disappeared.

EXC_BAD_ACCESS when trying to encode a subclass of a Codable-conformant class

I must be a moron or something but I'm scratching my head for a third day in a row and can't figure out what's going wrong with my intention to encode some JSON data in my Swift program...
Here's the situation:
I've got two classes as follows:
class Node: Codable {
// Nothing in here
}
and
class Shape: Node {
// No code here too
}
Then, I have an attempt to encode the subclass as follows:
do {
let encodedData = try JSONEncoder().encode(Shape())
} catch {
print(error)
}
This is all I have added to an empty Single View App project. When I run it, I get "Thread 1: EXC_BAD_ACCESS (code=1, address=0x350)" crash.
Of course, initially my classes used to have a lot of properties which were Codable too. I thought it was any of them, so I stripped them off but it appears it's not the properties that are causing the crash...
I'm running Xcode 9.3 on a High Sierra MacBook Pro. Is there anyone willing to reproduce this or anyone already bumped his head into such an odd behavior?
This is a known bug and you can work around it by turning on Whole Module Compilation mode for the Debug configuration, or by upgrading to Xcode 10 beta.

Use of unresolved identifier - app development with Swift Project 2

Sorry - I realise this is a complete beginner question, but I've googled for half a day now and still can't resolve the issue myself.
I'm using xCode 8.3 and trying to complete the apple - app development with swift course - end of unit project 2. I'm told (by the Apple book) I should be able to run the app without build failures, even if it isn't finished.
I get an unresolved identifier error when I add the following code to my ViewControler file, inside the class ViewController: UIViewController.
func updateUI() {
correctWordLabel.text = game.formattedWord
scoreLabel.text = "Wins: \(totalWins), Losses: \(totalLosses)"
treeImageView.image = UIImage(named: "Tree \(currentGame.incorrectMovesRemaining)")
}
xCode suggests I change it to Game.formattedWord, but when i do get 'Instance member 'formattedWord' cannot be used on type 'Game' ViewController.Swift'.
Could someone please help?
I've checked the sample code from Apple about 100 times and they are def saying it should be game.formattedWord in the code.
Thank you!
Try correctWordLabel.text = currentGame.formattedWord
I think it’s a typo.
Earlier in your code you create an instance of the Game struct called currentGame so you are accessing the formattedWord variable inside that instance. That’s why you couldn’t change it to Game. Game is like the blueprint of the struct. currentGame is your actual ‘thing’ Hope that makes some sense.

Opening Realm hangs after implementing new object class

I wish to support rearranging a UITableView. I have seen other answers here, here, and here recommend using a another class to manage the realm objects. The only problem is as soon as I add the class I cannot successfully open a Realm.
import RealmSwift
class Data: Object {
dynamic var id = ""
}
// Adding this class causes issues
class DataList: Object {
let list dataList = List<Data>()
}
Any ideas on what is going wrong here? When I attempt to open the Realm it just hangs: no error thrown.
Edit:
From the realm doc it says they should be declared as let.
Thanks to Tj3n for the solution. Migrate your schema or reinstall the app fixes the issue. Here is some doc on that.

App stuck in splash screen on iOS 9 with no error

My app gets stuck on splash screen in iOS 9 both on iPhone and simulator. I can run it on iOS 8 or lower on device and simulator with no problem. My colleague working on the same app has exactly the same problem.
There is no error or anything, just hangs on splash screen. If I stop it on xcode and try to run it from the phone or simulator directly, it would run without any problem.
By the way, I don't see didFinishLaunchingWithOptions or willFinishLaunchingWithOptions getting called!
In your "answer" you include the code:
+(void)initialize
{
titles = #[NSLocalizedString(#"CODE", nil), NSLocalizedString(#"ERROR", nil), NSLocalizedString(#"TROUBLESHOOTING", nil)];
}
This is indeed the source of your issue. It's wise to be very careful when implementing +load or +initialize. #bbum has a great article on exactly that topic.
+initialize is invoked the first time the class (or category) is touched - when the class is initialized +initialize is called by the class loading mechanism. There is no guarantee of when in the class loading process this may happen, which is part of your problem.
In your case you are using NSLocalizedString - which under the hood can be fairly heavy. It has dependancies on several other classes (NSString, etc) and can potentially access the file system. As #bbum points out in his article, that can lead to serious trouble. In your case, this may be a nasty deadlock.
Move your titles = #[NSLocalizedString... line to a more appropriate place in your object, like an initializer, awakeAfterUsingCoder:, etc. and your immediate problem should be solved. After doing so you should check your entire codebase for instances where +initialize and +load are implemented and audit them to make sure those uses are in line with #bbum 's recommendations.
OK I found the problem. It sounds ridiculous though!!
I am using UITabBarController and inside the first controller I have a UITableViewController with a customised datasource class which would initiate a hard code table header and these headers are localised!!
+ (void)initialize {
titles = #[NSLocalizedString(#"CODE", nil), NSLocalizedString(#"ERROR", nil), NSLocalizedString(#"TROUBLESHOOTING", nil)];
}
After I traced the stacks, I realised the process gets stuck right there with no trace and error! I still don't know why!
So I came up with a workaround:
+ (void)initialize {
titles = #[#"Code",#"Error",#"Troubleshooting"];
}
And only retrieve the localised value when returning the text:
- (NSString *)titleAt:(NSInteger)index {
return NSLocalizedString(titles[index],nil);
}
I have both debug and release set to NO
You sure "any SDK" also has arm64?
Ok, I think I found the answer.
You have to specify arm64 in all "Valid Architectures".
If you don't specify arm64 or forget one the app won't start and stays on the splashscreen.
Just verified this.
Is this an Xcode 7 bug?

Resources