• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: on NSString usage
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: on NSString usage


  • Subject: Re: on NSString usage
  • From: "Chong Hiu Pun" <email@hidden>
  • Date: Thu, 1 Aug 2002 16:46:28 +0800

1.)But strResult pointed to 3 different objects:

strResult ----> aaa, and then
strResult ----> bbb

will the system release the memory space of "aaa"? Otherwise memory leak may
occur!

2.) So a global NSString will just release after the application quit?

Thanks!
----- Original Message -----
From: Sylvain Leroux
To: Chong Hiu Pun
Cc: email@hidden
Sent: Thursday, August 01, 2002 4:27 PM
Subject: Re: on NSString usage


In objective-C class variables are in fact global variables. So, they don't
benefit of some special treatment from the compiler.

> 1.) In this case, how the memory managed. Do I need to call [alloc] and
[init]
> on strResult before enter into functionA
> 2.) After running out of the scope of functionC, will strResult became
> autorelease, and I lost the content of strResult inside funtion GetIt?

Well in fact you should initialize 'strResult' like any other global at some
point. As far as I remember, globals are set to 0 by the compiler. So, if you
do nothing special, 'strResult' will be set to nil when you enter for the
first time in functionA.

At the end of function 'functionA', 'strResult' now points to an 'anonymous
and constant' NSString object containing the value @"aaa". This object have
been created by the compiler, and it's not your job to release it.

'functionB' and 'functionC' does exactly the same things. So, at the end,
'getResult' will returns (a pointer to ) an anonymous constant NSString. The
life-time of this object extends throught you app life-time: it's not released
or autoreleased by the time you 'GetIt' and you don't have to (and you should
not) release it in anyway.


Le jeudi 1 ao{t 2002, ` 09:41 , Chong Hiu Pun a icrit :


I have a class variable:
NSString *strResult;

which will be modified inside a few different function.

-(void) functionA
{
....
strResult=@"aaa";
}

-(void) functionB
{
....
strResult=@"bbb";
}

-(void) functionC
{
....
strResult=@"ccc";
}

-(NSString*) GetIt
{
....
return strResult;
}

I will execute functionA , functionB and then functionC and get the
strResult.
My question is:

1.) In this case, how the memory managed. Do I need to call [alloc] and
[init]
on strResult before enter into functionA
2.) After running out of the scope of functionC, will strResult became
autorelease, and I lost the content of strResult inside funtion GetIt?

Thank you very much!
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: on NSString usage
  • Next by Date: Creating .dmg / .img archives?
  • Previous by thread: Re: on NSString usage
  • Next by thread: Creating .dmg / .img archives?
  • Index(es):
    • Date
    • Thread