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
What is the difference between

NSString *str = @"hello";

NSString *str = [NSString stringWithFormat:mad:"hello"];

NSString *str = [NSString alloc]initWithString:mad:"hello"];

If the 2nd is a convenient constructor, why we want to use/bother about "alloc-init" combination?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
The third will have a retain count of 1 so will stick around until released. The second will be autoreleased and will get deallocated from RAM at the next autorelease pool flush, normally at the end of this run loop.

Really you should read and understand the memory management guide before writing any code.
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
The third will have a retain count of 1 so will stick around until released. The second will be autoreleased and will get deallocated from RAM at the next autorelease pool flush, normally at the end of this run loop.

Really you should read and understand the memory management guide before writing any code.

I went through the docs...
Still i am not getting the diff in memory level....
I also noticed that for some types eventhough i gave

newObject =[oldObject copy]

both newObject and oldObject are sharing same pointer....

I got a sugestion to use mutableCopy... (Why copy didint worked?)

Thanks....
( I wish GC should be in iPhone also...)
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I went through the docs...
Still i am not getting the diff in memory level....

Then read it again. Understanding the difference between retained and autoreleased objects is critical.

I also noticed that for some types eventhough i gave

newObject =[oldObject copy]

both newObject and oldObject are sharing same pointer....

I got a sugestion to use mutableCopy... (Why copy didint worked?)
I imagine this is a performance optimisation in Apples code. There is no reason that copying an immutable string (for example) should return a new object: nothing can be done to alter the string so the copy pointing at the original object is fine as long as the original object gets it's retain count incremented. Unless you actually need a mutable copy don't ask for one: you will use more RAM and that is a precious resource on the iPhone.

I don't find the lack of GC to be an issue at all: managing your own memory is very easy once you learn the basic rules.

If the Apple document is too complicated (and it's really not), perhaps you should read this.
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Then read it again. Understanding the difference between retained and autoreleased objects is critical.


I imagine this is a performance optimisation in Apples code. There is no reason that copying an immutable string (for example) should return a new object: nothing can be done to alter the string so the copy pointing at the original object is fine as long as the original object gets it's retain count incremented. Unless you actually need a mutable copy don't ask for one: you will use more RAM and that is a precious resource on the iPhone.

I don't find the lack of GC to be an issue at all: managing your own memory is very easy once you learn the basic rules.

If the Apple document is too complicated (and it's really not), perhaps you should read this.

Okie....

What care we want to take for objects which will get initialized with Unarchived files???

say...

myObj =[NSKeyedUnarchiver unarchiveObjectWithFile:myFilePath];

the 'myObj' is going invalid at some part of code....

Is this stmnt is enough or i want to explicitily alloc it before coming to above line??
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Then read it again. Understanding the difference between retained and autoreleased objects is critical.


I imagine this is a performance optimisation in Apples code. There is no reason that copying an immutable string (for example) should return a new object: nothing can be done to alter the string so the copy pointing at the original object is fine as long as the original object gets it's retain count incremented. Unless you actually need a mutable copy don't ask for one: you will use more RAM and that is a precious resource on the iPhone.

I don't find the lack of GC to be an issue at all: managing your own memory is very easy once you learn the basic rules.

If the Apple document is too complicated (and it's really not), perhaps you should read this.

Okie....

What care we want to take for objects which will get initialized with Unarchived files???

say...

myObj =[NSKeyedUnarchiver unarchiveObjectWithFile:myFilePath];

the 'myObj' is going invalid at some part of code....

Its not invalid through out the application... But in applicationWillTerminate: i am checking object and found its invalid...

At which point, the autorelease objects will get released??
Is it before coming to applicationWillTerminate method???

Is this stmnt is enough or i want to explicitily alloc it before coming to above line??
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
myObj =[NSKeyedUnarchiver unarchiveObjectWithFile:myFilePath];

Does this method use the word init or copy? No? Then the returned object is autoreleased. Once again read and understand the links above. They make this rule very clear.

Is this stmnt is enough or i want to explicitily alloc it before coming to above line??

alloc will do nothing: all that does is allocate memory which you don't want to do. At best calling alloc will leak memory, at worst it will cause your application to crash.

As with every other autoreleased object if you want it to stick around you need to retain it to take ownership of it.

Seriously: if you can't understand this having read the memory management documents you should probably not be writing software.
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Seriously: if you can't understand this having read the memory management documents you should probably not be writing software.

Hi robbie,
Thanking You for such a valuable ADVICE...
Its a "GREAT INSPIRATION AND ADVICE :eek:" for those who are coming to this field...
Please understand that no one is born with loaded-knowledge of Mem-mgmnt.

Still i am thanking you for the support..
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.