Greg Cawthorne
Greg Cawthorne

Reputation: 416

EXC_BAD_ACESS when buying an in app purchase, only after going into the store the second time

Okay. I have in-app purchases working i my application. I have a view controller that acts as the store of my application, and the user can buy things from it.

You can buy things fine (as many purchases as you want) the first time you go into the store, but if you exit out of the store then go back into it it becomes temperamental. Some times it will contrinue to work a bit, but all of the time it eventually crashes with 'EXC_BAD_ACCESS'.

It crashes on the add payment line:

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
SKMutablePayment *payment = [[SKMutablePayment alloc] init];
payment.productIdentifier = @"uk.co.exampleEmail_name.MaxBuys";
[[SKPaymentQueue defaultQueue] addPayment:payment];

but also sometimes in the main.m file at:

int main(int argc, char *argv[])
{
@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}

Another similar question said it was because it had other code adding more than one transaction observer, but this the only place I call it. But of course it calls it once every purchase attempt. I do the productsrquest delegate commands in the view did load.

Any help will be appreciated.

Upvotes: 0

Views: 469

Answers (1)

Mehmood
Mehmood

Reputation: 931

The error message indicates a message is being sent to a deallocated instance of InAppPurchaseManager, which is your class. And it's happening after you open the view (creating an instance), close the view (releasing an instance), then opening the view again (creating a second instance). And the problem is happening within the addPayment: call. This indicates that the framework still has a handle on your old, released instance, and is trying to send it a message.

You give the framework a handle to your object in loadStore, when you call

  [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

I don't see anywhere where you remove self as an observer. Objects that send out notifications usually do not retain their observers, since doing so can create a retain cycle and/or a memory leak.

In your dealloc code you need to cleanup and call removeTransactionObserver:. That should solve your problem.

- (void)dealloc {

    [[SKPaymentQueue defaultQueue]removeTransactionObserver:self];

    [super dealloc];
}

Upvotes: 4

Related Questions