uhhh release self?

Discussion in 'iOS Development' started by Collateral, Nov 14, 2009.

  1. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    So i'm using some sample code to help teach myself how to use the in-app store...but im having some problems..


    Please Register or Log in to view images


    ok.. to solve this memory leak, what do i release?

    i REALLY dont think its :

    [OBJC][self release];[/OBJC]

    :/


    EDIT:

    To be completely honest, i have NO idea whats going on when it says

    [objc][[self alloc] init][/objc]

    What does that do?

    Sorry if this is SUPER noob question.
  2. crizh4x

    crizh4x Active Member

    Joined:
    Sep 19, 2007
    Messages:
    4,474
    Likes Received:
    17
    Device:
    2G iPod touch
    Something about that does seem right... post some more code.

    I don't see how you can have self instantiate itself if it isn't already instantiated... but perhaps I haven't learned something correctly here.
  3. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    MKStoreManager.h
    [objc]//
    // StoreManager.h
    // MKSync
    //
    // Created by Mugunth Kumar on 17-Oct-09.
    // Copyright 2009 MK Inc. All rights reserved.
    // mugunthkumar.com

    #import <Foundation/Foundation.h>
    #import <StoreKit/StoreKit.h>
    #import "MKStoreObserver.h"

    @interface MKStoreManager : NSObject<SKProductsRequestDelegate> {

    NSMutableArray *purchasableObjects;
    MKStoreObserver *storeObserver;

    }

    @property (nonatomic, retain) NSMutableArray *purchasableObjects;
    @property (nonatomic, retain) MKStoreObserver *storeObserver;

    - (void) requestProductData;

    - (void) buyFeatureA; // expose product buying functions, do not expose
    - (void) buyFeatureB; // your product ids. This will minimize changes when you change product ids later

    // do not call this directly. This is like a private method
    - (void) buyFeature

    Please Register or Log in to view images

    NSString*) featureId;

    - (void) failedTransaction: (SKPaymentTransaction *)transaction;
    -(void) provideContent: (NSString*) productIdentifier;

    + (MKStoreManager*)sharedManager;

    + (BOOL) featureAPurchased;
    + (BOOL) featureBPurchased;

    +(void) loadPurchases;
    +(void) updatePurchases;

    @end
    [/objc]
    MKStoreManager.m
    [objc]//
    // MKStoreManager.m
    //
    // Created by Mugunth Kumar on 17-Oct-09.
    // Copyright 2009 Mugunth Kumar. All rights reserved.
    // mugunthkumar.com
    //

    #import "MKStoreManager.h"


    @implementation MKStoreManager

    @synthesize purchasableObjects;
    @synthesize storeObserver;

    // all your features should be managed one and only by StoreManager
    static NSString *featureAId = @"com.mycompany.myapp.myfeatureA";
    static NSString *featureBId = @"com.mycompany.myapp.myfeatureB";

    BOOL featureAPurchased;
    BOOL featureBPurchased;

    static MKStoreManager* _sharedStoreManager; // self

    - (void)dealloc {

    [_sharedStoreManager release];
    [storeObserver release];
    [super dealloc];
    }

    + (BOOL) featureAPurchased {

    return featureAPurchased;
    }

    + (BOOL) featureBPurchased {

    return featureBPurchased;
    }

    + (MKStoreManager*)sharedManager
    {
    @synchronized(self) {

    if (_sharedStoreManager == nil) {

    [[self alloc] init]; // assignment not done here
    _sharedStoreManager.purchasableObjects = [[NSMutableArray alloc] init];
    [_sharedStoreManager requestProductData];

    [MKStoreManager loadPurchases];
    _sharedStoreManager.storeObserver = [[MKStoreObserver alloc] init];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:_sharedStoreManager.storeObserver];
    }
    }
    return _sharedStoreManager;
    }


    #pragma mark Singleton Methods

    + (id)allocWithZone

    Please Register or Log in to view images

    NSZone *)zone

    {
    @synchronized(self) {

    if (_sharedStoreManager == nil) {

    _sharedStoreManager = [super allocWithZone:zone];
    return _sharedStoreManager; // assignment and return on first allocation
    }
    }

    return nil; //on subsequent allocation attempts return nil
    }


    - (id)copyWithZone

    Please Register or Log in to view images

    NSZone *)zone
    {
    return self;
    }

    - (id)retain
    {
    return self;
    }

    - (unsigned)retainCount
    {
    return UINT_MAX; //denotes an object that cannot be released
    }

    - (void)release
    {
    //do nothing
    }

    - (id)autorelease
    {
    return self;
    }


    - (void) requestProductData
    {
    SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers:
    [NSSet setWithObjects: featureAId, featureBId, nil]]; // add any other product here
    request.delegate = self;
    [request start];
    }


    - (void)productsRequest

    Please Register or Log in to view images

    SKProductsRequest *)request didReceiveResponse

    Please Register or Log in to view images

    SKProductsResponse *)response
    {
    [purchasableObjects addObjectsFromArray:response.products];
    // populate your UI Controls here
    for(int i=0;i<[purchasableObjects count];i++)
    {

    SKProduct *product = [purchasableObjects objectAtIndex:i];
    NSLog(@"Feature: %@, Cost: %f, ID: %@",[product localizedTitle],
    [[product price] doubleValue], [product productIdentifier]);
    }

    [request autorelease];
    }

    - (void) buyFeatureA
    {
    [self buyFeature:featureAId];
    }

    - (void) buyFeature

    Please Register or Log in to view images

    NSString*) featureId
    {
    if ([SKPaymentQueue canMakePayments])
    {
    SKPayment *payment = [SKPayment paymentWithProductIdentifier:featureId];
    [[SKPaymentQueue defaultQueue] addPayment

    Please Register or Log in to view images

    ayment];
    }
    else
    {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle

    Please Register or Log in to view images

    "MyApp" message

    Please Register or Log in to view images

    "You are not authorized to purchase from AppStore"
    delegate:self cancelButtonTitle

    Please Register or Log in to view images

    "OK" otherButtonTitles: nil];
    [alert show];
    [alert release];
    }
    }

    - (void) buyFeatureB
    {
    [self buyFeature:featureBId];
    }


    - (void) failedTransaction: (SKPaymentTransaction *)transaction
    {
    NSString *messageToBeShown = [NSString stringWithFormat

    Please Register or Log in to view images

    "Reason: %@, You can try: %@", [transaction.error localizedFailureReason], [transaction.error localizedRecoverySuggestion]];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle

    Please Register or Log in to view images

    "Unable to complete your purchase" message:messageToBeShown
    delegate:self cancelButtonTitle

    Please Register or Log in to view images

    "OK" otherButtonTitles: nil];
    [alert show];
    [alert release];
    }

    -(void) provideContent: (NSString*) productIdentifier
    {
    if([productIdentifier isEqualToString:featureAId])
    featureAPurchased = YES;

    if([productIdentifier isEqualToString:featureBId])
    featureBPurchased = YES;

    [MKStoreManager updatePurchases];
    }


    +(void) loadPurchases
    {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    featureAPurchased = [userDefaults boolForKey:featureAId];
    featureBPurchased = [userDefaults boolForKey:featureBId];
    }


    +(void) updatePurchases
    {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:featureAPurchased forKey:featureAId];
    [userDefaults setBool:featureBPurchased forKey:featureBId];
    }
    @end
    [/objc]
  4. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    You should read up on object creation, overriding methods, and memory management.



    [objc]
    - (id)init {

    if (self = [super init]) {
    // do stuff
    }
    return self
    }
    [/objc]

    What we did:

    1. Assign the memory address of the object returned from super's init method to 'self' (current instance).

    2. Ensure that the memory address of 'self' is not nil (in other words: check if it exists).

    3. If 'self' exists, proceed with whatever initialization code you have.

    4. return self.

    Anyway, I don't know if that's exactly what you are struggling with, due to the limited amount of code you posted.

    EDIT: you need to assign _sharedStoreManager to the return value of init. Right now, you are simply returning nil.

    You can ignore the first part of my post as it is not exactly relevant to what you're doing, though it may help you with other things.
  5. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)

    uhh alright?

    How exactly would i accomplish that?

    [objc]init = _sharedStoreManager;
    return init;[/objc]

    ?

    That definitely does not seem correct

    Sorry. Im teaching myself OBJ-C and im not very good.
  6. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    _sharedStoreManager = [[MKStoreManager alloc] init];

    Assign the memory address.

    EDIT: It's best to learn, and then work on projects, rather than attempt to do both at the same time. I learned that the hard way.
  7. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    Thanks.

    And yea, im starting to learn that. haha. Could you recomend any good books/sites? Cause the apple docs just bore the living crap out of me.
  8. crizh4x

    crizh4x Active Member

    Joined:
    Sep 19, 2007
    Messages:
    4,474
    Likes Received:
    17
    Device:
    2G iPod touch
  9. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    Boo theres still a leak.

    Please Register or Log in to view images



    Ive also tried releasing it a million different ways.

    I am seriously at a loss.
  10. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    Why You have _sharedStoreManager = [[MKStoreManager alloc] init] and [[self alloc] init]?

    It look you init same class twice.

    why not to replace [[self alloc] init] to _sharedStoreManager = [[MKStoreManager alloc] init] ?

Share This Page