Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Objective C Memory Management
Index -> Programming, C -> C Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DtY




PostPosted: Sun Aug 09, 2009 6:47 pm   Post subject: Objective C Memory Management

I've been learning objective C for iphone development, but the book I'm reading assumes you already know objective C. I already know C, so I read a bit about obj. C, and I think I'm comfortable with the message system (or whatever they call it).

Now, something that I'm having trouble with is the memory management, as I understand it, all objects use reference counting, the reference count is increased if you send it the retain message, and decreased if you call the release method (and free()ed if the reference count drops to zero).
Is that all right?

Also, what does NS mean, like NSString and NSArray? Is that part of Objective C, or is that an Apple addon?

[edit] And all the datatypes from C (int, char, struct, etc.) are not objects, right?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun Aug 09, 2009 7:01 pm   Post subject: RE:Objective C Memory Management

NS is shorthand for NeXTSTEP. Apple bought NeXT and OpenStep (which evolved from NeXTSTEP) became the foundations for Mac OS X.

You're quite right on how retain and release work. Also see retainCount, and "free" actually frees the memory. You should also look into autorelease pools.

You can still use int and such, but it's preferable to use the typedef'd Obj-C equivalents, like NSInteger.
DtY




PostPosted: Sun Aug 09, 2009 7:52 pm   Post subject: RE:Objective C Memory Management

The book I'm reading briefly mentioned auto release pools, but suggested you avoid them on the iPhone because of severely limited memory. I get the impression that once in a while the garbage collector is started and checks if an object that should be auto released still has references to it, and frees the memory if it doesn't? Like Java works (I think).

So the NS___ types are specific to Apple's compiler, or would they work if compiled with GCC instead?

Thanks Smile
andrew.




PostPosted: Sun Aug 09, 2009 8:12 pm   Post subject: RE:Objective C Memory Management

I think the NS types are specific to the Apple compiler. I may be incorrect though.
wtd




PostPosted: Sun Aug 09, 2009 8:44 pm   Post subject: RE:Objective C Memory Management

There is nothing about them that ties them to Apple's compiler. However, they are part of Apple's libraries.
rdrake




PostPosted: Sun Aug 09, 2009 9:31 pm   Post subject: Re: RE:Objective C Memory Management

As wtd mentioned, reference counting works that way. As I recall the iPhone does not have a garbage collector, you must deallocate your own objects.

andrew. @ Sun Aug 09, 2009 8:12 pm wrote:
I think the NS types are specific to the Apple compiler. I may be incorrect though.
There are other implementations (GNUstep) out there that provide somewhat equivalent implementations. Also, Apple's compiler is GCC right now. I think they're going for Clang + LLVM later, but anyway... for the curious, Clang + LLVM will compile Objective-C code just fine, you just need the correct libraries installed. On Linux this isn't possible unfortunately due to incomplete implementations.

Well, the only problem I've really had with Clang + LLVM was with Objective-C 2.0-style properties. GNUstep lacks the correct code in order to pass the messages it generates.

Apple's site includes a rather large Objective-C programming guide, as well as guides on iPhone memory management. Also check out the Stanford CS139p lectures, available on iTunes, which include a lecture on memory management for the iPhone as well among other topics.

Oh, and to add onto what wtd said about the C primitive types... you can use them as he said, there also exists typedefs to each of them (NSFloat, NSInteger, etc.). They are certainly not objects and you will do nasty things if you treat them as such.

For example:
C:
NSInteger i = 42;
NSLog(@"%@", i);


You will get a random value as %@ is for an object which i is not.

The framework usually has an equivalent to the primitive types anyway. Check out classes such as NSNumber.
DtY




PostPosted: Mon Aug 10, 2009 11:16 am   Post subject: RE:Objective C Memory Management

Okay, thanks. That %@ makes more sense now, I was wondering why it didn't use printf style formatting.
andrew.




PostPosted: Mon Aug 10, 2009 12:53 pm   Post subject: RE:Objective C Memory Management

Guess I was wrong.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Mon Aug 10, 2009 5:23 pm   Post subject: RE:Objective C Memory Management

Happens to the best of us from time to time.
Joel92




PostPosted: Mon Aug 10, 2009 8:08 pm   Post subject: Re: Objective C Memory Management

Yeah, sadly the iPhone OS doesn't support garbage collection (N) but I guess it's good practice doing manual memory management.
DtY




PostPosted: Tue Aug 11, 2009 6:34 pm   Post subject: RE:Objective C Memory Management

I'm having a problem with the interface builder, I'm following along with the book, and so far the project hasn't changed the code yet, only used IB to put two text boxes in, and change a couple of the settings, and it says to press build and run to test it out.
It starts up okay, but when I click on the text box, which the text says should just open up a keyboard, nothing happens.

Anyone know what the problem might be?
rdrake




PostPosted: Tue Aug 11, 2009 10:39 pm   Post subject: RE:Objective C Memory Management

No. Check the properties of the text box and make sure you selected the type of keyboard to pop up.

Oh wait, did you wire up the text box to anything in the code? Otherwise it might just sit there and respond to nothing.
DtY




PostPosted: Wed Aug 12, 2009 9:23 am   Post subject: RE:Objective C Memory Management

There are two text boxes, one I didn't change the keyboard type, and one that's set to numpad. Do I have to make it respond to some code? The book says it should pop up as it is.
I was thinking that might be the problem, since you have to write code to release the keyboard, it doesn't really made sense to be able to get the keyboard up without code.

Do you know what I need to do to make it open?

[edit] I restarted from the beginning of the chapter, and the text boxes work now. I guess I did something wrong, though I'm not sure what, I went through the inspector thoroughly at least twice.
Joel92




PostPosted: Sat Aug 15, 2009 2:03 pm   Post subject: Re: Objective C Memory Management

@OP what book are you using?
DtY




PostPosted: Sat Aug 15, 2009 5:50 pm   Post subject: RE:Objective C Memory Management

"Beginning iPhone Development: Exploring the iPhone SDK" by Dave Mark & Jeff LaMarche, published by Apress.

[edit] Since I have this thread open already, can I have some input on this layout?
Posted Image, might have been reduced in size. Click Image to view fullscreen.
It's an RPN calculator, and all the buttons are working (except the trig ones floating out to the right), I was thinking of putting a pi button under the +/-, and log somewhere. There's a lot of extra screen space though, what should I do with it? Expand the buttons maybe?
Also, how do you do log in RPN, do you push the base, then the number, or what?
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 22 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: