Swift sometimes calls wrong method - ios

I noticed strange behaviour during working with Swift projects. I can't explain it other than Swift sometimes calls wrong method. It is very rare and even adding blank lines to the code could lead that this error is gone.
Let me explain in screenshots what I mean, next I use CoreData example of SwiftDDP project that can be found on Github, but such issues I was able to see in my own projects.
Here we at Todos.swift:74 where you can see breakpoint, I expect that next call should be getId() method of MeteorClient class because it was already instantiated:
After Step Into as you can see the ping() of the same instance is called:
The next two steps into lead to EXC_BAD_ACCESS exception:
In my project I saw this issue fairly often before I stopped using singletons, so it could be related to Swift static memory usage or I don't understand something that is not surprising as I have little experience with multithreading and memory management.
My environment is:
AppCode OC-145.184.11
Xcode Version 7.2.1 (7C1002)
iOS 9.2 SDK
Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81)
NOTE: Here I use AppCode but the same behavior I was able see in Xcode, and even more if the same issue that reproduces in Xcode could not reproduce in AppCode and vice versa.
Would be thankful if someone can explain this strange behaviour to me.
Thanks!

This just happened on my team, using Swift 2.2. It's really incredibly strange. It's not a threading issue or an async problem, it was a very cut & dry use case. We called one instance method and another one above it got called. I deleted the method that was getting called, and then the one above THAT got called instead. Then I moved the method I was actually calling to a different location in the file, and it looked like multiple properties were getting called.
This is disturbing and worrisome, as now you feel you can't trust your code to run properly.
But we did "solve" it. We moved the method up to the code that was actually getting triggered, and after a little trial & error the right method got called. Not yet sure if this will manifest itself for other methods.
It'd be nice to be able to provide a simple project where this is happening, but it seems highly unlikely that it's possible, and I can't share a snap shot of my code base with Apple. It must be a perfect storm of something to cause a bug with Swift's run time.

I had a similar issue, I think. Couldn't see that the wrong function was being called, but breakpoints on the function that was being called were never hit, and I couldn't step into the function from where it was being called. I added a new function (I called it wtf) with the same parameter list and implementation, and that one worked as expected, so it must have been a weird linking issue in the Swift compiler.
Update: super-cleaning appeared to work (see below), but it doesn't. Looks like I'm leaving my wtf function in.
After super-cleaning the project, it looks like everything's working as expected again:
clean (cmd + shift + k)
clean build dir (cmd + opt + shift + k)
quit XCode
delete derived data (rm -rf ~/Library/Developer/Xcode/DerivedData/*)
FYI, in my case, the function I call is in a generic base class, called from a generic subclass, triggered by a specialized sub-sub-class. As we all know, the generics are still very buggy in the Swift compiler, so that's probably why I encountered this.

Do you have multiple threads running? Maybe a network thread?
I thought I had this issue too but then it turned out my one thread was doing something and because the other thread crashed it stopped the other thread at a random point. I only noticed this thread so it seemed like it crashed at a random #IBAction function.

When I switched to iPhone 6 simulator instead of iPhone 7, it seems to be working correctly now!

Our app was rejected from review because of a mysterious crash. After debugging I found that it was having this same issue - but only for the Release scheme!
So I went through every setting in Build Settings one by one to see if switching it to that of another scheme would fix the issue: changing ENABLE_TESTABILITY to true fixed it......

Related

Xcode code autocompletion strange behaviour in function

Code completion suddenly stopped working in my viewDidLoad function. However, strangely, autocomplete works properly in other functions. It turns out that autocomplete does not work after a lot of code in a particular function. The viewDidLoad function has around 100 lines of code. And autocomplete becomes extremely slow after this. Autocomplete works properly in other projects.
P.S- The following did not work- Deleting Derived Data, commands in terminal etc. I had installed FuzzyAutocomplete through the Alcatraz plug in, but I deleted Alcatraz soon after. I think the fuzzy autocomplete plug in has still not been uninstalled even though I deleted alcatraz by pasting this into terminal-
rm -rf ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins/Alcatraz.xcplugin
I think there is some bug, and fuzzy autocomplete has blocked the Xcode code completion. All of this did not help. What can I do? Please help.
So well, I finally figured this out. I reinstalled Xcode on my laptop, and the problem didn't seem to go away. So I got to know that there was something wrong with my code. Xcode might have probably got stuck while processing that bit of code. So I slowly kept on commenting out one line after the other while checking whether code completion had started to work. I narrowed this down to one code block in my viewDidLoad() function which involved quite a lot of calculation. When I narrowed it down to just this much, Xcode suddenly gave me an error- Expression was too complex to be solved in reasonable time. Consider breaking up the expression in distinct sub-expressions. That line involved a long calculation of addition and subtraction which made Xcode get stuck on it which is why code completion could not find enough memory to run smoothly in that particular function, and Xcode kept on indexing again and again. For some reason, the previously installed copy of Xcode did not give me this error. Thankfully, this copy did. So the problem was sorted.
P.S- I was using Xcode 7.1 which was the latest version.

Xcode 7 crash every time I try to print something in the debugger console

I've searched around and can't find anything on this.
Using Swift 2 and Xcode Version 7.0.1 (7A1001). Every time I execute something in the debugger console, Xcode crashes.
The project is not very big, and has less than 10 third party frameworks.
I can't think of much more information that's relevant, but I'm sure there's more, so please do ask me if there's anything I should add to my question that would help.
I've of course cleaned build and derived data.
It's driving me insane. Thanks!
UPDATE 16/11/12
Submitted rdar://23559366.
How are you maintaining your third party frameworks? Via Carthage?
If so then this is probably your issue: https://github.com/Carthage/Carthage/issues/924
This is an issue if the location of the /Carthage/Build/iOS folder is in a different location to where it was produced (i.e if it was compiled on a different machine and the absolute file path has changed).
A temporary fix would be to run carthage build --no-use-binaries on your machine to rebuild the symbols using the current absolute file path working around the bug.
But if you wasn't using carthage then its probably not your issue so sorry
I had similar problem with Xcode whenever I hit breakpoint.
In case you see this screen right before your Xcode crashes - you are lucky and my fix might save you. All you need to do is open this window and in the Project Navigator, select any file that you want, so that instead of that white blank view you would get your code. After this you are most likely will be able to successfully stop your app at your breakpoint and perform the debug.
I am not sure why this happens, but I suspect that the reason is Debug View Hierarchy mode, which you might have triggered prior to setting you breakpoint and trying to stop at it. At least this is when it happens to me.
I have similar problem earlier.
If you try to print non-optional variable and unfortunately it holds nil value then it breaks/crash. so that make sure declare all possible variables as "Optional type".

Xcode Beta 6.3 magically delegates tasks to other threads?

We've came onto this strange bug in our project (I hope it's a bug not just our fault). Let's jump straight into the problem.
Our application is Storyboard based, of course it connects to the external API etc. But we've faced this strange thing that regular implementation of prepareForSegue: and things related to segues automagically delegates things to other threads. Just a small example - when I do NSLog(#"Sample log") in viewDidLoad it doesn't get fired when it should but it gets called in the random time that's why I guessed that something might get delegated to the other-than-main thread. Firstly I thought that the networking layer may cause this because of blocks and so but even when I put direc logs into the application they get called in a random time! We've tried to check the thread in debugger and it shows us thet everything happens in the main thread. Strange, right?
Anyway, my colleagues check this against the different versions of Xcode because on my machine there are bothe 6.2 and 6.3 Beta. When we compile this on a computer with only 6.3 installed everything works properly! Is it possible that 6.3 Beta introduces some changes which handles segues differently than previous versions? Or maybe we should start seeking for the troubles some oher place?

Xcode going crazy! while coding, loses classes, references and doesn't autocomplete giving often <<error type>>

After I installed the latest version of xcode i'm having a very annoying issue.
While I'm coding, xcode goes nuts. Without me doing anything weird, just typing code, xcode stop recognizing classes.
For example: I want to add UITableViewDelegate to my class, but it doesnt recognize it. After i type it manually sometimes it recognize it and it is shown in purple, some other time it doesnt. But both times it won't really consider it, so if I try to write down a method of that delegate, it won't show it.
Other times if I try to call a variable of a certain class, while trying to autocompleting it, it shows <>.
Other times if I try to call any class, let's say I try to type var test = UIActionSheet, it just shows a few elements in the autocomplete list (raw types, primitives, the classes of my main project, but it doesn't show the majority of classes).
It's like it's missing the documentation and the link with the main frameworks...
I'm working on a simple tabbed app from yesterday and it's the 3rd time i started all over because of this issue, thinking that starting over would fix the issue, but it's not working.
If i open a different project while the issue is going on, the other project works ( but i recoded all over my app so it's not that one the issue, and i also have the same issue on other project... it just doesn't affect 2 project at the same time )
i tried deleting derived data
i tried restarting both xcode and the comp
What's going on?
Here are two screenshoots where you can see what's happening:
I had the same problem earlier.
Exit Xcode and delete Derived Data folder here ~/Library/Developer/Xcode
Restart Xcode and you should see the autocomplete working again.
You have to learn how to take it apart and put back together.
Either reinstall Xcode, if it doesn't help, create a new project.
Then copy source files one by one and see when it breaks.
Freshly install the XCode IDE version 6.1
Use CMD + F to find all _element.
Check that if you named one variable _element
The following code may reproduce the error:
class _element:NSObject {
}
let _element:UIImage = UIImage()
// type _element under this line

iOS crash only when NOT running via XCode. Concidence?

My app was crashing only when not running using XCode debugger. It was hard to track because I can't debug but I finally figured it out. It was because of calling release on some object not owned by me. Before I corrected it I searched and found 2 related questions here (links below)
iOS App Crashes when running by itself on device, does not crash when running through Xcode using debugger, or in simulator
iPhone crash only when device not connected to xcode, how to understand the crash log?
None of the above question has answered why no crash when running via debugger.So my question is why it happens ? I know reasons for debug/release specific crashes but this is crazy. Is it just by chance although it happened more than 10 times.
What you describe is not atypical of obscure memory-related bugs. You might also want to use debug-malloc at such times. Although that is not guaranteed to find everything. The reason (and it's been happening probably as long as there've been source-level debuggers) is that memory is laid out at least somewhat differently in debuggable code, and when running under the debugger. So the error results in a different piece of memory being (harmlessly) corrupted when under the debugger. When not under the debugger the location corrupted is actually something that your code cares about, and it crashes.
The same could happen in reverse, but you'd never know - if it crashes when run debuggable, you'd find it before switching to running outside the debugging environment.
Reiterating #jyoung's answer since I didn't see it the first time I glanced through:
Try running with Zombie Objects turned off.
In debug mode if you have it turned on it is handling memory allocation differently. Try running it without.
Go to Edit Scheme... > Run > Diagnostics. Then make sure zombie objects is turned off:
Then run through your code path again.
I had this same issue while working on a project modularised with Xcode Frameworks. Even after removing all the logic in AppDelegate and only returning true inside application:didFinishLaunchingWithOptions, I was still getting the crash. Then I switched to my project settings, in the Frameworks, Libraries, and Embedded Content section and changed the embed option for the frameworks I added to Embed & Sign. This was what fixed the issue for me. I hope someone finds this helpful.
I was having this problem as well and was fortunate to figure out the cause quickly, hopefully by posting here I can save someone else some wasted time. To clarify, my app would run with no issues when launched directly from XCode, but would crash immediately when launched manually on the iPad.
The app in question is written in Obj-C but relies on some 3rd party code written in Swift. The Swift code is included in the app as an embedded framework. I had to set "Embedded Content Contains Swift Code" to Yes in the Build Settings for the app (under Build Options), then the problem went away.
I experienced this symptom when I made a NSString, sent a UTF8String from it to another object, and assigned it to a char pointer. Well, it turns out that I forgot to retain the original NSString, which wouldn't have mattered anyway, since I also failed to realize that the UTF8String method (which is presumably an object that gives access to the pointer itself) operates in the autorelease pool. That is, retaining the NSString itself did not fix the problem.
I suppose this appeared to work just fine when attached under the debugger only because I had zombies enabled, so the pointer I had was still valid. I should see if this is the reason it worked; if so, this is a good reason to test with and without NSZombie enabled.
At any rate, this was probably poor design to begin with, and a pretty obvious newbie memory management mistake once I found it. Luckily the console in the Organizer window gave me some hints on where to start looking, and debugging ultimately showed me where my pointer's value was changing. Hope this helps anyone who finds the way here.
I had this issue when accessing SQLite databases from outside the [[NSBundle mainBundle] resourcePath] directory, which caused iCloud errors.
I discovered the error only by installing a Console app onto my iPhone which logged the errors.
Once I accessed the databases from the correct directory, the errors disappeared and the application booted correctly.

Resources