Barry Nailor Barry Nailor - 1 year ago 102
Objective-C Question

EXC_BAD_ACCESS during in app purchase test

Running a test for my in app purchase (first time doing in app purchases). I get EXC_BAD_ACCESS on the third line of this code:

SKPayment *payment = [SKPayment paymentWithProduct:electronicProd];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];

The is under an IBAction for a button. electronicPack is declared in the header as a SKProduct. Threw some NSLogs in the productsRequest didReceiveResponse, and when the product was requested (in the viewDidLoad) and they showed it was correctly fetching the product and storing it in electronicPack. Defined electronicPack as [[request.products] objectAtIndex:0] in the didReceiveResponse page. So yea. Thats where im at, dont know what to do. Any help is appreciated.

UPDATE: FIXED accidentally left in code that was adding an extra transaction observer lol

Answer Source

You need to retain the object that you are creating

- (void)viewDidLoad {
   //... stuff
   SKProduct* electronicProduct = //...
   [electronicProduct retain];
   //... otherstuff

viewDidLoad is wrapped by the system in a autorelease pool, paymentWithProduct: returns an autorelease object. When viewDidLoad is done, all autorelease object are released, that's why you get a bad memory access when you try to access to it later.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download