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

  • SEARCH
  • Home
  • 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 8008981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:18:35+00:00 2026-06-04T18:18:35+00:00

I am having an issue I cannot seem to figure out. Done tons of

  • 0

I am having an issue I cannot seem to figure out. Done tons of searching, and tried about 50 different variations, so far no dice. Here is my dilemma.

I have 3 methods. One is called when my PageView object loads, another is called whenever a user makes a change, and the last is called whenever the user leaves the page.

First method:

- (void)captureInitialLinesTexture {
@autoreleasepool {
    self.initialLinesTextureCaptured = TRUE;
    GLubyte *buffer =(GLubyte *) malloc (1024 * 1024 * 4 * sizeof(GLubyte));
    glPixelStorei(GL_PACK_ALIGNMENT,1) ;
    glReadPixels(0, 0, 1024, 1024, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    glBindTexture(GL_TEXTURE_2D, [pageTexture name]);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1024, 1024, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    NSData __autoreleasing  *tempData = [NSData dataWithBytes:buffer length:1024*1024*4*sizeof(GLubyte)];
    if (textureData == nil) {
        textureData = [self.pageController.notebookDoc newTextureInPage:self.page];
    }

    [pageTextures addObject:tempData];
    if ([pageTextures count] > 5) {
        [pageTextures removeObjectAtIndex:0];
    }
    textureData.textureData = tempData;
    textureData.page = self.page;
    self.page.texture = textureData;
    self.page.lastLineId = [NSNumber numberWithInt:self.pageController.newLineId - 1];
    [self.pageController saveNotebookWithUndo:FALSE];
    free((GLubyte*)buffer);
  }
}

Second method:

-(void)capturePageTexture {
@autoreleasepool {
    GLubyte *buffer =(GLubyte *) malloc (1024 * 1024 * 4 * sizeof(GLubyte));
    glPixelStorei(GL_PACK_ALIGNMENT,1) ;
    glReadPixels(0, 0, 1024, 1024, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    NSData __autoreleasing  *tempData = [NSData dataWithBytes:buffer length:1024*1024*4*sizeof(GLubyte)];
    [pageTextures addObject:tempData];
    if ([pageTextures count] > 5) {
        [pageTextures removeObjectAtIndex:0];
    }
    free((GLubyte*)buffer);
  }
}

And last method:

-(void)attemptInstantPageTextureCapture {
  if ([pageTextures count] > 0) {
      self.page.texture.textureData = [pageTextures lastObject];
      self.page.lastLineId = [NSNumber numberWithInt:self.pageController.newLineId - 1];
      [self.pageController saveNotebookWithUndo:FALSE];
  }
  [pageTextures removeAllObjects];
  pageTextures = [NSMutableArray arrayWithCapacity:0];
}

My issue is with those NSData *tempData variables. For some reason one of them randomly hangs around after my PageView has gone away (discovered via Allocations Instruments).

If I load the PageView, captureInitialLinesTexture fires. I can then add lots of pen strokes and capturePageTexture fires after every stroke, keeping up to 5 NSData variables in an NSMutableArray names pageTextures. When I leave the PageView, the NSMutableArray is emptied.

Now here is where it gets head-banging-into-wall-ish. The allocation for the NSData is always the same size, 4mb in this case. When the page loads, I have 1 4mb allocation. If I make pen strokes I can get up to 5 more. Then, when I leave the PageView, up to 5 of the 4mb allocations (NSData) get released, but I always get left with 1. If I then go to Call Tree on instruments, it randomly says its from that first method or the second method. Why does it have no problem dumping the 5 NSData that are stored in the array, but then for some reason 1 NSData is left alive somewhere. Furthermore, this mystery NSData comes from either method randomly.

Like I said, I have searched and searched and cannot find a solution to this problem. 4MB of memory is obviously huge, so any leaks of this sort will kill the app over time.

Allocation from one attempt:
Allocation from one attempt

Call Tree for that attempt:
Call tree for that attempt

Allocation for another attempt:
Allocation for another attempt

Call Tree for other attempt:
Call Tree for other attempt

Same exact code fires every time, and every time it is random which NSData stays alive. Anyone have any idea what I am doing wrong? the “page” and “textureData” variables are NSManagedObjects in case you are wondering. As far as things I have tried:

alloc on the NSData objects then calling initWithBytes:length:

initWithBytesNoCopy:Length: – did not call free((GLubyte*)buffer);

initWithBytesNoCopy:Length:freeWhenDone: – did not call free((GLubyte*)buffer);

NSData class method version of the two above attempts

not using @autoreleasepool

not using __autoreleasing for var declaration

making the NSMutableArray pageTextures a property of PageView

setting PageView to nil before it gets Popped off the NavigationController stack

iterating through the pageTextures mutable array using __strong and setting all the NSData to nil or null.

As mentioned above, this all takes place in a PageView class that is one of many of its kind within a PageViewController that is pushed onto and then popped off of a navigation controller. Once the PageViewController (PageView’s only super view) is popped off the nav stack, one random NSData allocation is still alive.

Any suggestions at all would be extremely appreciated! As far as the core data elements mentioned above, the code works exactly as planned. The only single issue is that sneaky live NSData allocation… 🙁

  • 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-06-04T18:18:36+00:00Added an answer on June 4, 2026 at 6:18 pm

    Solved the problem a while back, but figured I would post what happened in case anyone else has the same issue. Basically the problem was that the NSData objects where being assigned to core data entities, and core data is in control of when it releases its own memory. Thus while the code seemed sound, core data was still holding onto the NSData objects, way after they had gone out of scope and the run loop finished (when ARC would have normally kicked in).

    To get around the issue, we just saved the NSData to a file, and changed the model so that the core data entities just contained a string of the file path (NSString) instead of boolean data (NSData).

    Problem solved 🙂

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

Sidebar

Related Questions

I am having the following issue, which I cannot seem to resolve, even searching
Now I know this issue is over-talked about but I cannot seem to find
I have been having this issue for a while now and I cannot seem
So I'm having a pretty annoying issue with IE that I just cannot seem
I am having an xslt issue that I cannot seem to solve. Right now
I am having an issue whereby I cannot use an objects method because of
Having an issue here that I have tried everything I can think of but
I have an issue where I cannot seem to get the values to re-populate
I seem to be having an issue, the application we're using uses a Ninject
I am having an annoying problem that I cannot seem to fix. I have

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.