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 6644305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:09:08+00:00 2026-05-26T00:09:08+00:00

The code below shows how I’m cutting my sprites, but the memory usage grows

  • 0

The code below shows how I’m cutting my sprites, but the memory usage grows constantly. How can I fix?

CGImageRef imgRef = [imgSprite CGImage];
[imgView setImage:[UIImage imageWithCGImage:CGImageCreateWithImageInRect(imgRef, CGRectMake(column*width, line, width, height))]];
CGImageRelease(imgRef);

This code is called by the NSTimer in an interval of 0.1.

  • 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-26T00:09:08+00:00Added an answer on May 26, 2026 at 12:09 am

    Since you haven’t posted the declaration of imgSprite, I’ll assume that its class follows Cocoa naming conventions.

    In:

    CGImageRef imgRef = [imgSprite CGImage];
    

    that method (a non-NARC1 method) returns an object that you do not own, hence you should not release it.

    In:

    [imgView setImage:[UIImage imageWithCGImage:CGImageCreateWithImageInRect(imgRef, CGRectMake(column*width, line, width, height))]];
    

    the argument is the expression:

    CGImageCreateWithImageInRect(imgRef, CGRectMake(column*width, line, width, height))
    

    CGImageCreateWithImageInRect() (a function whose name follows the Create Rule2) returns an image that you do own, hence you should release it, which you don’t.

    In:

    CGImageRelease(imgRef);
    

    you’re releasing an image that you do not own, so you should not release it.

    You have two problems: you’re (potentially over)releasing imgRef and you’re leaking the image returned by CGImageCreateWithImageInRect().

    You should do the following instead:

    // you do not own imgRef, hence you shouldn’t release it
    CGImageRef imgRef = [imgSprite CGImage];
    
    // use a variable for the return value of CGImageCreateWithImageInRect()
    // because you own the return value, hence you should release it later
    CGImageRef imgInRect = CGImageCreateWithImageInRect(imgRef, CGRectMake(column*width, line, width, height));
    
    [imgView setImage:[UIImage imageWithCGImage:imgInRect]];
    
    CGImageRelease(imgInRect);
    

    You might want to read the Memory Management Programming Guide and the Memory Management Programming Guide for Core Foundation.

    1NARC = new, alloc, retain, copy

    2The Create Rule states that if you call a function whose name contains Create or Copy then you own the return value, hence you should release it when you don’t need it any longer.

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

Sidebar

Related Questions

The code below shows a line in different colors, but that's a lot of
The code below shows nested while loops, but it's not the very efficient. Suppose
The code below shows a Wordpress loop. How can I modify this loop to
The code below shows adding of ascending-descending images when sorting the GridView but the
The code below shows one email address as a output but I want to
The code below shows a sample that I've used recently to explain the different
The code below shows me (I think) that the for each loop is about
My PHP code below shows an image on browser as its original size 300x300.
The code below compiled in Debug configuration in VS2005 SP1 shows two messages with
I have the code below that hides and shows the navigational bar. It is

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.