MrPink
MrPink

Reputation: 1371

message sent to deallocated instance in app purchase iphone

I have implemented in app purchases into my app and I currently face a critical error view the code below

@implementation Credits


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    if ([SKPaymentQueue canMakePayments]) {
        NSLog(@"PARENTAL CONTROL DISABLED");

        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"com.2sms.twosmsapp.credits.1"]];
        productsRequest.delegate = self;
        [productsRequest start];
    }
    else {
        NSLog(@"PARENTAL CONTROL ENABLED");
    }


    [super viewDidLoad];

}

-(IBAction)purchase100Credits{

    SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.2sms.twosmsapp.credits.1"];
    [[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 {
        NSLog(@"NO PRODUCTS AVAILIABLE");
    }

}

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

    for(SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchasing:

                break;

            case SKPaymentTransactionStatePurchased:

                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;

            case SKPaymentTransactionStateFailed:
                if (transaction.error.code != SKErrorPaymentCancelled)
                {
                    NSLog(@"AN ERROR ENCOUNTERED");
                }

                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
        }
    }


}




- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [productsRequest release];
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
    [super dealloc];

}

The button successfully gets the information for the itunes store however when i leave the view and then come back to it i receive

-[Credits respondsToSelector:]: message sent to deallocated instance 0x1a8810

This is giving me a headache! i believe its a memory management issue but I'm new to this and cannot find the problem :(

Upvotes: 3

Views: 1037

Answers (5)

Shayno
Shayno

Reputation: 808

I had the same problem, quite simple to solve really:

 -(void)dealloc{

    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}

You shouldn't get the error anymore

Upvotes: 0

pocjoc
pocjoc

Reputation: 510

I had the same error, a thing than is not correct in the code is when you alloc and initiate de products request:

productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet     setWithObject:@"com.2sms.twosmsapp.credits.1"]];
    productsRequest.delegate = self;
    [productsRequest start];

and in the dealloc only deallocate the productsRequest, before there is a real dealocation, the productsRequest can call to the delegate, the view that does not exists -> ERROR

One thing that prevent this error is to set the delegate to nil before the release:

productsRequest.delegate = nil;
[productsRequest release];

This solve my problem.

Hope this helps.

Upvotes: 2

voidzm
voidzm

Reputation: 649

It looks like your Credits object is being deallocated before you finish using it. Try turning on the environmental variable NSZombieEnabled to track deallocated objects, and messages sent to them. Just be sure to turn NSZombieEnabled off when you are done!

Also, if you are using Garbage Collection, make sure you always have a strong reference to the Credits object. That will keep GC from releasing and deallocating it, if GC is on.

Upvotes: 0

visakh7
visakh7

Reputation: 26400

The error suggests that you are trying to access something which has been released. So make sure you do not release the objects until you are certain that they are no longer needed. In your case when you get back to the view you are trying to use an object which is released, so please check the release of your objects.

Upvotes: 0

iPhoneDv
iPhoneDv

Reputation: 1969

I think, In the Credit class, you are using any released instance. Check your released components. There may be 2 reasons for the same case: 1. You are using any instance without allocating & initializing it. or 2. You are using any instance which is already released.

Upvotes: 0

Related Questions