<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>cocos2d for iPhone &#187; Topic: MemeoryMgmt again</title>
		<link>http://www.cocos2d-iphone.org/forum/topic/3428</link>
		<description>A fast, easy to use, free, and community supported 2D game engine</description>
		<language>en-US</language>
		<pubDate>Fri, 10 Feb 2012 02:03:54 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.1</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://www.cocos2d-iphone.org/forum/search.php</link>
		</textInput>
		<atom:link href="http://www.cocos2d-iphone.org/forum/rss/topic/3428" rel="self" type="application/rss+xml" />

		<item>
			<title>MikeSz on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21073</link>
			<pubDate>Tue, 15 Dec 2009 22:17:24 +0000</pubDate>
			<dc:creator>MikeSz</dc:creator>
			<guid isPermaLink="false">21073@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>ok, I get it now hactar ;) thanks for explaining it all in detail ;)</p>
<p>still - I guess that this mostly depends on the size of the app</p>
<p>if the app is rather small and doesn't stress the memory limit - it can be better not to release scenes/layers and basically remove all the loading time when changing scenes (no need to recreate them anymore)</p>
<p>but in a larger app your approach is absolutely better ;)</p>
<p>thanks for the input, will be more than useful soon enough I guess ;)
</p></description>
		</item>
		<item>
			<title>hactar on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21072</link>
			<pubDate>Tue, 15 Dec 2009 22:00:21 +0000</pubDate>
			<dc:creator>hactar</dc:creator>
			<guid isPermaLink="false">21072@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/489'>MikeSz</a>: Why release layers/scenes? Memory limits... The iPhone is very tight on memory, having all 3 scenes and their layers loaded in parallel while only one scene is actually running is wasting lots of memory. In the case of my app, it actually causes the iPhone to kill my app due to it being out of memory. So in my app the main menu scene is opened first, the user chooses his game mode, the scene is replaced with the game scene (while the main menu scene is released and so freed) and this in turn reduces memory load. When the player opens the in game menu a layer is added, when the user clicks continue the layer is released. When the game completes the main menu is loaded and the game scene is released.</p>
<p>Even if your app does not get killed for memory over-usage, you may notice the iPhone killing other background tasks when the iPhone is in low (but not out of) memory situations (check the iPhone console for messages). The iPhone then stops checking for mails in the background or kills Safari's preloaded pages, and so makes the iPhone slower for the user when he quits your app as all caches are gone.</p>
<p>Being a Good Citizen means releasing things you don't need anymore and allocating objects lazily (See "Memory is not Unlimited: <a href="http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/MobileHIG/DevelopingSoftware/DevelopingSoftware.html)" rel="nofollow">http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/MobileHIG/DevelopingSoftware/DevelopingSoftware.html)</a>. Will your app work without being a good citizen? Probably. But if you ever reach the point where your app breaks the iPhones limits, guess what, you get to rewrite your code to release scenes and layers. Even if you never reach that limit, users will still be happier if mails continue to be checked for in the background, your code will be future proof (iPhone Nano with less memory?), and you'll have less troubles with jailbroken users who install backgrounding apps on their iPhone and then mail you because they are surprised that your app crashed when their background apps are swallowing a substantial amount of memory.</p>
<p>Also note that in low memory situations (applicationDidReceiveMemoryWarning:), your app should call [[TextureMgr sharedTextureMgr] removeUnusedTextures]; which removes unused textures from your memory. If however all your layers and scenes are still loaded then all textures are still in use, so no memory will be freed, so you run a further risk of your app being killed for memory over-usage.</p>
<p>Hope that helps :)</p>
<p>@<a href='http://www.cocos2d-iphone.org/forum/profile/3778'>goldplated</a>: No problem, glad its of use :)
</p></description>
		</item>
		<item>
			<title>goldplated on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21056</link>
			<pubDate>Tue, 15 Dec 2009 16:39:42 +0000</pubDate>
			<dc:creator>goldplated</dc:creator>
			<guid isPermaLink="false">21056@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/1750'>hactar</a> - Thanks for taking the time to explain this further...   very informative!
</p></description>
		</item>
		<item>
			<title>MikeSz on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21054</link>
			<pubDate>Tue, 15 Dec 2009 16:30:54 +0000</pubDate>
			<dc:creator>MikeSz</dc:creator>
			<guid isPermaLink="false">21054@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>thanks again hactar ;)</p>
<p>however - I'm not really sure why would you release layers during the game. I mean - you can just keep them, no ?</p>
<p>for example let's suppose you have 3 scenes:<br />
1) gamescene with gamelayer and guilayer (buttons etc)<br />
2) menuscene with menulayer<br />
3) optionsscene with optionslayer</p>
<p>you can just switch from one to another, why release anything ? </p>
<p>I understand that after you die (or run the game again) game objects need to be removed from the gamelayer, but the layer itself can stay like it was, no ?
</p></description>
		</item>
		<item>
			<title>hactar on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21032</link>
			<pubDate>Tue, 15 Dec 2009 14:32:47 +0000</pubDate>
			<dc:creator>hactar</dc:creator>
			<guid isPermaLink="false">21032@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Yes, i don't usually work with tags either.</p>
<p>Well, this is all coding philosophy, and its a thing of personal preference. As long as you are consistent in your working everything is fine.</p>
<p>I nearly always prefer retain over assign, but only because I understand cocoas memory management and it saves me lines of code. If one does not fully understand cocoas memory management then explicitly having to add retains and releases may help you learn  it quicker as you see exactly whats going on.</p>
<p>I personally try to write my code symmetrically to keep my code clean and make checking for memory leaks easy. So, in your case, if i had "another class" which does gD.gameLayer = [CCLayer node];  (which is converted to two lines, an assignment followed by a retain) on init, i would also have "another class" do gD.gameLayer = nil; (which is converted to two lines, a release and an assignment to nil) on its dealloc.</p>
<p>Now when checking my code for leaks I look at the init and see, ooh, "another class" has assigned something to a retain property during init, hence "another class" needs to do something on dealloc.</p>
<p>Another approach would be to use your gD as a dumpster, where you (using retain properties) store your access to different objects, and only it is responsible for releasing everything at the end (during its own dealloc).</p>
<p>As for "never releasing layers"... Well, don't know what your coding, but when switching scenes the layers on it are generally released. Also, lets say you have a game and the player has a "open menu" button. What I do here is drop down a layer with the menu while pausing the layers behind it, and if the player closes the menu the layer gets removed and released again... There are many situations where layers are released.
</p></description>
		</item>
		<item>
			<title>MikeSz on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21026</link>
			<pubDate>Tue, 15 Dec 2009 14:08:55 +0000</pubDate>
			<dc:creator>MikeSz</dc:creator>
			<guid isPermaLink="false">21026@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>thanks hactar, I never liked all those settings, so much easier and clearer in C++ ;)</p>
<p>makes it clearer now ;) not entirely clear, but clearer ;)</p>
<p>---</p>
<p>my real question was something more like:<br />
I have a storage class where I keep all the "public" data, also pointers to some of the more important layers (dont want to access variables using getByTag as I feel it must be rather slow). so in the header I have something like:<br />
CCLayer * gameLayer;<br />
@property (nonatomic, retain) CCLayer * gameLayer;<br />
and of course @synthesize in .mm</p>
<p>I initialize it in another class like this:<br />
gD.gameLayer = [CCLayer node];<br />
[gameScene addChild:gD.gameLayer]; // gD is my macro for gamedata class (The one with "public" data), gameScene is some CCScene</p>
<p>I never release those layers (why would I ;) ), they stay with me till the end. so while I guess there is not much difference between retain and assign (memory will be purged when the app is closed anyway), I just wonder what is the correct way of doing it
</p></description>
		</item>
		<item>
			<title>hactar on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21025</link>
			<pubDate>Tue, 15 Dec 2009 13:47:14 +0000</pubDate>
			<dc:creator>hactar</dc:creator>
			<guid isPermaLink="false">21025@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/7184'>Mike</a>: you can use both assign or retain, it all depends on how you like working.</p>
<p>A retain property will automagically always add a retain and release whenever you access the property (see my post on how cocoa converts that).</p>
<p>An assign property will NOT add any retain/release things, you are responsible for now adding those yourself.</p>
<p>There is no "letting cocos do it" option, cocos2d claims the object for itself and will only keep it for itself as long as it needs it. So cocos2d might release it, causing the retainCount to reach 0, while you still need access to the object. A typical example is this (retainCount is the retain count of aSprite):</p>
<p>(assuming 'Sprite *aSprite;' in the header file of a layer)<br />
<pre><code>- (id) init {
....
aSprite = [SpriteWithFile:@&#34;blabla.gif&#39;];// retainCount: 1
[self addChild: aSprite]; //retainCount: 2
....
}  // retainCount 1

//... time passes, the game is playing ...

// you want to temporarily remove the sprite from the screen, so you remove it from the layer.
[self removeChild: aSprite];  // retainCount: 0 OBJECT DEALLOCED!

//... time passes, the game is playing ...

// the sprite needs to reappear on the screen
[self addChild: aSprite];  // CRASH, aSprite has been dealloced and no longer exists.</code></pre>
<p>There are multiple solutions to this, one of the simplest is to make aSprite into a retain property, call self.aSprite = [SpriteWithFile:@"blabla.gif']; and call self.aSprite = nil; during the dealloc of the layer to also release the sprite.
</p></description>
		</item>
		<item>
			<title>hactar on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21023</link>
			<pubDate>Tue, 15 Dec 2009 13:31:32 +0000</pubDate>
			<dc:creator>hactar</dc:creator>
			<guid isPermaLink="false">21023@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>PS: also read this:</p>
<p><a href="http://www.cocos2d-iphone.org/forum/topic/3224" rel="nofollow">http://www.cocos2d-iphone.org/forum/topic/3224</a>
</p></description>
		</item>
		<item>
			<title>MikeSz on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21022</link>
			<pubDate>Tue, 15 Dec 2009 13:31:20 +0000</pubDate>
			<dc:creator>MikeSz</dc:creator>
			<guid isPermaLink="false">21022@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>ok... then I also have a question regarding it:</p>
<p>if I want to access some property (of Cocos kind of course, say it's a CCLayer or CCSprite) from outside a class, should I use:<br />
@property (nonatomic,retain) CCNode * someNode;<br />
@property (nonatomic,assign) CCNode * someNode;</p>
<p>or how ?</p>
<p>not releasing it myself (letting Cocos do it), non deallocing (also leaving it for Cocos), just want to be able to access it
</p></description>
		</item>
		<item>
			<title>hactar on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21020</link>
			<pubDate>Tue, 15 Dec 2009 13:24:12 +0000</pubDate>
			<dc:creator>hactar</dc:creator>
			<guid isPermaLink="false">21020@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Ok, I'll try to explain some of this (though you should read apples documentation on retain/release, its very good and very detailed.</p>
<p>1) Pablo is right, you should call [super dealloc] in the dealloc method.</p>
<p>2) You do not actually need your layer to be a property, your code will fully work without it. You only need it if you access the layer from outside of gameScene.</p>
<p>3) Assuming you keep your code as it is, here is what happens in your code:</p>
<pre><code>layer = [GameLayer node]; // the node function creates a GameLayer for you which will be autoreleased when the end of init is reached. So current retainCount of layer is 1.
[self addChild:layer]; // You add the layer to the cocos2d scene, cocos claims the layer and retains it, retain count is now 2.
} // the end of the init function is reached, layer is autoreleased. The retain count is now 1 again (because cocos is still retaining)</code></pre>
<p>You now no longer own layer! Only cocos owns it. If you call [gameScene removeObject:layer] somewhere the retain count will reach 0, and the layer will be dealloced. If you then call [layer release]; your retain count will reach -1 and your app will crash.</p>
<p>Heres whats wrong: if you want to own the layer using the property system, you need to not only declare layer as a property, but use it as a property. if you do self.layer = [GameLayer node]; (notice the self!) you use layer as a property and not as an objective C object. Now internally, because you said @property (nonatomic, [strong]retain[/strong]) GameLayer *layer;, cocoa will convert self.layer = [GameLayer node]; into </p>
<pre><code>layer = [GameLayer node];
[layer retain];</code></pre>
<p>As you now own the object correctly, you can dealloc it in your (void) dealloc. You could also do:</p>
<pre><code>self.layer = nil;</code></pre>
<p>because as you are using it as a property by calling it via self, it is actually converted to:</p>
<pre><code>[layer release];
layer = nil;</code></pre>
<p>I hope this helps, but again, i strongly recommend you read apples documentation on memory management and properties, else you'll have memory leaks all over the place.
</p></description>
		</item>
		<item>
			<title>pabloruiz55 on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21017</link>
			<pubDate>Tue, 15 Dec 2009 12:46:58 +0000</pubDate>
			<dc:creator>pabloruiz55</dc:creator>
			<guid isPermaLink="false">21017@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>also i am pretty sure your dealloc method should have</p>
<p>- (void) dealloc<br />
{<br />
   [super dealloc];<br />
}
</p></description>
		</item>
		<item>
			<title>Receptor on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21015</link>
			<pubDate>Tue, 15 Dec 2009 12:31:16 +0000</pubDate>
			<dc:creator>Receptor</dc:creator>
			<guid isPermaLink="false">21015@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I wouldn't make the property as "retain" in this case, since you add the layer immediately to the scene. Make the property as "assign" so you won't have to care about releasing (cocos2s will do it for you)
</p></description>
		</item>
		<item>
			<title>trump-card on "MemeoryMgmt again"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/3428#post-21012</link>
			<pubDate>Tue, 15 Dec 2009 10:05:11 +0000</pubDate>
			<dc:creator>trump-card</dc:creator>
			<guid isPermaLink="false">21012@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hello!</p>
<p>Very simple question. I'm confusing</p>
<p>Example:<br />
<pre><code>@class GameLayer;

@interface GameScene : Scene
{
   GameLayer *layer;
}

@property (nonatomic, retain) GameLayer *layer;

@end

@interface GameLayer : Layer
{

}

@end

@implementation GameScene

@synthesize layer;

- (id) init
{
   layer = [GameLayer node];
   [self addChild:layer];
}

- (void) dealloc
{
   [layer release];
}

@end</code></pre>
<p>Question: Should I do [layer release] in this example?
</p></description>
		</item>

	</channel>
</rss>

