0

I have a class where I have:

static UiImage *image;

In the same class I have a method setImage (UiImage*) imag

{self.image= [[Uiimage alloc]init]; //*
self.image=imag;}

In another class I have

[myFirstClass setImage: (uiimage)]

This uiimage exists .

The app frezees and it stops at (*). Houndreds of proces start at this line. I also see EXC_BAD_ACCESS

Thanks

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Raluca Lucaci
  • 2,058
  • 3
  • 20
  • 37

3 Answers3

3

If you have a property image then you're getting into infinite loop by calling your setter from setter.

ksh
  • 1,894
  • 19
  • 20
  • i don t have property in h file i just declared it static in m file . I am moving now from java to objective c...and principles are different and i do a lot of mistakes – Raluca Lucaci Jan 31 '12 at 13:37
1

Use the following instead...

- (void)setImage (UiImage*)anImage {
  [image release];
  image = [anImage retain];
}
Simon Lee
  • 22,304
  • 4
  • 41
  • 45
1

You have declared image

static UiImage *image;

Is that because you wish to initialise it once and thereafter refer to it - as a constant? If so, a good way to do this is to override the getter accessor method for image.

// foo.h
class foo {
    UIImage* image_;
}

@property (nonatomic, retain) UIImage* image;

// foo.m
@synthesize image = image_;

-(UIImage*)image {
    if (image_ == nil) {
        //set the image here
        image_ = [[UIImage alloc] init];    
    }
    return image_
}

Then in client code, the first time you refer to foo.image it will be instantiated. The second and every other time you refer to it, it will already have a value.

// elsewhere in foo.m

UIImageView* fooImageView = [[UIImageView alloc] initWithImage:self.image];

// bar.m

UIImageView* barImageView = [[UIImageView alloc] initWithImage:foo.image];

See this SO answer also for reference.

Community
  • 1
  • 1
Damo
  • 12,840
  • 3
  • 51
  • 62