Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > will this cause a memory leak?

will this cause a memory leak?
Thread Tools
poulh
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
May 2, 2005, 01:40 AM
 
Here is what I am trying to do...

I have a class Account. And the name is returned with accountName

I have a searchfield and as i type i filter my table of accounts on the fly... i want it to be case insensitive search.
Everything works great.

I'm wondering though if by creating the lowercase strings for this comparison I am causing a memory leak. I never release the pointer that is returned to me for the lowercase string. Is this taken care of? If so how (autoRelease maybe?)?

NSRange subStringRange = [[[account accountName] lowercaseString] rangeOfString:[[searchField stringValue] lowercaseString]];

if(subStringRange.length)
{
// if you are here, the search field contains is a sub string of the account name.
}
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
     
Wevah
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
May 2, 2005, 02:22 AM
 
Stuff that isn't alloced or copied is returned autoreleased, so no, you won't be leaking with that code.

(You can check by releasing them, and seeing if you crash ).

Also, you could do:

NSRange subStringRange = [[account accountName] rangeOfString:[searchField stringValue] options:NSCaseInsensitiveSearch];
[Wevah setPostCount:[Wevah postCount] + 1];
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
May 2, 2005, 04:41 PM
 
what if i were to save the returned string pointer and use the lowercased-string in a few places... am i still ok?

i'll try the options param as well... didn't see that when poking thru the headers
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 2, 2005, 04:43 PM
 
Then you should retain it, because you'll become an owner of the string.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
May 2, 2005, 04:46 PM
 
so NSString *myLowercaseString = [aString lowercase]; retains?

but this is ok

[aString lowercase];
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 2, 2005, 05:07 PM
 
No, [[aString lowercaseString] retain] retains. I suggest you read up on Cocoa memory management. (Not trying to be rude; you just seem a little fuzzy on the topic, which won't do you any good.)
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
May 3, 2005, 01:36 PM
 
ok... reread my little beige book and made a test app.. i got it. Thanks!
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
     
dstaudigel
Fresh-Faced Recruit
Join Date: Jan 2003
Status: Offline
Reply With Quote
May 8, 2005, 05:36 PM
 
Autoreleasing confused me for a while, but it's actually pretty simple. Most functions in Cocoa return objects that are put into the autorelease pool (the only exception i know of is the [[class alloc] init] call). This means that they will be destroyed when control of the application returns to Cocoa, i.e., once your function returns. So, if you want a class for longer than that you have to take control of it by [class retain] ing it. Once you retain it, you also have to release it. But in general, if you don't ever retain or release a class it stays around until you return your function (or if you are doing autoreleasepools).

Caio

Daniel
     
Wevah
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
May 8, 2005, 09:47 PM
 
Technically, they aren't released when your function returns; they're released at the end of the current run loop iteration. But your advice still holds true.
[Wevah setPostCount:[Wevah postCount] + 1];
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 8, 2005, 10:43 PM
 
Think of it as ownership. If you explicitly create an object (e.g. with alloc, new, copy or mutableCopy), you own that object � you control when it goes away. But if you ask for an object from another object, you need to retain that object in order to keep it. The only thing that's at all vague about the system is that with factory methods like [NSArray array], it's NSArray that's doing creating the object, so you have to retain it if you want to own it.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
May 9, 2005, 12:34 PM
 
thanks everyone.. i've been NSLogging the retain count and I think I've got it figured out!
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 02:43 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,