views:

36

answers:

1

Dear Scholars. I am generating a simple scrollView with some images attached to buttons. This works fine apart from the fact that this scroll view is taking rather much memory.

Since this scrollView is just a sub Menu allowing the user to pick an image and soon after I do not need it, I would like to free this heavy block from memory.

Can you kindly help me out understanding this issue, and free this huge memory block when not needed

int flipFlop = 1;
masksAvailable = 18;
float topMaskXX = 85.0;
float topMaskYY = 96.0;
UIButton *button;
for (int buttonsLoop = 1;buttonsLoop < masksAvailable+1;buttonsLoop++){


  button = [UIButton buttonWithType:UIButtonTypeCustom];
  NSString *tempname = [NSString stringWithFormat:@"mask_frame%i.png",buttonsLoop];

  // This fellow there is the memory eating monster
  [button setBackgroundImage:[UIImage imageNamed:tempname] forState:UIControlStateNormal];

  tempname = nil;

  button.tag = buttonsLoop;
  [button addTarget:self action:@selector(handleMaskKeys:) forControlEvents:UIControlEventTouchUpInside];


  UIImageView *frameForSubImages = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image_frame.png"]];
  frameForSubImages.frame = CGRectMake(0.0, 0.0, 320.0/2.9, 480.0/2.9);
  frameForSubImages.center = CGPointMake(topMaskXX,topMaskYY);
  [scrollView addSubview:frameForSubImages];


  button.frame = CGRectMake(0.0, 0.0, 320.0/3.4, 480.0/3.4);
  button.center = CGPointMake(topMaskXX,topMaskYY);
  if (flipFlop == 1){
   topMaskXX += 150;
  } else {
   topMaskYY += 185.0;
   topMaskXX = 85.0;

  }
  flipFlop = flipFlop * -1;
  [scrollView addSubview:button];






}
A: 

First of all, I'd like to suggest that you do a "Clean All" and a "Build and Analyze" on your project. It is very good at pointing out problems with retain/release.

Secondly, any class that retains objects should define a "dealloc" that releases those objects to make sure they get deleted when the object is released.

-(void) dealloc {
    // release all retained objects here.

    [super dealloc];
}

Thirdly, in your example above, it also looks like frameForSubImages might have an extra retain on it, since you've allocated it (+1 reference) and assigned it to a view (+1 reference) without ever calling release (which would be -1 reference and leave you with a refcount of 1).

Finally, I would also recommend reading the Memory Management Programming Guide for iOS.

samkass