Objective-C Class allocation crash

MrMegabytes
Unregistered
 
Post: #1
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 Grin

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.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
+alloc is an NSObject method. You'll want to change your Sprite interface to subclass NSObject, like so:

Code:
#import <Cocoa/Cocoa.h>

@interface Sprite : NSObject

In most cases, you'll want all of your objects to subclass NSObject (or a subclass thereof).
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #3
And -init must return self. Please read the docs, again if you have done so already.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  c pointers and memory allocation NelsonMandella 15 7,176 Feb 27, 2010 12:59 AM
Last Post: Kerome