Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS when adding and removing a view from different views

I have created a game with a in game shop view and view controller. The shop can be accessed in then menu (ViewController.m) and from the Game Over screen (GameViewController.m).

My problem is that if I have displayed the shop once in the menu, and then play a game and access the shop in the game over screen and try to buy something, the app crashes trowing a EXC_BAD_ACCESS error without much info. (Breaking at

[[SKPaymentQueue defaultQueue] addPayment:lPayment];

line in the ButtonPressed action in my ShopViewController when trying to buy a IAP.

My view are set up like this:

Menuview -> Ladderview -> Gameview -> ShopView

and

Menuview -> Shopview

Hope you can help me pinpoint the error,

EDIT -----------

It seems that I can reproduce the error from the menu -> Shopview without using the game view. I can do this by pressing a "buy button", pressing cancel, navigate back to the menu, go back to the shop, and repeat. On the 3-4th attempt it crashes at the same line. Here is the entire button pressed method:

- (void)buyButtonPressed:(UIButton *)pButton {
    NSInteger lTag = [pButton tag];
    //////NSLog(@"Button tag: %i"), lTag;

    Reachability *lReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus lCurrentNetworkStatus = [lReachability currentReachabilityStatus];
    if (lCurrentNetworkStatus != NotReachable) {
        if ([SKPaymentQueue canMakePayments]) {
            SKPayment *lPayment = [SKPayment paymentWithProduct:[mPriceArray objectAtIndex:lTag]];
            [[SKPaymentQueue defaultQueue] addPayment:lPayment];
            [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

        } else {
            [self showAlertViewWithText:@"Purchases are disabled. Please check your settings for General -> Restrictions -> In-App Purchases and try again." andTitle:@"Warning"];
        }
    } else {
        [self showAlertViewWithText:@"No network connection!" andTitle:@"Warning"];
    }
}

So it might seem as the lPayment is being deallocated. I even tried to set

mProductIds = nil;
mPriceArray = nil;

when I remove the shop view, trying to force it to allocate it again when I reload the shop, but without any luck.

Thanks

like image 852
CCDEV Avatar asked Dec 10 '25 20:12

CCDEV


2 Answers

Your issue is a dangling pointer. EXC_BAD_ACCESS is the CPU moaning that you are addressing non-existent memory or memory which is outside of your access rights area. The cause is a lack of retainment of an object which causes early deallocation and then is overwritten. At which time (which may be delayed), the pointer will point to garbage whose dereference (class examination) causes an EXC_BAD_ACCESS to be thrown. This error canNot be caught using @try. There is an assumption here that the stack itself is corrupt causing continuation to be impossible (although such is most likely not the case), which will throw the debugger for a spin, whose current state output is already lacking in many areas. It is like uncontrollable anarchy when the CPU resets important registers and performs a long jump.

consider Automatic Reference Counting. In you are already there, consider that delegate-like properties are not retained by the host object. Any property which could logically contain self will not retain any value stored in it. ARC will not help you there.

in your case: defaultQueue is probably good. lPayment has probably been deallocated.

like image 190
carmin Avatar answered Dec 12 '25 10:12

carmin


Try to trace the problem at first enabling NSZombie . In case of EXC_BAD_Access Problem some time it(NSZombie ) becomes more useful to trace deallocated object than simple guessing where the problem is.

like image 26
P4ul Avatar answered Dec 12 '25 09:12

P4ul