I have a large number of views in my project where I do programmatic re-sizing and re-positioning of the subviews. I do this in layoutSubviews, using code like this:
CGRect rect = self.btnOne.frame;
// change values for rect
self.btnOne.frame = rect;
// re-use same CGRect to reposition other views
rect = self.btnTwo.frame;
// change values
self.btnTwo.frame = rect;
// etc. ...
This type of code has always worked fine for me for a couple of years, now. I have recently added a bunch of code like this to a co-worker’s project, and it’s now starting to behave very strangely. For no reason I can figure out, the re-used rect object here now sometimes randomly changes its own values between when it’s assigned the frame of a view and when it is re-assigned to the view’s frame at the end of the block. The result is subviews that are in the incorrect position. It seems that the error usually takes the form of rect.origin.x somehow acquiring the value of rect.origin.y or vice versa.
Even weirder, this problem only occurs on a real device; the exact same code running on the simulator does not have the problem.
The fix is to use a uniquely-declared CGRect object for each subview, like so:
CGRect rect = self.btnOne.frame;
// change values for rect
self.btnOne.frame = rect;
// declare new CGRect to reposition other views
CGRect rect2 = self.btnTwo.frame;
// change values
self.btnTwo.frame = rect2;
When I make this change, everything goes back to working correctly. But I’m really interested in knowing why this happens, and especially knowing why it happens on a device but not on a simulator. It also seems to not happen until a co-worker makes changes to the view, although it’s not clear to me how any change (not related to positioning of views) could do this.
One other change is that the project the code was imported into is using ARC, whereas my original code was not.
If there’s really nothing else going on, it sounds like a compiler error. There’s nothing magic about CGRects. They’re just structs.
Do you get the same results with and without optimizations? On different compilers (gcc-llvm vs clang, gcc if you still have it)?
Is this all on armv7 devices? There are known issues with thumb code on armv6 devices and in the past it hasn’t been disabled. As of Xcode 4.3, Apple seems to want to turn off thumb for all archs.
If the code is as simple as you show, it shouldn’t be that hard to look at the assembly.
I reuse CGRects all the time.