EXC_BAD_ACCESS when adding and removing a view from different views

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

My problem is that if I display the store once in the menu and then play the game and reach the store in the game on the screen and try to buy something, the application crashes the EXC_BAD_ACCESS error without much information. (Violation at

[[SKPaymentQueue defaultQueue] addPayment:lPayment];

      

in the ButtonPressed activity in my ShopViewController when trying to buy an IAP.

My view is configured like this:

Menu β†’ Ladderview β†’ Gameview β†’ ShopView

and

Menuview β†’ Shopview

I hope you can help me identify the error,

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

It seems that I can reproduce the error from Menu -> Shopview without using the game view. I can do this by clicking the buy button, clicking Cancel, go back to the menu, go back to the store and try again. On the 3-4th attempt, it falls in one line. Here is the entire button click 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"];
    }
}

      

It may seem that lPayment is being released. I even tried to install

mProductIds = nil;
mPriceArray = nil;

      

when i delete the store view trying to get it to highlight it again when i reload the store but with no luck.

thank

+3


source to share


3 answers


Your problem is a dangling pointer. EXC_BAD_ACCESS - This is the CPU moaning that you are accessing non-existent memory or memory that is outside your scope. The reason is the inability to get an object that causes an early deal and then gets overwritten. At this time (which may be delayed) the pointer will point to the garbage whose tracing (class consideration) causes the EXC_BAD_ACCESS to be thrown. This error cannot be detected using @try. There is speculation that the stack itself is corrupt, making it impossible to continue (although this is most likely not the case), which will cause the debugger to rotate, whose output is no longer present in many areas. It's like an out-of-control anarchy where the processor flushes important registers and takes a long jump.

consider automatic reference counting. In you already have it, consider that properties like delegates are not persisted by the host object. Any property that can logically contain self will not store any value in it. ARC won't help you.



in your case: defaultQueue is probably fine. lPayment was probably released.

+3


source


Try to trace the problem by first enabling NSZombie. In case of a problem with EXC_BAD_Access for a while (NSZombie) it becomes more useful to keep track of the deallocated object than just guessing where the problem is.



+1


source


It is difficult to tell from the information provided, but it could be the following: Your expression

SKPayment *lPayment = [SKPayment paymentWithProduct:[mPriceArray objectAtIndex:lTag]];  

      

creates an instance of the SKPayment object and passes it to the current autofill pool. If this pool does not exist (this may be the case if the code is running on a separate thread for which the auto-registration pool is not explicitly configured), the object is released immediately, and your statement

[[SKPaymentQueue defaultQueue] addPayment:lPayment];  

      

is accessing invalid memory.

+1


source







All Articles