Lefora Free Forum
login join
Loading
595 views

Memory Manegement

Page 1
1–7
regular - member
54 posts

In getting more familiar with cocoa I have seen two(at least) ways to create a variable:

NSString *example=@"This is just an example";

AND

NSString *example = [NSString alloc] init];
example=@"This is just an example";

Both seem to do the same thing, create a variable names example and put "This is just an example" into it. But what is the difference between the two? As I understand it, without Garbage Collection, I need to manually release the second example when done with it. But why, since the first example does the same basic thing, do I not need to release the first version? And lastly if they are the "same", why would I choose to use the second version, when the first releases itself?

Thank you,
                   Todd

?
288 posts

Todd

I think it goes like this:  If you alloc, you must release.  This could be the cocoa country'n'western song - "please release me if you m'alloced, and don't retain me any more".

There's a possibility of a major difference between those two things (when seen from the compiler's point of view).  When you say NSString* example = @"abc", the string "abc" is in the text area of the executable (and never gets changed or goes away - you can see it with the strings utility). But when you say NSString* example = [[NSString alloc]init] ; example = @"abc", the string "abc" is going to be copied into example (I think.  Are you sure maybe that shouldn't be a NSMutableString).

Well - something like that.  However, I'll be totally honest.  I'm also rather puzzled by Cocoa's memory management.  Sometimes you have to release, and sometimes you don't.  And sometimes you have to retain, and sometimes you don't.  However I think it boils down to the rule - If you alloc (or retain), you must release.
 
Does that sound right - or have I added to the confusion?

__________________
?
288 posts

Todd

I thought about this on the way to the movies and realized "it's obvious".  If you do:


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

e = @"I am a dead parrot" ; // leak!
[e retain] ; // explode

This is like doing in vanilla "C"

const wchar_t* e = (const char*) malloc(sizeof(NSString)) ;
e = L"it's an ex-parrot"; // leak
free((void*) e) ; // explode

Let me guess.  You (like me) are a veteran of C++ and STL and all that stuff in which = is an overloaded operator, right?  Obj/C is "C".  It's easy to leak and = is simply assignment (no more, no less).

__________________
regular - member
54 posts

I guess I will have to do some research on this, I can see it could be an art into itself. I do appreciate the = operator being broken into two pieces. Makes them more distinct. Although a little confusing at first. I understand the if you alloc'd you must release bit. There must be a scope issue I am not seeing. Neither defines a global variable does it, available outside the current method? Is it as simple as you can just release my second example when you wish, allowing you to maximize memory usage, to fine tune if you will?

On a similar note, in another post you had said "...ignore the code the garbage collector inserted...". What does GC code look like, as to recognize it when I see it and be able to (over)compensate for it.


Thank you again,
                             Todd

?
288 posts

Oh, I think I'd written a command-line program in order to discuss something with you and the wizard inserts that NSAutoRelease code (or something like that).  I simply wanted to avoid discussing NSAutoRelease at that time.  Nothing deep or meaningful.

Like you, I'm also rather puzzle by this part of Cocoa.  So let's agree to share any new knowledge and  insight that we acquire about this.

__________________
regular - member
54 posts

Sounds good. I am making great strides to my app, I think. learning alot on the way. As soon as I get to a stopping point I will try to do some research into memory manegement and see what turns up.

BTW, I am new to c/obj c/c++, my background is mostly with html, php and applescipt. A little javascript thrown in. I always wanted to learn to program my mac but back in the days of OS 7-9 it was not real friendly looking and the tools were expensive and scattered about. Two or three years ago I finally let go of the old os for os x and wished I would have done it as soon as it was available.

Cocoa is a bit weird to learn but when the code is correct it looks very elegant to me, as far as programming languages go. Alot of code can be packed into just a couple of lines of code. It seems very mac to me, it just works on a basic level but PLENTY of room to dial it in at the advanced level. I have a feeling that is what the memory thing is about, dialing it in just so.

Todd

P.S. My initial project is a downloader. I find tons of links and do not download hem as fast as I can find them. Lots of docs, source code and stuff. If I downloaded it as fast as I found it I would never be able to study each item as I would like, and my harddrive would explode it was so full. This goes from file hosts like rapidshare and megaupload to youtube vids, webpage templates, web pages, and the list goes on. My project is to store the links and they can be downloaded as needed/wanted. I thought this was a good project as it is pretty much just text manipulation/parsing/searching. Turns out to be true.

Whew, long P.S., anyhow when I get it basically functioning I was hoping you could give the code a quick browse and point out any obvious errors?

?
288 posts

It would be an honor to look at this for you - thank you for asking.  You can email it to me directly - or put it somewhere and I'll download and build it.  As always, I'll be happy to talk on Skype (with/without VNC to my machine) if you wish to talk about this.  I have some thoughts about learning cocoa - however I'd prefer to share them with your privately and seek your opinion off-line.

__________________
Page 1
1–7

Locked Topic


You must be a member to post in this forum

Join Now!