Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 19, 2011, 02:54 AM   #1
alter
macrumors newbie
 
Join Date: Jul 2011
Stand alone/direct initialization (without object instance)

Hello everyone,

New here. I have a question related to initialization and memory.
I have a class (I am using cocos2D but is related to general Objective-C i guess) with initialization as follows:
Code:
@implementation MyClass
+(id) initializeMyClass
{
    return [[[self alloc] initMyClass] autorelease];
}

-(id) initMyClass
{
    if (([self = [super init]))
    {
    }
    return self;
}

-(void) dealloc
{
    NSLog(@"Deallocating");//I also used CCLOG instead.
    [super dealloc];
}
@end
I initialize this class in another class without an object:
Code:
[MyClass initializeMyClass];
This works fine but the MyClass' dealloc method is not called and crashes since some resources are not freed.

This is totally puzzling and can't find anything online. If anyone would suggest a solution or an alternative method, i'd really appreciate it.

Thank you.
alter is offline   0 Reply With Quote
Old Jul 19, 2011, 03:17 AM   #2
admanimal
macrumors 68040
 
Join Date: Apr 2005
We need to see all relevant parts of the actual code that is crashing in order to figure out what is wrong.
admanimal is offline   0 Reply With Quote
Old Jul 19, 2011, 03:24 AM   #3
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Never mind. I though you were trying to do the singleton pattern. But if you need dealloc, then maybe you're not. It seems you just want initializeMyClass to be a poorly named convenience constructor.
jiminaus is offline   0 Reply With Quote
Old Jul 19, 2011, 04:13 AM   #4
alter
Thread Starter
macrumors newbie
 
Join Date: Jul 2011
Thanks for the replies.

Here's the actual class:
Code:
@implementation Test
+(id) initializeFade:(CCNode *)sprite
{	
	return [[self alloc] initWithSprite:sprite];
}

-(id) initWithSprite:(CCNode *)sprite
{
	if ((self = [super init]))
	{
		spritePointer = (CCSprite *)sprite;
		[[CCScheduler sharedScheduler] scheduleUpdateForTarget:self priority:0 paused:NO];//Removing this code solves the crash but this code is indispensable in this case
		CCLOG(@"test initialized");
	}
	return self;
}

-(void) update:(ccTime)delta
{
	if (spritePointer.opacity < 255)
		spritePointer.opacity += 1;

}

-(void) dealloc
{
	CCLOG(@"test deallocated");
	[[CCScheduler sharedScheduler] unscheduleUpdateForTarget:self];//The crash seems to occur because the scheduler is not unscheduled here
	[super dealloc];
}
This is how i invoke it in another class:
Code:
[Test initializeFade:slide1];
As i said this is implementing cocos2D and am actually trying to emulate the cocos2D style actions. Example:
Code:
[CCFadeIn actionWithDuration:2.0f];
The crash seems to occur due to the scheduler not being unscheduled in the dealloc method.
alter is offline   0 Reply With Quote
Old Jul 19, 2011, 04:35 AM   #5
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
When is the object alloc'ed and init'ed in initializeFade: ever released? When do you want it to be released?
jiminaus is offline   0 Reply With Quote
Old Jul 19, 2011, 04:51 AM   #6
alter
Thread Starter
macrumors newbie
 
Join Date: Jul 2011
Oops sorry. I forgot to type in autorelease.
This is the actual:
Code:
+(id) initializeFade:(CCNode *)sprite
{	
	return [[[self alloc] initWithSprite:sprite] autorelease];
}
I want the it it to be released when the class calling
Code:
[Test initializeFade:slide1];
is deallocated.

Please ask any more details if i'm not clear.
alter is offline   0 Reply With Quote
Old Jul 19, 2011, 08:36 AM   #7
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
I'm not sure you understand how autorelease works. Autorelease arranges for release to be sent "sometime later". In practice, that's at the end of the current event.

Is this literally how you using initializeFade:? You're not using or assigning the returned pointer?
jiminaus is offline   0 Reply With Quote
Old Jul 19, 2011, 09:57 AM   #8
alter
Thread Starter
macrumors newbie
 
Join Date: Jul 2011
Yes, this is how i'm trying to initialize and not assigning the returned pointer.

If you're familiar with the Cocos2D framework, you might have come across its actions.

Like suppose you want an image/sprite to fade away, you use the Cocos2D's actions like:
Code:
[image1 runAction:[CCFadeOut actionWithDuration:1.0f]];
I thought my method would have been simpler than this method. The problem seems to be that my dealloc method doesn't seem to be called.

Any suggestions on how to emulate this type of action?
alter is offline   0 Reply With Quote
Old Jul 19, 2011, 04:35 PM   #9
admanimal
macrumors 68040
 
Join Date: Apr 2005
Quote:
Originally Posted by alter View Post
The problem seems to be that my dealloc method doesn't seem to be called.
The shared CCScheduler instance that you are using in the init method must be retaining self, preventing it from being dealloc'ed even though the Test class itself releases it. Presumably that unschedule method in dealloc would release the object if it was ever called, but you're obviously going to have to do that prior to dealloc.

Last edited by admanimal; Jul 19, 2011 at 05:15 PM.
admanimal is offline   0 Reply With Quote
Old Jul 19, 2011, 10:59 PM   #10
alter
Thread Starter
macrumors newbie
 
Join Date: Jul 2011
Quote:
Originally Posted by admanimal View Post
The shared CCScheduler instance that you are using in the init method must be retaining self, preventing it from being dealloc'ed even though the Test class itself releases it. Presumably that unschedule method in dealloc would release the object if it was ever called, but you're obviously going to have to do that prior to dealloc.
You're absolutely right! The CCScheduler IS retaining self. I just used NSTimer instead and invalidated it appropriately and it works! You can also use the CCScheduler's unscheduleAllSelectors method.

Thank you and also to jiminaus for your time and help!
alter is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: How do I convert an NSData object to an NSDictionary object, and vice versa? moonman239 iPhone/iPad Programming 6 May 2, 2014 02:17 PM
Assigning new instance of a class to an object that had an instance of the class moonman239 iPhone/iPad Programming 3 Oct 11, 2013 01:58 PM
Can someone help with an Initialization Error? Johnsa69 Mac Applications and Mac App Store 6 Sep 6, 2013 09:57 AM
Multi-monitor initialization sequence in ML cjd Mac Pro 2 Jun 1, 2013 08:47 PM
External Hard Drive Initialization bbhuang OS X 1 Dec 26, 2012 11:05 AM

Forum Jump

All times are GMT -5. The time now is 05:46 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC