removeFromSuperview causes crash (non-ARC) - ios

I'm having a weird issue with UIViews and manual memory management.
I have a view (contentView) which is the main view of a view controller.
After a long press on the contentView, another view is supposed to fade in (on top of it).
When the gestures ends, the additional view fades out.
The issue is:
When the contentView receives a long press, I create the auxiliary view, add it to the contentView, and then release it, which is/was the common practice back in the pre-ARC days.
It works okay on the iPhone, but it crashes on the iPad!
The crashy line is:
[ZPNowPlayingItemInfoView dealloc]
...which gets triggered when I remove the auxiliary view from the contentView.
Any clues on why this happens?
If I comment out the release line (see my comment in the code), it works flawlessly on both devices, but it feels bad.
Here's the code:
-(void)longPressDetected:(UILongPressGestureRecognizer*)longPressGR
{
//Content view of the view controller I'm in
UIView *contentView = MSHookIvar<UIView*>(self, "_contentView");
if (longPressGR.state == UIGestureRecognizerStateBegan) {
id item = MSHookIvar<MPAVItem*>(self, "_item");
ZPNowPlayingItemInfoView *infoView =
[[ZPNowPlayingItemInfoView alloc] initWithFrame:
CGRectMake(0,0,contentView.frame.size.width,contentView.frame.size.height)
item:item];
//infoView retain count: 1
[infoView setAlpha:0.f];
[contentView addSubview:infoView];
//infoView retain count: 3 (???)
//iPad goes berserk on this line
//Commented - Works both on iPhone and iPad
//Uncommented - Works only on iPhone
//[infoView release];
//infoView retain count: 2 (if release is uncommented)
[UIView animateWithDuration:0.35f animations:^{
[infoView setAlpha:1.0f];
} completion:^(BOOL finished) {
//infoView retain count: 3
}];
} else if (longPressGR.state == UIGestureRecognizerStateEnded) {
ZPNowPlayingItemInfoView* infoView = nil;
for (UIView *subview in contentView.subviews) {
if ([subview isKindOfClass:[ZPNowPlayingItemInfoView class]]) {
infoView = (ZPNowPlayingItemInfoView*)subview;
break;
}
}
[UIView animateWithDuration:0.35f animations:^{
[infoView setAlpha:0.f];
} completion: ^(BOOL finished){
[infoView removeFromSuperview];
}];
}
P.S. I need to use manual memory management. This is a tweak for jailbroken devices.
Stack trace:
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libobjc.A.dylib 0x195287bdc 0x19526c000 + 0x1bbdc // objc_msgSend + 0x1c
1 + Musix.dylib 0x10015b19c 0x100154000 + 0x719c // -[ZPNowPlayingItemInfoView dealloc] + 0x48
2 libsystem_blocks.dylib 0x19590d90c 0x19590c000 + 0x190c // _Block_release + 0xfc
3 UIKit 0x188ef8590 0x188eb0000 + 0x48590 // -[UIViewAnimationBlockDelegate dealloc] + 0x44
4 CoreFoundation 0x1845f1374 0x1845ec000 + 0x5374 // CFRelease + 0x208
5 CoreFoundation 0x184601004 0x1845ec000 + 0x15004 // -[__NSDictionaryI dealloc] + 0x8c
6 libobjc.A.dylib 0x19528d720 0x19526c000 + 0x21720 // (anonymous namespace)::AutoreleasePoolPage::pop(void*) + 0x230
7 CoreFoundation 0x1845f4f90 0x1845ec000 + 0x8f90 // _CFAutoreleasePoolPop + 0x18
8 CoreFoundation 0x1846c774c 0x1845ec000 + 0xdb74c // __CFRunLoopRun + 0x5d8
9 CoreFoundation 0x1845f51f0 0x1845ec000 + 0x91f0 // CFRunLoopRunSpecific + 0x188
10 GraphicsServices 0x18d7575a0 0x18d74c000 + 0xb5a0 // GSEventRunModal + 0xa4
11 UIKit 0x188f26780 0x188eb0000 + 0x76780 // UIApplicationMain + 0x5cc
12 Music (*) 0x10006ee28 0x100064000 + 0xae28 // 0x0000adac + 0x7c
13 libdyld.dylib 0x1958e2a04 0x1958e0000 + 0x2a04 // start + 0x0
ZPNowPlayingItemInfoView:
#interface ZPNowPlayingItemInfoView()
#property (nonatomic, retain) MPAVItem* item;
#property (nonatomic, retain) MPUSlantedTextPlaceholderArtworkView *artworkView;
#property (nonatomic, retain) UILabel *artistLabel;
#property (nonatomic, retain) UILabel *albumLabel;
#property (nonatomic, retain) UILabel *songLabel;
#end
ZPNowPlayingItemInfoView dealloc:
-(void)dealloc
{
[super dealloc];
[self.item release];
[self.artworkView release];
[self.artistLabel release];
[self.songLabel release];
}

You have some problem in ZPNowPlayingItemInfoView class. When this problem happens? Only when the object gets deallocated. When you comment [infoView release] out, your object is never deallocated and the problem doesn't arise - you will have a memory leak though.
Inspect what ZPNowPlayingItemInfoView does, especially its dealloc method. Are you sure you are constructing it correctly? Is item always a valid object?
After seeing the ZPNowPlayingItemInfoView dealloc method, the problem is quite clear - [super dealloc] must always be the last call, not the first one. Once you have deallocated the object, accessing its properties is an undefined operation.

When commenting out the release is a working workaround, that indicates that you have released it once too often. It may well be the very one release that you commented out.
removeFromSuperview does reduce the retain count by 1.
I suggest re-visiting the full life cycle of the view object. This can be tricky though. Each retain needs to have exactly one corresponding release or autorelease. Assigning the view to a property using its getter (self.myView = subview) does retain it and re-assigning another view to the property (self.myView = someOhterview) releases subview.
On the contrary accessing the iVar directly (myView = subview) does not maintain the release/retain-cycle.
There is more than that. Adding the view and removing it from an array, set or dictionary will change the retain count accordingly.
So go and have a deeper look at it. Use instruments to observe the retain count.

Related

ios:EXC_BAD_ACCESS for Webview delegate

I have a situation where I am trying to resolve these Crashlytics issues and I have this crash log
Thread : Crashed: com.apple.main-thread
0 libobjc.A.dylib 0x34217f46 objc_msgSend + 5
1 UIKit 0x29a2d5a3 -[UIWebView webView:decidePolicyForNavigationAction:request:frame:decisionListener:] + 182
2 CoreFoundation 0x2630cad4 __invoking___ + 68
3 CoreFoundation 0x26239645 -[NSInvocation invoke] + 300
4 CoreFoundation 0x2623d0c7 -[NSInvocation invokeWithTarget:] + 50
5 WebKitLegacy 0x326d9261 -[_WebSafeForwarder forwardInvocation:] + 224
6 CoreFoundation 0x2630b62f ___forwarding___ + 354
7 CoreFoundation 0x2623d008 _CF_forwarding_prep_0 + 24
8 CoreFoundation 0x2630cad4 __invoking___ + 68
9 CoreFoundation 0x26239645 -[NSInvocation invoke] + 300
10 WebCore 0x31c02729 HandleDelegateSource(void*) + 100
11 CoreFoundation 0x262cefbf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
12 CoreFoundation 0x262ce461 __CFRunLoopDoSources0 + 364
13 CoreFoundation 0x262cca35 __CFRunLoopRun + 772
14 CoreFoundation 0x2621a3b1 CFRunLoopRunSpecific + 476
15 CoreFoundation 0x2621a1c3 CFRunLoopRunInMode + 106
16 GraphicsServices 0x2d801201 GSEventRunModal + 136
17 UIKit 0x2988443d UIApplicationMain + 1440
18 abc 0x0030dcd7 main (main.m:14)
I can understand that its some callback on webview delegate and bad excess has occurred, so to rectify this I handled the delegates via
[self.webview stopLoading];
self.webview.delegate =nil;
in all the classes, yet I can see this crash. Can you enlighten me what's possibly going wrong and some approach to rectify this?
The following might be the case here
The user is presented a screen with UIWebView
The UIViewController sets self as the delegate Web page starts downloading
The User quits screen
UIViewController gets deallocated UIWebView finishes loading and sends I am finished loading message to its delegate
or
some other delegate method gets called when the webview object is no more.i.e dangling pointer effect
1.Always make sure you stop loading the webView and remove the delegate before leaving the view
Before releasing an instance of UIWebView for which you have set a
delegate, you must first set its delegate property to nil. This can
be done, in your dealloc method
Here is the reference
// If ARC is used
- (void)dealloc {
[_webView setDelegate:nil];
[_webView stopLoading];
}
// If ARC is not used
- (void)dealloc {
[webView setDelegate:nil];
[webView stopLoading];
[webView release];
[super dealloc];
}
// ARC - Before iOS6 as its deprecated from it.
- (void)viewWillUnload {
[webView setDelegate:nil];
[webView stopLoading];
}
2.Make sure you are not stopLoading and setDelegate to nil in viewWillDisappear
if the ViewController is a child of a another ViewController, u can
trigger the removal of the ViewController's view from the parent
ViewController's view with an animation. At the same time, u can
remove the ViewController from its parent and nil out its reference.
at this point ViewController will be nil and viewWillDisappear
will never be called, meaning the WebView delegate will never be
cleaned up
Use dealloc and ensure that your WebView is always cleaned up.
3.Make sure you set the ContentOffset of the subviews of webview to CGPointZero without animation
In iPad in some versions while webview is scrolling if you close the parent
viewcontroller without setting ContentOffset to CGPointZero this
kind of problems will come
so its better to you call the following code of in parent viewcontroller before closing it
for (id subview in webView.subviews){
if ([[subview class] isSubclassOfClass: [UIScrollView class]]){
[subview setContentOffset:CGPointZero animated:NO];
}
}
Hope this helps.Feel free to ask your doubts.
4.Generally speaking You should not embed UIWebView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
Here is the reference
Try disabling your UIWebView's scrolling behaviour before the ViewController deallocs it
for (id subview in webView.subviews){
if ([[subview class] isSubclassOfClass: [UIScrollView class]]){
[subview setContentOffset:CGPointZero animated:NO];
}
}
p.s. Dipen Chudasama's approach is correct, according to Apple's documentation, you should really set the delegate property to nil before releasing the webview, assuming you have released the webview correctly inside dealloc function but not viewWillDisappear
UIWebViews delegate uses assign and not weak. So you need to nullify the delegate when the webView's controller gets deallocated.
Example:
- (void)dealloc
{
self.webview.delegate =nil;
}
Are you adding a web view within your subclass of a web view? Generally that's the problem and if so, changing the superclass to a UIView would solve the problem.
Use visa versa means first nil your delegate and then after stop loading web view may be help you.
like this.
[_webView setDelegate:nil];
[_webView stopLoading];
As per apple document : Important Before releasing an instance of UIWebView for which you have set a delegate, you must first set its delegate property to nil.
Use [listener use] instead, to tell your UIWebView to handle clicked URL.
-(void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id )listener
{
[listener use]
}

UICollectionView crash on unhighlightAllItems

I've gotten several crash reports related to a UICollectionView in iOS 7. I'm not able to consistently recreate this crash.
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x91c4392b
Crashed Thread: 0
Application Specific Information:
*** Terminating app due to uncaught exception '', reason: ''
Thread 0 Crashed:
0 libobjc.A.dylib 0x39dd2b26 objc_msgSend + 6
1 UIKit 0x31fd5eef -[UICollectionView cellForItemAtIndexPath:] + 111
2 UIKit 0x32060bfd -[UICollectionView _unhighlightItemAtIndexPath:animated:notifyDelegate:] + 149
3 UIKit 0x32383947 -[UICollectionView _unhighlightAllItems] + 151
4 UIKit 0x3205f9fb -[UICollectionView touchesBegan:withEvent:] + 367
5 UIKit 0x31fcb101 forwardTouchMethod + 233
6 UIKit 0x31fcb101 forwardTouchMethod + 233
7 UIKit 0x31e3be4b _UIGestureRecognizerUpdate + 5523
8 UIKit 0x31e73c41 -[UIWindow _sendGesturesForEvent:] + 773
9 UIKit 0x31e735e7 -[UIWindow sendEvent:] + 667
10 UIKit 0x31e48a25 -[UIApplication sendEvent:] + 197
11 UIKit 0x31e47221 _UIApplicationHandleEventQueue + 7097
12 CoreFoundation 0x2f69e18b __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
13 CoreFoundation 0x2f69d6e1 __CFRunLoopDoSources0 + 341
14 CoreFoundation 0x2f69be4f __CFRunLoopRun + 623
15 CoreFoundation 0x2f606ce7 CFRunLoopRunSpecific + 523
16 CoreFoundation 0x2f606acb CFRunLoopRunInMode + 107
17 GraphicsServices 0x342f4283 GSEventRunModal + 139
18 UIKit 0x31ea8a41 UIApplicationMain + 1137
19 JackThreadsIpad 0x000922b7 main (main.m:16)
The UICollectionViewCells in the app share a common superclass that manages highlighting. When the cell is highlighted the alpha changes.
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
self.alpha = 0.8;
} else {
self.alpha = 1.0;
}
}
Could calling [super setHighlighted:highlighted] cause a crash like this? The app was compiled and submitted with XCode 4 and is only happening on iOS 7. Any other suggestions to figure out where this is happening. Thanks for your help.
Edit:
I was able to catch this in the debugger, but it still is not consistently reproducible. The crash is:
[NSIndexPath section] message sent to deallocated instance XXXXXXXX
If you are calling reloadData while the user is dragging the view, that might be the reason.
I had crashes related to this with similar crash reports and "fixed" the issue by delaying the reloadData call until after the user has finished scrolling the view. E.g. create a wrapped method instead of calling reloadData directly.
- (void)updateData {
if (self.collectionView.isTracking) {
self.updateDataOnScrollingEnded = YES;
} else {
[self.collectionView reloadData];
}
}
Then when scrolling ends, call the updateData method (if needed) from the scroll view's delegate methods.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate) {
[self scrollViewStopped:scrollView];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self scrollViewStopped:scrollView];
}
- (void)scrollViewStopped:(UIScrollView *)scrollView
{
if (self.updateDataOnScrollingEnded) {
[self updateData];
self.updateDataOnScrollingEnded = NO;
}
}
My guess is that there is a weak reference to the highlighted cell's indexPath somewhere inside of the collectionView, and that calling reload will dealloc that indexPath. When the collectionView then tries to unhighlight the cell, it crashes.
EDIT:
As mentioned in comments below, this "solution" has some flaws. While investigating the issue further, it seems that in my case the problem had to do with multiple reloadData calls being queued on the main thread during the dragging of the collection view. When there was only one reloadData call, everything was fine, but whenever there was more than one – crash!
Since I always had exactly one section in my collectionView i replaced the reloadData call with
reloadSections:[NSIndexSet indexSetWithIndex:0]
However, this causes the cells to quickly fade out and back in again which I avoided with the following method (it would probably be better off as a category on the collection view)
- (void)reloadCollectionView:(UICollectionView *)collectionView animated:(BOOL)animated
{
[UIView setAnimationsEnabled:animated];
[collectionView performBatchUpdates:^{
[collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} completion:^(BOOL finished) {
[UIView setAnimationsEnabled:YES];
}];
}
So far, this has worked well for me and it also allows for the data to actually be updated while scrolling.
Not sure after identifying only this piece of code. But as crash signal (SIGSEGV) seems due to memory leak. You just go to your Xcode setting and inside Edit Scheme jsut enable Zombie option and then try to reproduce your crash. It will show you the controller class name of method or any crash related information inside console of Xcode.
And also just try to modify you condition below:-
- (void)setHighlighted:(BOOL)highlighted {
//just comment this line or write this line to the below and check
//[super setHighlighted:highlighted];
if (highlighted) {
self.alpha = 0.8;
} else {
self.alpha = 1.0;
}
[super setHighlighted:highlighted];
}
I had this problem, though slightly different crash. Fixed by holding off any reloadData until the highlight is cleared. While toostn's suggestion would fix the issue, it is useful to be able to reloadData whilst scrolling, but doesn't make much sense when highlighting - as you have your finger on a cell.
implement the following UICollectionViewDelegate methods:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
self.allowReload = NO;
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
self.allowReload = YES;
[self reloadIfNecessary]; // calls reloadData if it is necessary to do so!
}
I also had this crash in _unhighlightAllItems in a collection view where I used a long press recognizer that changes the state of cells (but not their number) and then called [collectionView reloadData]. In my case, the solution from #toostn (using performBatchUpdates) works great.
I also found that using reloadItemsAtIndexPaths: instead of reloadData also avoids the crash.

ios App crashes when deallocating an UIView subclass instance

My app built natively with CocoaTouch and ARC crashes when deallocating a UIView subclass instance.
Here is the crash log.
OS Version: iOS 6.1.3 (10B329)
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00000008
Crashed Thread: 0
0: 0x39de65b0 libobjc.A.dylib objc_msgSend + 16
1: 0x31edb694 CoreFoundation -[NSArray makeObjectsPerformSelector:] + 300
2: 0x33d8c57a UIKit -[UIView(UIViewGestures) removeAllGestureRecognizers] + 146
3: 0x33d8c144 UIKit -[UIView dealloc] + 440
4: 0x00240b36 MyApp -[StandardPanelView .cxx_destruct](self=0x20acba30, _cmd=0x00240985) + 434 at StandardPanelView.m:139
5: 0x39deaf3c libobjc.A.dylib object_cxxDestructFromClass(objc_object*, objc_class*) + 56
6: 0x39de80d2 libobjc.A.dylib objc_destructInstance + 34
7: 0x39de83a6 libobjc.A.dylib object_dispose + 14
8: 0x33d8c26a UIKit -[UIView dealloc] + 734
9: 0x0022aa14 MyApp -[StandardPanelView dealloc](self=0x20acba30, _cmd=0x379f1a66) + 156 at StandardPanelView.m:205
10: 0x39de8488 libobjc.A.dylib (anonymous namespace)::AutoreleasePoolPage::pop(void*) + 168
11: 0x31e98440 CoreFoundation _CFAutoreleasePoolPop + 16
12: 0x31f28f40 CoreFoundation __CFRunLoopRun + 1296
13: 0x31e9bebc CoreFoundation CFRunLoopRunSpecific + 356
14: 0x31e9bd48 CoreFoundation CFRunLoopRunInMode + 104
15: 0x35a502ea GraphicsServices GSEventRunModal + 74
16: 0x33db1300 UIKit UIApplicationMain + 1120
17: 0x00113c50 MyApp main(argc=1, argv=0x2fd3ed30) + 140 at main.m:23
The questions are:
what could be set wrong that makes the internal call to [UIView(UIViewGestures) removeAllGestureRecognizers] crash. One theory is that some gesture in the gestures array is deallocated already somewhere else.
When a UIView contains subviews, how is the sequence of deallocation process?
Some extra background info:
The crash happens, but there is no exact way to reproduce it.
The StandardPanelView instance works as delegate for gestures belongs to its subviews.
We are using flyweight on StandardPanelView instances, i.e., caching and recycling.
Thanks in advance for any hint about how this crash could happen.
My first impression is that you might be trying to access the StandardPanelView, which has just gotten deallocated.
What could be set wrong that makes the internal call to [UIView(UIViewGestures) removeAllGestureRecognizers] crash. One theory is that some gesture in the gestures array is deallocated already somewhere else.
It will not be because a UIGestureRecognizer got deallocated. The UIView strongly holds the UIGestureRecognizers in an NSArray. They will not be deallocated while they are still in the array.
However, the UIGestureRecognizer's delegate may have gotten deallocated. That is only an (assign) property, meaning that it is -not- strongly held, and if the delegate is deallocated, it will be a dangling pointer. So, if in [NSArray makeObjectsPerformSelector:] the delegate is used, this might happen.
When a UIView contains subviews, how is the sequence of deallocation process?
Objects are deallocated from 'parent' to 'child', ie. the superview is deallocated, then the subview, then the gesture recognizers. (Although whether the subviews are dealloced before the gesture recognizers is an implementation detail, so you probably shouldn't depend on it).
We can see this in a simple sample controller:
// The UIView that will be used as the main view
// This is the superview
#interface MyView : UIView
#end
#implementation MyView
- (void)dealloc {
NSLog(#"Dealloc MyView");
}
#end
// This view will be put inside MyView to be used as a subview
#interface MySubview : UIView
#end
#implementation MySubview
- (void)dealloc {
NSLog(#"Dealloc MySubview");
}
#end
// This is the gesture recognizer that we will use
// We will give one to each view, and see when it is deallocated
#interface MyGestureRecognizer : UIGestureRecognizer
#property (nonatomic, copy) NSString *tag;
#end
#implementation MyGestureRecognizer
#synthesize tag;
-(void)dealloc {
NSLog(#"Dealloc MyGestureRecognizer tag: %#", tag);
}
#end
// Just a test view controller that we will push on/pop off the screen to take a look at the deallocations
#interface TestViewController : UIViewController
#end
#implementation TestViewController
- (void)loadView {
self.view = [[MyView alloc] init];
MyGestureRecognizer *recognizer = [[MyGestureRecognizer alloc] initWithTarget:self action:#selector(doStuff)];
recognizer.tag = #"MyViewGestureRecognizer";
recognizer.delegate = self;
[self.view addGestureRecognizer:recognizer];
}
- (void)viewDidLoad {
[super viewDidLoad];
MySubview *subview = [[MySubview alloc] init];
MyGestureRecognizer *recognizer = [[MyGestureRecognizer alloc] initWithTarget:self action:#selector(doStuff)];
recognizer.tag = #"MySubviewGestureRecognizer";
recognizer.delegate = self;
[subview addGestureRecognizer:recognizer];
[self.view addSubview:subview];
}
- (void)doStuff {
// we don't actually care what it does
}
#end
All we are doing is adding MyView to be the main view for TestViewController, and then adding a MySubview inside MyView. We also attach a MyGestureRecognizer to each of the views.
When we push it off the screen, our log output presents:
Dealloc TestViewController
Dealloc MyView
Dealloc MySubview
Dealloc MyGestureRecognizer tag: MySubviewGestureRecognizer
Dealloc MyGestureRecognizer tag: MyViewGestureRecognizer
Sorry for the long answer... It has been about 3 months since you posted it, so maybe you already solved the problem, but in case anyone else stumbles across this answer, I hope it helps out.

UIView removeFromSuperview causes app to crash

Let me explain my problem. I have 3 UIView: a LoginView, LibraryView, and a StoreView. I have this code to switch from one UIView to another:
- (void)showView:(NSInteger)viewTag
{
if (viewTag == 1)
{
if (self.loginView)
{
self.loginView = nil;
self.loginView.delegate = nil;
}
LoginView *loginPage = [[LoginView alloc]initWithFrame:self.view.bounds];
[loginPage setDelegate:self];
self.loginView = loginPage;
[loginPage release];
[self.view addSubview:self.loginView];
}
else if(viewTag == 2)
{
if (self.libraryView)
{
self.libraryView = nil;
self.libraryView.delegate = nil;
}
LibraryView *libraryPage = [[LibraryView alloc]initWithFrame:self.view.bounds];
[libraryPage setDelegate:self];
self.libraryView = libraryPage;
[libraryPage release];
[self.view addSubview:self.libraryView];
}
else
{
if (self.bookStoreView)
{
self.bookStoreView = nil;
self.bookStoreView.delegate = nil;
}
BookStoreView *bookStore = [[BookStoreView alloc]initWithFrame:self.view.bounds];
[bookStore setDelegate:self];
self.bookStoreView = bookStore;
[bookStore release];
[self.view addSubview:self.bookStoreView];
}
}
Basically, that is how I initialize the UIViews. And here are the buttons used to switch between them:
- (void)loginViewToLibraryView
{
[self.loginView removeFromSuperview];
[self showView:2];
}
- (void)libraryViewToStoreView
{
[self.libraryView removeFromSuperview];
[self showView:3];
}
//so on...
The problem arises when the I call the function libraryViewToLoginView and storeViewToLoginView. The app crashes whenever I call these functions, which is weird because the two functions worked properly before. I checked Profile and it gave me this stack trace:
# Address Category Event RefCt Timestamp Size Responsible Library Responsible Caller
0 0xc4dcac0 CALayer Malloc 1 00:02.233.004 48 UIKit -[UIView _createLayerWithFrame:]
1 0xc4dcac0 CALayer Retain 3 00:02.238.317 0 QuartzCore CA::Layer::insert_sublayer(CA::Transaction*, CALayer*, unsigned long)
2 0xc4dcac0 CALayer Release 2 00:02.238.324 0 UIKit -[UIView(Internal) _addSubview:positioned:relativeTo:]
3 0xc4dcac0 CALayer Retain 3 00:02.238.518 0 QuartzCore -[CALayerArray copyWithZone:]
4 0xc4dcac0 CALayer Release 2 00:02.238.602 0 UIKit -[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:]
5 0xc4dcac0 CALayer Retain 3 00:02.238.665 0 QuartzCore -[CALayerArray copyWithZone:]
6 0xc4dcac0 CALayer Release 2 00:02.238.796 0 UIKit -[UIView(Internal) _didMoveFromWindow:toWindow:]
7 0xc4dcac0 CALayer Retain 3 00:05.107.397 0 QuartzCore -[CALayerArray copyWithZone:]
8 0xc4dcac0 CALayer Release 2 00:05.107.539 0 UIKit -[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:]
9 0xc4dcac0 CALayer Retain 3 00:05.107.613 0 QuartzCore -[CALayerArray copyWithZone:]
10 0xc4dcac0 CALayer Release 2 00:05.107.700 0 UIKit -[UIView(Internal) _didMoveFromWindow:toWindow:]
11 0xc4dcac0 CALayer Retain 2 00:06.105.958 0 QuartzCore -[CALayerArray copyWithZone:]
12 0xc4dcac0 CALayer Release 2 00:06.108.134 0 UIKit -[UIView dealloc]
13 0xc4dcac0 CALayer Release 1 00:06.108.492 0 UIKit -[UIView dealloc]
14 0xc4dcac0 CALayer Zombie -1 00:06.115.332 0 QuartzCore CA::release_objects(X::List<void const*>*)
As you can see, it is a bunch of calls on the CALayer which I don't really understand. I want to understand why is this happening. Can anyone explain?
Frankly I have not understood everything that you want to achieve here. But you should consider the sequence of events. See my comments:
if (self.loginView)
{
self.loginView = nil;
//self.loginView is nil now. What so you think doese happen on the next line?
self.loginView.delegate = nil;
// change the sequence of this lines and it will be ok.
}
LoginView *loginPage = [[LoginView alloc]initWithFrame:self.view.bounds];
[loginPage setDelegate:self];
self.loginView = loginPage;
[loginPage release]; //here you release the object. it is gone now. However, there are still references to it.
[self.view addSubview:self.loginView]; // here you add the released object. What do you expect to happen?
// Switch those two statmetns and you should be fine.
Well, it may even work as those statmens are next to each other. And when you remove it from its super view it gets released again. And there your app might crash.
BTW, without having it retained you should not release it at all. Or am I wrong here? However, addSubview should retain it and removeFromSuperview will release it, so no additional release would be required. Onece removed it should be gone. (If not retained somewhere else)

How to keep IBOutlet property from being released (iOS using ARC)

I am implementing the AQRecorder class from Apple's SpeakHere example into my project using ARC. To get it to compile, I had to create a class (AQRecorderController) that controls the AQRecorder instance (equivalent to the SpeakHereController in the example). AQRecorderController is connected through the nib of my main view controller and implemented as a property. The problem occurs whether or not the property is strong or weak.
My problem is that shortly after loading the view controller, the AQRecorderController is released, but only when tested on device. In the simulator, this does not occur. It occurs for iPad and iPhone, iOS 5 and iOS 6. I need to maintain this reference throughout the lifetime of my view controller for recording purposes (you can't delete the recorder while recording and expect to have a finished file).
Has anyone run into this or anything similar? If the AQRecorderController property is strong, I get a bad access error when trying to use it, if its weak, I just get a nil, and its unusable.
Any help would be greatly appreciated.
formViewController.h:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#class AQRecorderController;
#interface formViewController : UIViewController <UIActionSheetDelegate, UITableViewDelegate, UIGestureRecognizerDelegate> {
IBOutlet AQRecorderController *aqRecorderController;
}
#property (nonatomic, weak) IBOutlet AQRecorderController *aqRecorderController;
#end
AQRecorderController.h
#import <Foundation/Foundation.h>
#import "AQRecorder.h"
#interface AQRecorderController : NSObject
{
AQRecorder *aqRecorder;
}
#property (readonly) AQRecorder* aqRecorder;
#property (nonatomic, assign) bool isRecording;
#property (nonatomic, strong) NSString* fileName;
-(bool)startRecording;
-(bool)pauseRecording;
-(bool)stopRecording;
-(bool)initializeRecordSettingsWithCompression:(bool)compressionEnabled;
#end
formView.xib:
Here is the stack trace after the AQRecorderController has been released:
2012-10-23 10:34:09.600 TestApp[510:907] (
0 TestApp 0x000f32ab
-[AQRecorderController dealloc] + 138
1 CoreFoundation 0x32247311 CFRelease + 100
2 CoreFoundation 0x3225195d <redacted> + 140
3 libobjc.A.dylib 0x31ad5489 <redacted> + 168
4 CoreFoundation 0x32249441 _CFAutoreleasePoolPop + 16
5 Foundation 0x37303a7f <redacted> + 466
6 CoreFoundation 0x322db5df <redacted> + 14
7 CoreFoundation 0x322db291 <redacted> + 272
8 CoreFoundation 0x322d9f01 <redacted> + 1232
9 CoreFoundation 0x3224cebd CFRunLoopRunSpecific + 356
10 CoreFoundation 0x3224cd49 CFRunLoopRunInMode + 104
11 GraphicsServices 0x32fb52eb GSEventRunModal + 74
12 UIKit 0x34e92301 UIApplicationMain + 1120
13 TestApp 0x00081a9d main + 48
14 TestApp 0x0005aa68 start + 40
)
This is where the recorder is instantiated.
AQRecorderController.mm:
- (void)awakeFromNib
{
aqRecorder = new AQRecorder();
}
This is where the recorder is used. By this point, the AQRecorderController has been released and this code never executes (it causes a crash, because the AQRecorderController has been deallocated).
-(bool)startRecording
{
if (aqRecorder->IsRunning())
{
[self stopRecording];
}
else // If we're not recording, start.
{
#try
{
// Start the recorder
CFStringRef filenameString = (CFStringRef)CFBridgingRetain(self.fileName);
aqRecorder->StartRecord(filenameString);
}
#catch(NSException *ex)
{
NSLog(#"Error: %#", [ex description]);
return NO;
}
[self setFileDescriptionForFormat:aqRecorder->DataFormat() withName:#"Recorded File"];
}
[self checkIfRecording];
return YES;
}
Here is where the AQRecorderController is instantiated.
formViewController.mm:
//this is called in viewDidAppear
-(void)initializeAQRecorder: (NSString*)soundFileName
{
aqRecorderController = [[AQRecorderController alloc] init];
NSLog(#"AQRecorderController is being initialized for file %#",soundFileName);
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *soundFilePath =[[NSString alloc] initWithFormat:#"%#",[documentsDir stringByAppendingPathComponent:soundFileName]];
[aqRecorderController setFileName:soundFilePath];
[aqRecorderController initializeRecordSettingsWithCompression:NO];
}
My problem is that shortly after loading the view controller, the
AQRecorderController is released...I need to maintain this reference
throughout the lifetime of my view controller
Mark your property strong instead of weak. weak means that the object pointed to by aqRecorderController won't be retained by the setter; strong will cause it to be retained.
If the AQRecorderController property is strong, I get a bad access
error when trying to use it, if its weak, I just get a nil, and its unusable.
That sounds like the property is being set to some invalid value somewhere in your program. Since you can't manually retain the object under ARC and you've marked the property weak, it may be released very early on. I'm not sure why you'd have a problem if you mark it strong... it'd help to see the code where you set the variable or property.
You're never setting the AQRecorderController to your formViewController from what I see. You need to do self.aqRecorderController = aqRecorderController, I believe it's just disappearing as soon as you leave the scope where you create the controller.
I got it working for now. I haven't completely fixed it, but I can record without it crashing. I commented out every line having to do with AQRecorderController until it stopped being released, then slowly added them back until I found out where it happens. It looks like the audio session setup code somehow provokes it to release the controller. This is the code that causes it (but no errors are thrown here):
From AQRecorderController.mm:
-(void)initializeRecordSettingsWithCompression:(bool)compressionEnabled
{
OSStatus error = AudioSessionInitialize(NULL, NULL, interruptionListener, (__bridge void*)self);
if (error) printf("ERROR INITIALIZING AUDIO SESSION! %d\n", (int)error);
else
{
UInt32 category = kAudioSessionCategory_PlayAndRecord;
error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
if (error) printf("couldn't set audio category!");
error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, propListener, (__bridge void*)self);
if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", (int)error);
UInt32 inputAvailable = 0;
UInt32 size = sizeof(inputAvailable);
// we do not want to allow recording if input is not available
error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
if (error) printf("ERROR GETTING INPUT AVAILABILITY! %d\n", (int)error);
// we also need to listen to see if input availability changes
error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, propListener, (__bridge void*)self);
if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", (int)error);
error = AudioSessionSetActive(true);
if (error) printf("AudioSessionSetActive (true) failed");
}
}
So far, this isn't necessary for the functioning of my app, but I am curious as to why it would cause the AQRecorderController instance to release.

Resources