Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials

Reply
 
LinkBack Thread Tools Display Modes
Old 08-30-2011, 05:29 PM   #76 (permalink)
Digitally FlyŽ Software
 
JoeApps's Avatar
 
Join Date: Aug 2011
Location: www.joeapps.com
Age: 24
Posts: 10
JoeApps is on a distinguished road
Default Sample & Analogy

Quote:
Originally Posted by longst View Post
a great tutorial for new comers, especial for someone like me coming from C# and Java.
I am still not sure I did quite understand with this part
"
The same would be true if a new value gets assigned to the property. Remember, the setter will release (-1) the old value, and the object we created is now the old value. So again we are left with a +1 net retain count, again we do not have any means to communicate with the object, and again it is a leak. And as a double-bonus, if the new object is another alloc/init creation, we're replacing a leaked object with another leaked object.
"

Could you please show me a sample code regarding this part Thank you
"For every "alloc or retain" you should have a "release" and vice-versa."

So just using ivars:
someView = [[UIView alloc] init]; // Alloc message sent.

Using accessors:
self.someView = [[UIView alloc] init]; // Alloc and retain message sent.

So if your project was setup up with:
.h
@property (nonatomic, retain) UIView *someView; //telling accessors to include a retain message using the retain attribute.
.m
@synthesize someView;


Step 1: allocates space for the object and initializes it, then "do stuff with object", then releases it.
someView = [[UIView alloc] init];
"Do stuff with object"
[someView release];

someView was released from the allocated space. But now the ivar points to an empty space that once contained your object because you did not nil out the ivar using someView = nil;


Step 2: accessor which will call a release on the old value in Step 1: and retain the new value we see here
self.someView = [[UIView alloc] init]; // Alloc & Retain *Needs 2 releases sometime in the future

but since the old value in step 1 has already used a release for its alloc, it has long already left the place someView is currently still pointing to.

It will crash because it tries to release something not there!

Remember: someView is different than self.someView

FUN ANALOGY

The cookie monsta tells you to stick your hand out and then put a cookie in it (alloc),
you take a bite(do stuff),
then the cookie monsta takes it away from you because he is the cookie monsta and he does what he wants,
"I DO WHAT I WANT!!!"(release sucka!)
OM NOM NOM!

You don't have a cookie anymore and feel sad and hungry....awwwww.
COOKIE MONSTA DON'T CARE!!!

Cookie monsta come back still looking for the cookie in your hand...Again...
(release sucka!)
...but you don't have anything to give the cookie monsta...
COOKIE MONSTA DON'T CARE!!!

He rears up, opens his mouth, and BITES YOUR ARM OFF! OM NOM NOM NOM!

THE END

- It's a harsh life. lol
JoeApps is offline   Reply With Quote
Old 08-30-2011, 05:36 PM   #77 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Don't crap on my thread, bro.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 08-30-2011, 05:49 PM   #78 (permalink)
Digitally FlyŽ Software
 
JoeApps's Avatar
 
Join Date: Aug 2011
Location: www.joeapps.com
Age: 24
Posts: 10
JoeApps is on a distinguished road
Default You got it!

Quote:
Originally Posted by BrianSlick View Post
Don't crap on my thread, bro.
Ha ha! I promise...I won't crap on your thread.

Anyways, you agree with what I have said? Everyone thinks about things a little differently and I was just giving my view on the subject.

Hope all is well,
JoeApps is offline   Reply With Quote
Old 08-30-2011, 09:30 PM   #79 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

What's to agree with? You gave a longer answer to a question that was answered a really long time ago (you seem to like digging through old threads - there are plenty of newer threads that need attention), didn't really answer the question that was asked, didn't really say anything that hasn't already been said in this thread, and in addition to being a lame ripoff of the honey badger, your analogy doesn't really clear things up.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 08-30-2011, 10:26 PM   #80 (permalink)
Digitally FlyŽ Software
 
JoeApps's Avatar
 
Join Date: Aug 2011
Location: www.joeapps.com
Age: 24
Posts: 10
JoeApps is on a distinguished road
Default

Ok. Thanks for the feedback. Didn't know about the honey badger...will have to check that out!

Last edited by JoeApps; 08-30-2011 at 10:42 PM.
JoeApps is offline   Reply With Quote
Old 03-18-2012, 06:05 PM   #81 (permalink)
Registered Member
 
Join Date: Mar 2012
Posts: 2
equiinferno is on a distinguished road
Question ARC? No leaks on instruments?

Hello everyone, especially Slick,
I've been a lurker for quite a while but I just registered to reply to this thread. It's the ultimate eye-opener. If what you say is true, I have to do some serious overhauling to my code-base. Before I do so, I have two questions:

1) Does all of this still apply with ARC enabled?
Specifically, this pattern:
Code:
Figure 13
- (void)viewDidLoad
{
   MyClass *newItem = [[MyClass alloc] init];    // Create it
   [self setMyProperty: newItem];                // Do something with it
   [newItem release], newItem = nil;             // Get rid of it, ASAP
}
?

Im guessing yes, hence my second question:
2) Why does the Leak-Instrument not show any leaks allthough I'm doing everything wrong?
As it seems, I've been wildly mis-using & confusing ivars/properties and doing faux pas like this again and again:
self.anIvarArray = [[NSMutableArray alloc] init];
My apps hardly ever crash on the simulator or while profiling via Instruments (Leaks). They run rather stable. However, I do experience lots of random crashes on devices.

Why don't any of these leaks show in the Leak-Instrument? Do these faux pas produce super stealthy-sneaky leaks that the Leak-Instrument doesn't show??

Thank you very much in advance for your help!!
equiinferno is offline   Reply With Quote
Old 03-18-2012, 06:16 PM   #82 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Quote:
Originally Posted by equiinferno View Post
1) Does all of this still apply with ARC enabled?
Specifically, this pattern:
Code:
Figure 13
- (void)viewDidLoad
{
   MyClass *newItem = [[MyClass alloc] init];    // Create it
   [self setMyProperty: newItem];                // Do something with it
   [newItem release], newItem = nil;             // Get rid of it, ASAP
}
?
If you try doing this code in ARC, you will get an error on the 3rd line because release is not allowed in ARC. So you can remove the 3rd line. Otherwise, it still basically applies.

Since you can't do autorelease in ARC either, then your code below is also acceptable.

Properties in general aren't quite as important under ARC, but IMO are still a good idea to use.

Quote:
Originally Posted by equiinferno View Post
Im guessing yes, hence my second question:
2) Why does the Leak-Instrument not show any leaks allthough I'm doing everything wrong?
As it seems, I've been wildly mis-using & confusing ivars/properties and doing faux pas like this again and again:
self.anIvarArray = [[NSMutableArray alloc] init];
My apps hardly ever crash on the simulator or while profiling via Instruments (Leaks). They run rather stable. However, I do experience lots of random crashes on devices.
ARC: No problem
Not-ARC: This is a leak.

Also, leaks don't cause crashes, or at least not quickly. It is over-releases that will crash.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 03-19-2012, 04:19 AM   #83 (permalink)
Registered Member
 
Join Date: Mar 2012
Posts: 2
equiinferno is on a distinguished road
Default

Slick, thank you for your quick reply!!
Huh, so that means (as I am using ARC throughout all projects) I'm actually not doing anything wrong (at least in that regard).

Quote:
Properties in general aren't quite as important under ARC, but IMO are still a good idea to use.
Could you please elaborate a bit on this? I'm wondering: Is it worth the effort to make sure that I'm using properties throughout all my projects (couple thousand lines of code)?
I'd be highly interested in a "Slick's Definitive Guide To Properties with ARC enabled" )
equiinferno is offline   Reply With Quote
Old 03-19-2012, 07:21 AM   #84 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Well, read the thread. One of the key points I make here is that properties help with memory management. That's what ARC does, too. So, the importance of that particular aspect of properties is reduced under ARC. But this is not the only thing that properties are good for, so you should continue to use them.

As to whether or not you should rewrite a bunch of code, I can't answer that for you.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 05-08-2012, 12:27 PM   #85 (permalink)
[[Brain alloc]init];
 
fhsjaagshs's Avatar
 
Join Date: Aug 2011
Location: New Jersey
Age: 15
Posts: 92
fhsjaagshs is on a distinguished road
Default

Very useful. Solved many of my problems.
fhsjaagshs is offline   Reply With Quote
Reply

Bookmarks

Tags
memory management, properties

Thread Tools
Display Modes

Posting 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 On
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 463
16 members and 447 guests
alexeir, David-T, Dj_kades, Elad, foslock, HemiMG, iAppDeveloper, jeroenkeij, LunarMoon, Mijator, Pauluz85, pipposanta, QuantumDoja, robsmy, sacha1996, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:21 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0