SKProductsRequest导致崩溃 - Cocos2d和In App Purchase

时间:2011-05-01 11:49:42

标签: ios iphone sdk

“In App Purchase”和Cocos2D引擎有一个非常奇怪的问题。 一切似乎都运作良好,交易没有问题。 但是当我打开和关闭几次使用“在app app”事务中的场景时,应用程序崩溃了。有时它会在几分钟后发生。

.m文件:

在我的初始化功能中:

  if ([SKPaymentQueue canMakePayments]) {
    productsRequest=[[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects: @"xxx.blockads", @"xxx.unlocklevels",nil]];
    productsRequest.delegate = self;
    [productsRequest start];
    } else {
    // 
    }

与“In App”相关的其他功能

-(void) unlock1: (id) sender {

     payment = [SKPayment paymentWithProductIdentifier:@"xxx.blockads"];

     [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

     [[SKPaymentQueue defaultQueue] addPayment:payment];

}


-(void) unlock2: (id) sender {

     payment = [SKPayment paymentWithProductIdentifier:@"xxx.unlocklevels"];

     [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

     [[SKPaymentQueue defaultQueue] addPayment:payment];

}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

     SKProduct *validProduct = nil;
     int count = [response.products count];
     if (count > 0) {

          validProduct = [response.products objectAtIndex:0];
     } else if (!validProduct) {

     }

     NSLog(@"Valid products: %@", response.products);
     NSLog(@"invalid products: %@", response.invalidProductIdentifiers);
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

     for (SKPaymentTransaction *transaction in transactions) {

          switch (transaction.transactionState) {

               case SKPaymentTransactionStatePurchasing:


                                  break;

               case SKPaymentTransactionStatePurchased:
                                   //Something
                    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

                    break;


               case SKPaymentTransactionStateRestored:

                    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

                    break;

               case SKPaymentTransactionStateFailed:

                    if (transaction.error.code != SKErrorPaymentCancelled) {

                         NSLog(@"An error encounterd");
                    }

                    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

                    break;

          }
    }
}

崩溃日志:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)

Exception Codes: KERN_INVALID_ADDRESS at 0xd0000008
Crashed Thread:  0
Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:

0   libobjc.A.dylib                    0x35db9c98 objc_msgSend + 16
1   StoreKit                           0x31c77d96 -[SKProductsRequest handleFinishResponse:returningError:] + 38
2   StoreKit                           0x31c79086 -[SKRequest _requestFinishedNotification:] + 150
3   Foundation                         0x3010f17c _nsnote_callback + 136
4   CoreFoundation                     0x31091208 __CFXNotificationPost_old + 396
5   CoreFoundation                     0x3102bee4 _CFXNotificationPostNotification + 112
6   Foundation                         0x3010c5cc -[NSNotificationCenter postNotificationName:object:userInfo:] + 64
7   AppSupport                         0x325ae44e -[CPDistributedNotificationCenter deliverNotification:userInfo:] + 38
8   AppSupport                         0x325af77a _CPDNDeliverNotification + 198
9   AppSupport                         0x325ae1d0 _XDeliverNotification + 116
10  AppSupport                         0x325a4dd0 migHelperRecievePortCallout + 132
11  CoreFoundation                     0x31099a90 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 20
12  CoreFoundation                     0x3109b838 __CFRunLoopDoSource1 + 160
13  CoreFoundation                     0x3109c606 __CFRunLoopRun + 514
14  CoreFoundation                     0x3102cebc CFRunLoopRunSpecific + 224
15  CoreFoundation                     0x3102cdc4 CFRunLoopRunInMode + 52
16  GraphicsServices                   0x32215418 GSEventRunModal + 108
17  GraphicsServices                   0x322154c4 GSEventRun + 56
18  UIKit                              0x32795d62 -[UIApplication _run] + 398
19  UIKit                              0x32793800 UIApplicationMain + 664
20  ninjaballhd                        0x00003b96 main (main.m:13)
21  ninjaballhd                        0x00003b68 start + 32

Thread 1 name:  Dispatch queue: com.apple.libdispatch-manager
Thread 1:

0   libsystem_kernel.dylib             0x3002afbc kevent + 24
1   libdispatch.dylib                  0x366ee094 _dispatch_mgr_invoke + 672
2   libdispatch.dylib                  0x366ef04a _dispatch_queue_invoke + 86
3   libdispatch.dylib                  0x366ee60a _dispatch_worker_thread2 + 190
4   libsystem_c.dylib                  0x3624358a _pthread_wqthread + 258
5   libsystem_c.dylib                  0x36243bbc start_wqthread + 0


Thread 2:

0   libsystem_kernel.dylib             0x3002a3ec __workq_kernreturn + 8
1   libsystem_c.dylib                  0x362436d8 _pthread_wqthread + 592
2   libsystem_c.dylib                  0x36243bbc start_wqthread + 0

Thread 3 name:  WebThread

Thread 3:

0   libsystem_kernel.dylib             0x30027c00 mach_msg_trap + 20
1   libsystem_kernel.dylib             0x30027758 mach_msg + 44
2   CoreFoundation                     0x3109a2b8 __CFRunLoopServiceMachPort + 88
3   CoreFoundation                     0x3109c562 __CFRunLoopRun + 350
4   CoreFoundation                     0x3102cebc CFRunLoopRunSpecific + 224
5   CoreFoundation                     0x3102cdc4 CFRunLoopRunInMode + 52
6   WebCore                            0x32ccb37a RunWebThread(void*) + 378
7   libsystem_c.dylib                  0x3624230a _pthread_start + 242
8   libsystem_c.dylib                  0x36243bb4 thread_start + 0



Thread 4 name:  AURemoteIO::IOThread

Thread 4:
0   libsystem_kernel.dylib             0x30027c00 mach_msg_trap + 20
1   libsystem_kernel.dylib             0x30027758 mach_msg + 44
2   AudioToolbox                       0x32308824 AURemoteIO::IOThread::Run() + 80
3   AudioToolbox                       0x3230e342 AURemoteIO::IOThread::Entry(void*) + 2
4   AudioToolbox                       0x3224348a CAPThread::Entry(CAPThread*) + 138
5   libsystem_c.dylib                  0x3624230a _pthread_start + 242
6   libsystem_c.dylib                  0x36243bb4 thread_start + 0

2 个答案:

答案 0 :(得分:6)

这很可能是内存管理问题。 handleFinishResponse:returningError:可能是一个对象的内部方法,因为你的内存管理错误而被解除分配 在从IAP收到响应之前,您解除了处理应用内购买的控制器的负担。由于来自互联网连接的延迟响应,选择器被发送到解除分配的对象。

Stackoverflow上还有另一个question具有完全相同的异常,原因是内存管理问题。

使用NSZombieEnabled检查这是否是异常的原因

来自cocoadev.com:

  • 双击Xcode项目的可执行文件组中的可执行文件。
  • 单击Arguments选项卡。
  • 在“要在环境中设置的变量:”部分中,创建一个名为“NSZombieEnabled”的变量,并将其值设置为“YES”。

答案 1 :(得分:0)

handleFinishResponse:returningError:的代码在哪里 - 如果不存在,那么这会导致崩溃......

还要将以下代码添加到场景中,以便停止创建多个事务观察器:

- (void)onExit {
    [super onExit];
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}