Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tomgoodenough

macrumors newbie
Original poster
Sep 10, 2010
13
0
I have a real problem here, i really cant figure it out.

I'm new to xcode so please explain stuff simply. ok, i have an ibaction that says this:


Code:
-(IBAction)ChangeImage1 {
	
	imageView.image = [[UIImage alloc] initWithContentsOfFile: @"Image1.jpg"];
	
}

This changes the image fine and i have 6 images, but the memory leaks REALLY bad and my app just crashes after showing 1 image. How do i do this without massive leaks? any ideas? please explain simply!!!
 
You are using alloc/init to create the image. This means that you are the owner of that and have to ensure it is released. This is covered in the Memory Management Rules.

The simplest answer is to autorelease the object:

Code:
-(IBAction)ChangeImage1
{
	imageView.image = [[[UIImage alloc] initWithContentsOfFile: @"Image1.jpg"] autorelease];
}

Or use a method that will return an autorelease UIImage.

Either way you need to read the memory management document I linked to as this is basic stuff you will have to understand.
 
You are using alloc/init to create the image. This means that you are the owner of that and have to ensure it is released. This is covered in the Memory Management Rules.

The simplest answer is to autorelease the object:

Code:
-(IBAction)ChangeImage1
{
	imageView.image = [[[UIImage alloc] initWithContentsOfFile: @"Image1.jpg"] autorelease];
}

Or use a method that will return an autorelease UIImage.

Either way you need to read the memory management document I linked to as this is basic stuff you will have to understand.

My app still isn't working, with that code the pictures dont even show up. I tried this:

Code:
imageView.image = [[UIImage imageNamed:@"Image1.jpg"] autorelease];

but it still didnt work and just crashed like before. ????????????????

Maybe should i get them from online instead of from the resources? but then ipod touch users wont see them unless on wi fi.....
 
Code:
imageView.image = [[UIImage imageNamed:@"Image1.jpg"] autorelease];

That code is wrong. It autoreleases an image that you don't own.

If that doesn't make sense, then you need to re-read the Memory Management Guide that was already pointed out to you. If it still doesn't make sense, you need to go back to some other fundamentals.
 
Stop. Step back. Take a breath. Chill. Resist the temptation to throw stuff against the wall to see what sticks. Spend time to understand the real problem and then spend time to understand whatever solutions you attempt.

My app still isn't working, with that code the pictures dont even show up.
And you were certain they were showing up before? Perhaps the images you think you're referencing aren't available. I would suggest some basic debugging of breaking down the issue into smaller pieces you can test against and verify conclusions.

I tried this:

Code:
imageView.image = [[UIImage imageNamed:@"Image1.jpg"] autorelease];

but it still didnt work and just crashed like before. ????????????????
Memory leaks usually don't result in crashing that quickly. So, there is probably some other cause to the crash. That is what you should investigate. Set up breakpoints and/or look at console messages and/or look at crash logs. Make sure you understand why it is crashing before attempting to fix it.

Maybe should i get them from online instead of from the resources? but then ipod touch users wont see them unless on wi fi.....
No, if they are static images, getting them from resources will be much more efficient and speedy then getting them online.
 
Stop. Step back. Take a breath. Chill. Resist the temptation to throw stuff against the wall to see what sticks. Spend time to understand the real problem and then spend time to understand whatever solutions you attempt.
sorry yeah i am getting annoyed, its just im only 12 and i dont know xcode that well. Maybe it would just be easier to make actions for each image, and make them setHidden:NO when the button is tapped, would that be easier than messing around with imageviews?
 
Robbie originally advised you to use an autorelease with your alloc/init pair OR use a factory method that returns an autoreleased object (any class method that returns an object and doesn't begin with the word 'new'). Not both, which is why you are having a problem.

You can do this:

Code:
imageView.image = [[[UIImage alloc] initWithContentsOfFile:@"file.jpg"] autorelease];

OR

Code:
imageView.image = [UIImage imageNamed:@"file.jpg"];

I would probably use the latter as a matter of style.

The imageNamed method returns an autoreleased object; calling autorelease on it again will result in over-releasing and a crash.

I echo everybody else's suggestion that you go and re-read the memory management rules document.
 
Robbie originally advised you to use an autorelease with your alloc/init pair OR use a factory method that returns an autoreleased object (any class method that returns an object and doesn't begin with the word 'new'). Not both, which is why you are having a problem.

You can do this:

Code:
imageView.image = [[[UIImage alloc] initWithContentsOfFile:@"file.jpg"] autorelease];

OR

Code:
imageView.image = [UIImage imageNamed:@"file.jpg"];

I would probably use the latter as a matter of style.

The imageNamed method returns an autoreleased object; calling autorelease on it again will result in over-releasing and a crash.

I echo everybody else's suggestion that you go and re-read the memory management rules document.

hmmm still not working am i supposed to be declaring the images as properties?
 
sorry yeah i am getting annoyed...
And throwing random solutions to a poorly-understood problem is probably not going to decrease your annoyance.

...its just im only 12...
Really? Then you're not even supposed to be posting on these forums and have lied in order to register. The minimum age is 13. I'm consulting the other mods to see how we normally handle these kinds of situations.

...i dont know xcode that well...
Probably more likely that not knowing Objective-C well enough is the real issue.

Maybe it would just be easier to make actions for each image, and make them setHidden:NO when the button is tapped, would that be easier than messing around with imageviews?
Instead of just proposing a new solution, you should try to understand why the current approach is not working before you abandon it.

hmmm still not working...
You really need to start applying some basic debugging skills and report on the outcome of those rather than just continue with the "not working" reports. What, specifically, is not working? Have you verified your assumptions made in the code? Are you getting compile-time errors and/or warnings? How about run-time? If it's still crashing, what do the crash logs say? Etc. Answers to these things are much more helpful than "still not working".
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.