title: iphone memory leaks and malloc?
body: <p>Okay so im finally to the point where I am testing my iPad App on an actual iPad...</p>
  
  <p>One thing that my app does is display a large (2mb) image in a scroll view. This is causing the iPad to get memory warnings. I run the app in the instruments to check for the leak. </p>
  
  <p>When I load the image, a leak is detected and i see the following in the allocations:</p>
  
  <p>ALl Allocations: 83.9 MB
  Malloc 48.55 MB: 48.55 MB
  Malloc 34.63 MB: 34.63 MB</p>
  
  <p>What im trying to understand is how to plug the leak obviously, but also why a 2MB image is causing a malloc of 20x that size</p>
  
  <p>I am very new to programming in obj-c so im sure this is an obvious thing, but I just cant figure it out. Here is the code:</p>
  
  <pre><code>@interface ChartsViewController : UIViewController &lt;UIScrollViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource&gt; {
      IBOutlet UIScrollView *scrollView;
      UIImageView *imageView;
      NSString *chart;
      NSString *chartFile;
      UIPickerView *picker;
      NSDictionary *chartsDictionary;
      NSArray *chartTypes;
      NSArray *charts;
      IBOutlet UILabel *chartNameLabel;
      IBOutlet UIActivityIndicatorView *activityIndicator;
  
  
  
  }
  
  @property (nonatomic, retain) UIScrollView *scrollView;
  @property (nonatomic, retain) UIImageView *imageView;
  @property (nonatomic, retain) NSString *chart;
  @property (nonatomic, retain) NSString *chartFile;
  @property (nonatomic, retain) IBOutlet UIPickerView *picker;
  @property (nonatomic, retain) NSDictionary *chartsDictionary;
  @property (nonatomic, retain) NSArray *chartTypes;
  @property (nonatomic, retain) NSArray *charts;
  @property (nonatomic, retain) IBOutlet UILabel *chartNameLabel;
  @property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
  
  
  -(IBAction) chartSelected;
  - (void)alertView:(UIAlertView *)actionSheet 
  </code></pre>
  
  <p>///////////////////////////////</p>
  
  <pre><code>-(IBAction) chartSelected {
      [imageView removeFromSuperview];
      imageView = nil;
      chartNameLabel.text = @"";
  
      NSInteger chartTypeRow = [picker selectedRowInComponent:kChartTypeComponent];
      NSInteger chartRow= [picker selectedRowInComponent:kChartComponent];
      chart = [self.charts objectAtIndex:chartRow];
      chartFile = [chart stringByReplacingOccurrencesOfString:@" " withString:@"_"];
  
  
      NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                           NSUserDomainMask, YES);
  
  
      NSString *docsPath = [paths objectAtIndex:0];
      NSString *tempString = [[NSString alloc]initWithFormat:@"%@/%@.jpg",docsPath,chartFile];
  
  
  
  
      NSData *temp = [NSData dataWithContentsOfFile:tempString];
  
      if (temp != NULL){
  
          temp = nil;
          [imageView removeFromSuperview];
          imageView = nil;
  
          UIImageView *tempImage = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile: tempString]];
          [tempString release];
          self.imageView = tempImage;
  
  
          scrollView.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);
          scrollView.maximumZoomScale = 4.0;
          scrollView.minimumZoomScale = .05;
          scrollView.clipsToBounds = YES;
          scrollView.delegate = self;
          scrollView.zoomScale = .3;
  
          [scrollView addSubview:imageView];
          [tempImage release];
          imageView = nil;
          chartNameLabel.text = chart;
  
      }
  
      else {
  
          UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Download Chart" 
                                                         message:@"It appears that you have not yet downloaded this chart. Press OK to download this chart to your iPad. Depending on your internet connection, the download could take several minutes." 
                                                        delegate:self 
                                               cancelButtonTitle:@"OK" 
                                               otherButtonTitles:@"Cancel", nil];
          [alert show];
          [alert release];
  
      }
  
      }
  
  - (void)dealloc {
  
      [imageView release];
      [scrollView release];
      [chartsDictionary release];
      [picker release];
      [chartTypes release];
      [charts release];
      [super dealloc];
  }
  </code></pre>