Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:43:15+00:00 2026-05-25T20:43:15+00:00

This has to be very basic, but I don’t see the problem. The program

  • 0

This has to be very basic, but I don’t see the problem. The program crashes whenever the following code block is executed. Analyzer reports a possible memory leak:

if (anImage) {
    eventImageView.frame = defaultEventImageFrame;
    UIImage *scaledImage = [anImage scaleToFitWithin:defaultEventImageFrame.size interpolationQuality:kCGInterpolationHigh];
    eventImageView.backgroundColor = [UIColor colorWithPatternImage:scaledImage];                
}

The message is -[UIImage release]: message sent to deallocated instance 0x1129d920*

Instance 0x1129d920 is scaledImage

I tried adding retains and releases, like this

if (anImage) {
    eventImageView.frame = defaultEventImageFrame;
    UIImage *scaledImage = [[anImage scaleToFitWithin:defaultEventImageFrame.size interpolationQuality:kCGInterpolationHigh] retain];
    eventImageView.backgroundColor = [UIColor colorWithPatternImage:scaledImage];                
    [scaledImage release];        
}

and still get the error message.

So I tried replacing the assignment with a copy, like this

if (anImage) {
    eventImageView.frame = defaultEventImageFrame;
    UIImage *scaledImage = [anImage copy];
    eventImageView.backgroundColor = [UIColor colorWithPatternImage:scaledImage];                
}

And the problem is gone.

Checking the scaleToFitWithin method, I see it returns an autoreleased object:

- (UIImage *) scaleToFitWithin:(CGSize) newSize
          interpolationQuality:(CGInterpolationQuality)quality{

    CGSize originalImageSize = self.size;
    CGSize newImageSize;
    if (originalImageSize.width <= originalImageSize.height) {
        newImageSize.width = self.size.width * newSize.width  / self.size.width;
        newImageSize.height = self.size.height * newSize.width  / self.size.width;
    }
    else {
        newImageSize.width = self.size.width * newSize.height  / self.size.height;
        newImageSize.height = self.size.height * newSize.height  / self.size.height;        
    }
    return [[[self normalize] resizedImage:newImageSize interpolationQuality:kCGInterpolationHigh] autorelease];

}

So there is something about memory management that I’m not understanding. What is the problem likely to be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:43:16+00:00Added an answer on May 25, 2026 at 8:43 pm

    The problem is most likely that the scaleToFitWithin:interpolationQuality: method is calling autorelease on an object which has already previously been autoreleased. This can occur if you initialise the UIImage using a temporary constructor, like +[UIImage imageWith...], earlier in the same method you call the scaling method from. The reason it works when you use [anImage copy] is because the behaviour of the copy constructor is such that the object returned to you has already had retain called on it (so it has a local retain count of 1 and zero autoreleases).

    What happens at the end of the current run loop is: the autorelease pool which is currently in use is drained, and as a part of that two release messages will be sent to the UIImage. When the first one is sent, the application then runs off and calls dealloc on the image, because the retainCount has decreased to zero. When the second one is sent, the application throws an exception because a message is being sent to an object which was previously deallocated.

    Try removing the autorelease message from the scaleToFitWithin:interpolationQuality: method. Even if your resizedImage:interpolationQuality: method IS returning a new object, you should only be calling autorelease in that method rather than the scaling method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question seems to be very basic, but I don't know why, there is
I'm generally not a fan of microbenchmarks. But this one has a very interesting
Sorry if this is very basic as a question....but I can't my head around
This may seem like a very basic question but I did not seem to
First of all thanks in advance, this has been very frustrating and I'm hoping
I'm hoping this question has a very simple answer. I can think of ways
This has been a problem that I haven't been able to figure out for
This has been driving me crazy for a few days. Why doesn't the following
This has got to be something I just missed, but how do I add
I'm having some trouble with a very basic CUDA program. I have a program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.