Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Hi all..

In my iPhone app,
i make use of a static object...

The getter and setter method for the static object is as follows...
Code:
+(cart *)getMyCart
{
	if(staticCartObject == nil)
	{
		
		staticCartObject = [[cart alloc]init];
	    staticCartObject.productArray = [[NSMutableArray alloc]init];	
	}
	return cartObject;
}

+(void)setCart:(cart*)newCart
{
     [staticCartObject release];
     [newCart retain];
     staticCartObject=newCart;
}

Hope the above code is OK....


In one of my viewController i am doing an assignment like

Code:
localCartObjectInFirstViewController = [cart getCartObject];

I know its a "copy by reference"...

But i want the "localCartObjectInFirstViewController" to have a separate memory containing the return value of [cart getMyCart];

If i allocate memory for localCartObjectInFirstViewController and do the above will result in a memory leak , right???

So whats the solution???
 

aLoC

macrumors 6502a
Nov 10, 2006
726
0
Maybe try this:

localCartObjectInFirstViewController = [[cart getCartObject] copy];
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Maybe try this:

localCartObjectInFirstViewController = [[cart getCartObject] copy];

Still... both are at same memory....

I tried for strings as well..

But "mutableCopy" worked fine...
Then my question is which of the folllowing is right???

NSString str1 =[[NSString alloc]init];
str1 = [originalStr mutableCopy];


OR

NSString str1 = [originalString mutableCopy];

What the difference between two ??

If both are fine, why we want to allocate, adding burden of release and all??
 

aLoC

macrumors 6502a
Nov 10, 2006
726
0
NSString str1 =[[NSString alloc]init];
str1 = [originalStr mutableCopy];


OR

NSString str1 = [originalString mutableCopy];

What the difference between two ??

There are two string classes in Cocoa, one where the text can't change after it is initialized (NSString) and one where is can (NSMutableString). If you have ever used Java, it is the same as String and StringBuffer.

The first example creates an NSString, then makes an NSMutableString object with the same text. Both of these objects will have to be released at some point.

The second example just creates a mutable copy of a String given you already have one. The reason you get the same memory when you use [NSString copy] is that since NSString's text can't be changed, it is ok for everyone to share the same instance.
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
There are two string classes in Cocoa, one where the text can't change after it is initialized (NSString) and one where is can (NSMutableString). If you have ever used Java, it is the same as String and StringBuffer.

The first example creates an NSString, then makes an NSMutableString object with the same text. Both of these objects will have to be released at some point.

The second example just creates a mutable copy of a String given you already have one. The reason you get the same memory when you use [NSString copy] is that since NSString's text can't be changed, it is ok for everyone to share the same instance.

Okie aLoC...

Still a doubt....

first example creates a string with some memory allocated ,right??

So my question is the mutableCopy i am assigning will go to the same memory which it allocated in previous line? OR that alloc will cause a leak
as mutableCopy returns same string with some address defined by them?

Thing is that i am having a collection of items (item name, item porice, item description etc...) in ItemClass.

But user will select some items , (all will store in a class FavoriteList).

I am just assigning it during he add this items to his favorite list.
But i found that those items in FavoriteList are sharing the same address with its location in ItemClass.

I want both as separate entities, as those change in fav list dont reflect itemClass.
 

aLoC

macrumors 6502a
Nov 10, 2006
726
0
I want both as separate entities, as those change in fav list dont reflect itemClass.

You need to add a method to your Item class to make it return a copy of itself, e.g.
Code:
@implementation Item

- (id)copyWithZone:(NSZone *)zone
{
    Item *copyOfMe = [[Item allocWithZone:zone] init];
    // OR: if your superclass already has an allocWithZone method, do this instead:
    // Item *copyOfMe = [super copyWithZone:zone];

    [copyOfMe setName:[[self name] copy]];
    [copyOfMe setPrice:[[self price] copy]];
    [copyOfMe setDescription:[[self description] copy]];
    // etc.

    return copyOfMe;
}

@end

Then when you want to copy an object (e.g. before adding it to the favorites) do it like so:
Code:
Item item2 = [item1 copy];

This will give two wholly independent Item objects.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.