2011-05-01 108 views
2

我有一個非常奇怪的問題,與「內應用程序購買」和Cocos2D引擎。 一切似乎都很好,並且交易沒有問題。 但是,當我打開和關閉幾次使用「在應用程序購買」事務中的場景時,應用程序崩潰。有時會在幾分鐘後發生。SKProductsRequest導致崩潰 - Cocos2d和應用程序內購買

.m文件:

在我的初始化函數:有關 「應用程序內」

-(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; 

      } 
    } 
} 

崩潰日誌

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

其他功能:

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 

回答

6

最可能這是一個內存管理問題。 handleFinishResponse:returningError:可能是因爲您的內存管理錯誤而被釋放的對象的內部方法。
您可以在收到IAP響應之前解除處理應用內購買的控制器。由於來自互聯網連接的延遲響應,選擇器被髮送到解除分配的對象。

在Stackoverflow上還有另外一個question,其中有一個完全相同的異常,原因是內存管理問題。

使用NSZombieEnabled檢查,如果這是一個例外

從cocoadev.com的原因:可執行文件組Xcode項目的

  • 雙擊可執行文件。
  • 單擊參數選項卡。
  • 在「要在環境中設置的變量:」部分中,創建一個名爲「NSZombieEnabled」的變量並將其值設置爲「YES」。
+0

我得到「***任何信息 - [TransactionScene respondsToSelector:]:消息發送到解除分配的實例0x28a980',那麼我現在應該怎麼做? – Dawid 2011-05-01 15:00:37

+1

嘗試取消啓動它們的控制器的dealloc中的所有活動請求。取消後,將請求的委託設置爲零。 – 2011-05-01 15:08:13

+2

我已添加productsRequest.delegate = nil;到我的dealloc函數,現在看起來沒問題。感謝幫助 – Dawid 2011-05-01 15:51:39

0

的代碼在哪裏?- 如果不存在的話是有道理的,這是導致飛機失事的原因....

而且下面的代碼添加到您的場景,以停止創建多個事務觀察家:

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

是的,代碼 'handleFinishResponse:returningError:' 不存在,但在文檔中,輔導我做的不是 「handleFinishResponse」 – Dawid 2011-05-01 12:50:47