0

I have a c style array used in objective c .m file:

Gift gifts[3];

And Gift is a struct defined as this:

typedef struct {
    NSString *name;
    CCSprite *sprite;
}Gift;

This is how I use the array

for (int i=0; i<3; i++) {
    gifts[i].name = [[NSString stringWithFormat:@"Reward%d_%@.png", i+1, location] retain];
    gifts[i].sprite = [CCSprite spriteWithSpriteFrameName:gifts[i].name];
    CGPoint pos = [[GameConfigure sharedManager] getCoordinateforKey:@"REWARD_ITEM"];
    gifts[i].sprite.position = ccp(pos.x+i*238, pos.y);
    [rewardLayer addChild:gifts[i].sprite z:1 tag:100+i];
}

How do I manage the memory?
Do I need to free the C array?

3
  • how and where do you declare your "gifts" array? Commented May 9, 2012 at 7:59
  • In the header file: Gift gifts[3]; Commented May 9, 2012 at 8:04
  • Seems to be a duplicate to stackoverflow.com/questions/9869883/… Commented May 9, 2012 at 8:31

2 Answers 2

0

There is no need to free the array, as you don't allocate it.

Are you sure that spriteWithSpriteFrameName doesn't return an autoreleased object (its naming convention suggests it does)?

Sign up to request clarification or add additional context in comments.

2 Comments

spriteWithSpriteFrameName returns an autorelease object. Does this matter?
@DragonDoingPragramming Yes, it matters - just as you have done with the NSString, you need to retain it.
0

you don't have to free C array , but you should manage the Object in the C array

in your code

for (int i=0; i<3; i++) {

    //retain is good, but if you don't release it at the end ,  it leaks!!!!
    gifts[i].name = [[NSString stringWithFormat:@"Reward%d_%@.png", i+1, location] retain];

    //here is not a good idea, why you don't retian here?  it will be autorelease 
    gifts[i].sprite = [CCSprite spriteWithSpriteFrameName:gifts[i].name];

    CGPoint pos = [[GameConfigure sharedManager] getCoordinateforKey:@"REWARD_ITEM"];
    gifts[i].sprite.position = ccp(pos.x+i*238, pos.y);
    [rewardLayer addChild:gifts[i].sprite z:1 tag:100+i];
}

you'd better create a NSObject instead of the struct GIFT

use Property of the member

@interface GIFT : NSObject
{
    NSString *name;
    CCSprite *sprite;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) CCSprite *sprite;

@end

and use NSArray instead of C Array

NSArray *gifts = [NSArray arrayWithObjects:
                                         [[[GIFT alloc] init] autorelease],
                                         [[[GIFT alloc] init] autorelease],
                                         [[[GIFT alloc] init] autorelease],
                                         nil];

but if you still want to use C array, try to manage the reference count in the struct of your GIFT

4 Comments

@interface GIFT : NSObject { NSString *name; CCSprite *sprite; } I have never seen this kind of implementation, can thou explain a bit?
eeee..., what means you have never seen? it's the definition of a Object in Objective-C just like NSString and CCSprite and etc..
Hmm... Yeap :) I could not recognize it, haha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.