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 > class allocation crash

class allocation crash
Thread Tools
Fresh-Faced Recruit
Join Date: Jan 2009
Status: Offline
Reply With Quote
Jan 10, 2009, 03:29 PM
 
Hey guys, this is a real noobie question so please bare with me. I'm 100% new to Mac, only had this macbook for a week. I want to get into IPhone dev and here I am. I've been writing code for c/c++ windows applications for some time now so i'm not a coding noob, just obj-c

Anyway, here is my question.
I'm trying to allocate memory for a near empty class. But its crashing on the alloc function... here we go..

Sprite.h file
Code:
@interface Sprite { @private bool m_init; } - (void)draw; @end

Sprite.m
Code:
#import "Sprite.h" @implementation Sprite - (void)init { m_init = TRUE; } - (void)draw { } @end
ELGLView.m offending code
Code:
if( !m_init ) { m_sprite = [[Sprite alloc] init]; m_init = TRUE; }
I have m_sprite decleared in the ELGView.h file. When I compile i get a waring saying:

Warning: 'Sprite' may not respond to '+alloc'

I'm not sure what that warning means either, but i'm sure it has something to do with my crash. When i rush i through the Iphone simulator it crashes and asks if i want to reload.

When i step through the code it comes up to the
Code:
m_sprite = [[Sprite alloc] init];
line and the callstack says its finished in __forwarding__
Looking at tutorials it appears to be correct, but there might be something very simple that i've forgotten somewhere. Any help would be greatly appreciated.
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jan 10, 2009, 04:04 PM
 
1. Sprite should probably be a subclass of NSObject, not a root class itself.

2. The init method is supposed to return the initialized object — if your init method returns void, what do you expect to be returned when you do somevar = [[Sprite alloc] init]? That's the same thing assigning the return value of a void function. Additionally, init methods should call the superclass's designated initializer.

Here's Apple's primer on how this stuff is done in Objective-C: http://developer.apple.com/documenta...001163-CH1-SW2
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
Jan 12, 2009, 11:50 AM
 
Replace

Code:
@interface Sprite
with

Code:
@interface Sprite : NSObject
and also replace

Code:
- (void)init { m_init = TRUE; }
with:

Code:
- (id)init { if( self = [super init] ) { m_init = YES; } return self; }
The problems are:

1. All your objects need to be derived from one of Apple's base classes (i.e. NSObject or NSProxy) in order to inherit the +alloc method.

2. The init method needs to return an id, and needs to call super's implementation. The reason you want to assign the result of [super init] to the 'self' variable is because it's perfectly legal for an init method to pull a switcheroo and return a different object than the one you called -init on (for example, this is how class clusters such as NSString and NSArray work - initialize an instance of one of those two classes and you actually get back a new object belong to some private subclass of NSString or NSArray). For this very reason, you need to return self at the end of the method, so the caller of -[Sprite init] will know what object it's getting back from the method (which, from its POV, may or may not be the same object it created with +[Sprite alloc]).

Also, in Objective-C, the boolean type is actually spelled with upper-case letters - i.e. 'BOOL' rather than 'bool', and the defines for truth and falsity are YES and NO rather than TRUE and FALSE. The way you're doing it should work, but is not as idiomatic. With that said, I'm not sure why you're using a variable to keep track of the class having been initialized - the class's -init method should be called immediately after creation, so in practical usage that variable should always be true, making it look a bit unnecessary to me.
(Last edited by CharlesS; Jan 12, 2009 at 12:08 PM. )

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
   
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:52 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2