small medium large xlarge

12 Apr 2009, 05:53
Vip Malixi (7 posts)

Hi Bill

In the memory screencast, you say you only need to release that which you created or retained.

What’s not clear to me is, when you create an NSString this way:

NSString *myString = @”some text”;

Since I created this, do I need to release it? In some sites they say I don’t because it is a “compile time constant string object” and I don’t know what that means.

If I created the NSString this way, I’m pretty sure I need to release it right?

NSString *myString = [[NSString alloc] init];

Also, when we create pointers to ordinary C types, do we need to “release” or dealloc or do something with these? For example…

while (1) { int *someIntPointer; char *someCharPointer.



So I “created” these, but do I need to release or dealloc them, and do we use dealloc() for C types?

Thanks again for your brilliant screencasts.

15 Apr 2009, 16:31
Vip Malixi (7 posts)

Hi Bill.

I just want to follow up with my question: when you create Objects in IB, do you need to release them or will IB handle the releasing part?

How about with pointers you create with plain C? Do you need to release these?


16 Apr 2009, 20:27
Bill Dudney (916 posts)

Hi Vip,

Sorry for being so late in responding.

You do need to release the objects you have connected in IB. The nib file loading machinery is smart in the way it assigns those values so it will put a retain in on your behalf if you don’t indicate that you will do it (via a property or with a set method).

If you use malloc you need to call free.

Hope this helps!


03 May 2009, 10:10
David Hart (2 posts)

I’d like to second the OP about his first questions concerning @NSString@. Here are a few example directly from the screencast code:

NSLog(@"the summary = %@", [movie summary]);

In this case I’m going to assume that strings created this way are indeed compile time constants. But in this example:

Movie *movie = [[Movie alloc] initWithTitle:[[NSString alloc] initWithString:@"iron man"]

Here we clearly have NSString alloc and init calls, which are passed a compile time constant string. But since the alloc and init messages are passed, I was under the assumption that there is supposed to be a matched release.

Second question regarding memory management of @NSString@. In the Movie class, the property for the @title@ variable is given an @assign@ attribute. Is that safe? What if I send @setTitle@ with an @NSString@ which I release right after the call? For example:

NSString* garbageString = [[NSString alloc] init];
[movie setTitle:garbageString];
[garbageString release];

Thanks in advance for any replies, David Hart.

04 May 2009, 12:58
Bill Dudney (916 posts)

Hi David,

Thanks for posting.

In episode 1 around 34:35 I used assign because that is what the code did when I wrote the accessors by hand. Around 38 I spent a bit of time talking about using copy instead of assign. However keep in mind that I was trying to discuss the properties and how the method invocation works (and its not a struct operator). The memory management stuff does not come into the discussion until the second episode.

In the second code block you are correct, you should be releasing the alloc’ed NSString. Or and even more commonly used idiom using an autoreleased string would be better with;

Movie *movie = [[Movie alloc] initWithTitle:[NSString stringWithString:@"iron man"]

Which would create an autoreleased string.

You are correct in the final code bit there that you’d get a freed object in there for the title. Around 22:20 in episode I discuss using retain as the memory attribute instead of assign.

Strings are commonly copied instead of retained so its common to have this kind of property decl for strings.

@property(copy) NSString *title;

Please let me know if any of this does not make sense.

19 Jun 2009, 07:11
Matthew Carriere (1 post)

Can you clarify why in the screencast you declared the title property with assign?

@property(assign) NSString *title;

I tried changing the grossSales property declared later in the chapter to assign and everything worked the same, it even used the same amount of memory after 20 secs, so my question is why the retain over assign?


29 Jun 2009, 11:37
Bill Dudney (916 posts)

Sorry to take so long to respond. This post must have escaped my RSS reader.

(assign) does a simple assignment (i.e. myTitle = newTitle). Which in some cases is what you want and makes things very simple. However what if that string gets released. If that happens them myTitle points to a freed object. Any message you send to it will probably cause a crash.

If you want/need an object to stick around you have to send it retain to register your need, then send it release when you are done.

Good luck!

You must be logged in to comment