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

Facing EXC_BAD_ACCESS when projects and states become large and nested #293

Closed
fans3210 opened this issue Sep 26, 2017 · 20 comments
Closed

Comments

@fans3210
Copy link

fans3210 commented Sep 26, 2017

Hi,
Thanks for the amazing framework. Currently our project are using it heavily. But as the project grows, we are facing problems. The most common one is the 'EXC_BAD_ACCESS' issue.

00

11

22

33

This happens when I started to add a new state. The app will crash if the state contains one or more enum properties with associated value. If I simply changed the state to a simpler struct, the app won't crash most of the times. Sometimes, crash also happened when I tried to add a substate inside another substate(Eg: add a state in EntitiesState. I have to move that substate out of the EntitiesState to avoid the crash).

This only happens when we have about hundreds of different states and substates.

I would be very appreciated if you could give me some help. Thanks a lot!

@fans3210 fans3210 changed the title Facing EXC_BAD_ACCESS when projects and states becomes large and complicated Facing EXC_BAD_ACCESS when projects and states become large and complicated Sep 26, 2017
@DivineDominion
Copy link
Contributor

My states are far more deeply nested without problems. Are you 100% certain there are no reference types involved?

@amreshkumar
Copy link

@fans3210 You could try putting logs in init and deinit of EntitiesState – that may help unveil actual issue. As @DivineDominion said – with value types you are very unlikely to get an EXC_BAD_ACCESS unless there is a reference type involved.

@fans3210
Copy link
Author

fans3210 commented Sep 26, 2017

Hi @DivineDominion and @amreshkumar . Thanks for the quick reply. I've gone through all our states and confirmed all of them are value types, with their properties too. And the EXC_BAD_ACCESS issue might not only happen in EntitiesState, but also could happen in the main AppState.
The only condition that some states might interact with reference types are in their extensions. Eg: we used ObjectMapper frameworks for json parsing for some properties of a state and 'map' is a reference type.

extension AppState: Mappable {

public init?(map: Map) {

}

public mutating func mapping(map: Map) {
    userAuthState <- map["userAuthState"]
    chatState <- map["chatState"]
}

}

Another example is the conversion between our value type models and NSManagedObjects. But all these are handled by extensions and shouldn't have any issue with the EXC_BAD_ACCESS

@fans3210
Copy link
Author

@amreshkumar . The crash always happen in this line:

public func appReducer(action: Action, state: AppState?) -> AppState {
    var state = state ?? AppState() //this is where the crash happens, the AppState init is not even called since the crash happens before the checking of the optional state value

@fans3210 fans3210 changed the title Facing EXC_BAD_ACCESS when projects and states become large and complicated Facing EXC_BAD_ACCESS when projects and states become large and nested Sep 27, 2017
@DivineDominion
Copy link
Contributor

I vaguely recall I had a mind-boggling issue like this a few months ago, too. Not related to ReSwift, but to reading a perfectly fine optional value type instance. In my case it turned out something completely different did mess with the memory; it was something like a weak NSTextView reference (unbeknownst to me, weak references were not supported until 10.12!) -- anyway, it didn't have anything to do with the reported line at all.

Could be totally off-track with this wild-ass guess :) If you want to check this out, I suggest you remove a huge part of the app code except ReSwift and then see if the error goes away at all, i.e. if it is an issue like I had.

@buscarini
Copy link

Are you using Xcode 8 and running in the Device in debug mode? There was a swift compiler bug in Xcode 8. If this is the case you can try upgrading to Xcode 9 or enabling optimizations. There is a workaround but I think it didn't work for us.

@fans3210
Copy link
Author

fans3210 commented Sep 27, 2017

Thanks @DivineDominion for your suggestions.
@buscarini . Yes I was using xcode 8 before but I have already upgraded to xcode 9 with swift 3.2. Unfortunately I still faced such issue sometimes. I'm using a device to debug.

@amreshkumar
Copy link

@fans3210 I'm able to see similar behavior in my project. But instead of crashing it is a memory leak and it seems to be harmless for now. Can anyone else try to instrument with Leaks? I'm using Xcode 9/Swift 3.2

@mjarvis
Copy link
Member

mjarvis commented Sep 28, 2017

@amreshkumar Perhaps the memory leak you're seeing is #278 ? Does it go away if you use latest master instead of 4.0.0? We should probably think about making a 4.1 release with the latest changes.

@DivineDominion
Copy link
Contributor

@fans3210 any progress on this?

@fans3210
Copy link
Author

fans3210 commented Nov 3, 2017

@DivineDominion . No update for now since we didn't face such problems recently. I will report again if there is further issue. Thanks a lot

@fans3210
Copy link
Author

@DivineDominion @amreshkumar . The issue comes again after I merged my teammates' work which introduced a large number of new states

@fans3210
Copy link
Author

Currently 3 developers of our project are having the same issue.
The crash only happened on devices, not simulator

@amreshkumar
Copy link

@fans3210 can you try debugging with zombies enabled. It will help in understanding the problem better.

@Dimillian
Copy link

maybe link to mine #301 ? Is code optimisation turned off or on?

@fans3210
Copy link
Author

fans3210 commented Nov 17, 2017

@Dimillian , yes it's on. But my crash has nothing to do with the optimisation. Tested by turning it off with no luck. Another of my teammate tried to reduce the complexity of the structure of the state which caused crash by remove some properties and it worked.

@Dimillian
Copy link

@fans3210 Too bad. I'm worked with a quite complexe store, with a lot of structure. Got no crash in debug. What properties and complexity did you remove for example?

@fans3210
Copy link
Author

fans3210 commented Nov 17, 2017

@Dimillian . It's implemented by my colleague. It's quite straight forward actually. The model is a very simple struct called 'User'. So u can have a basic idea of what properties the struct might have. We didn't use any reference type property in that struct. During the project development, there is a state called FriendRecommendationState which contains two complete 'User' structs(eg: friendsToBeRecommended and recommender). There was no crash on this state before until recently we introduced several more states. His solution is to create another struct called 'SimpleUser' which only contains only the userid and name and use the 'SimpleUser' in our states instead of the previous 'User' as much as possible to avoid the crash

@DivineDominion
Copy link
Contributor

This is weird. I'd love to have a private look at your code commits before and after, maybe some day in December via Skype, so someone on the ReSwift team here has seen firsthand what causes crashes -- and how you fixed them. Email me if your team is interested.

@fans3210
Copy link
Author

@DivineDominion . Sure, I will let my team leader know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants