2011-11-25 255 views
0

我知道這是一個非常愚蠢的問題,但我有一個視圖控制器,其中有一個mapview和一些uibuttons我已經做了每一個thng以消除所有的泄漏bt仍然2-3後切換兩控制器應用程序崩潰。以下是分配和解除分配的代碼。 BTW我沒有得到任何 「沒有recive memoru警告」 ..日Thnx很多可能的內存泄漏

.H/

@interface MapView : BaseViewController <MKMapViewDelegate,MKAnnotation> { 

    MKMapView *mapView; 
    NSMutableArray *placeName; 
    NSString *mid; 
    UISegmentedControl *segmentedControl; 
    IBOutlet UILabel *numberofbeeps; 
    NSInteger badgenumber; 
} 
@property (nonatomic, retain) UILabel *numberofbeeps; 
@property(nonatomic, retain) NSString *mid; 
@property (nonatomic, retain) IBOutlet MKMapView *mapView; 
@property(nonatomic,retain) NSMutableArray *placeName; 
@property (nonatomic,retain) IBOutlet UISegmentedControl *segmentedControl; 


-(IBAction)refreshButtonPressed:(id)sender; 

-(IBAction) segmentedControlIndexChanged; 
-(IBAction)SignUpButtonPressed:(id)sender; 
-(IBAction)BackButtonPressed:(id)sender; 
-(IBAction)AddBeepButtonPressed:(id)sender; 
-(id)initWithAnnotation:(id) annotation; 
-(IBAction)sliderChanged:(id)sender; 
-(IBAction)MyAccountPageButtonPressed:(id)sender; 
-(IBAction)MyBeepsButtonPressed:(id)sender; 

@end 

的.m/

-(void)network:(WNetwork*)network didFinishLoadingWithRequest:(NSInteger)pReq data:(NSMutableDictionary*)pData 
{ 
    [self removeLoader]; 

    switch (pReq) { 
     case JBJsonParser: 
     { 

      NSMutableArray *array = [NSMutableArray new]; 
      self.placeName = array; 
      [array release]; 


      self.placeName = pData; 



      badgenumber = [placeName count]; 

       NSString *checkstring = [[AppHelper mDataManager] objectForKey:@"numberofbeepsnearby"]; 


      NSInteger check = [checkstring intValue]; 

      switch (check) { 
       case 0: 
       { 
        self.numberofbeeps.text =[NSString stringWithFormat:@"No beeps found nearby. Why not beep something?"]; 
        NSLog(@"%@",checkstring); 
       } 
        break; 
       case 1: 
       { 

        self.numberofbeeps.text =[NSString stringWithFormat:@"%@ beep found nearby! %d beeps worldwide.",checkstring,badgenumber]; 
        NSLog(@"%@",checkstring); 
       } 
        break; 

       default: 
       { 
        self.numberofbeeps.text =[NSString stringWithFormat:@"%@ beeps found nearby! %d beeps worldwide.",checkstring,badgenumber]; 
        NSLog(@"%@",checkstring); 
       } 
        break; 
      } 

      if ([placeName count]) 
      { 
       for (int i =0; i < [placeName count]; i++) 
       { 
        NSDictionary *dict = [placeName objectAtIndex:i]; 

        CLLocationCoordinate2D coordinatemain; 
        coordinatemain.latitude = [[dict objectForKey:@"Lat"] doubleValue]; 
        coordinatemain.longitude = [[dict objectForKey:@"long"] doubleValue]; 

        DLog(@"id of Beeps %@", mid); 

        NSString *username = [NSString stringWithFormat:@"by %@",[dict objectForKey:@"username"]]; 
        MyAnnotation *ann = [[MyAnnotation alloc] init]; 

        ann.title = [dict objectForKey:@"beep"]; 

        ann.subtitle = username; 
        ann.beepid=[dict objectForKey:@"beepid"]; 
        ann.coordinate = coordinatemain; 
        ann.coordinate.latitude == [[dict objectForKey:@"Lat"] doubleValue]; 
        ann.coordinate.longitude == [[dict objectForKey:@"long"] doubleValue]; 
        [mapView addAnnotation:ann]; 
        [ann release]; 

       } 
      } 





     } 

      break; 

     default: 
      break; 
    } 

} 



-(IBAction) segmentedControlIndexChanged{ 

    switch (self.segmentedControl.selectedSegmentIndex) { 

     case 0: 

     { 
      Screen1 *objCont = [[Screen1 alloc] initWithNibName:@"Screen1" bundle:nil]; 
      objCont.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
      [self presentModalViewController: objCont animated: YES]; 
      [objCont release]; 
     } 
      break; 

     case 1: 
     { 

      MapView *objCont = [[MapView alloc] initWithNibName:@"MapView" bundle:nil]; 
      objCont.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
      [self presentModalViewController: objCont animated: YES]; 
      [objCont release]; 


     } 
      break; 



     default: 

      break; 

    } 

} 
#pragma mark - 
#pragma mark request delegates 
-(void)makeAccomodationRequest 
{ 


    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init]; 


    [self.mWNetowrk makeRequsetWithURL:URL_Showbeepsmap type:JBJsonParser paramDictionary:paramDic delegate:self]; 
    [paramDic autorelease]; 
} 

-(BOOL)network:(WNetwork*)network shouldStartForRequest:(NSInteger)pReq 
{ 
    [self addLoaderWithtext:@"Loading"]; 


    return YES; 
} 

-(void)network:(WNetwork*)network didFailForRequest:(NSInteger)pReq WithError:(NSString*)error 
{ 
    [AppHelper showAlert:error]; 
    [self removeLoader]; 
} 



-(void)initializeView 
{ 
    [self initializeOutlets]; 
    [self makeAccomodationRequest]; 

} 
-(void)initializeOutlets 
{ 
} 


-(IBAction)BackButtonPressed:(id)sender 
{ 
    Screen1 *objCont = [[Screen1 alloc] initWithNibName:@"Screen1" bundle:nil]; 
    objCont.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentModalViewController: objCont animated: YES]; 
    [objCont release]; 

} 

-(IBAction)refreshButtonPressed:(id)sender 
{ 
    MapView *objCont = [[MapView alloc] initWithNibName:@"MapView" bundle:nil]; 
    objCont.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentModalViewController: objCont animated: YES]; 
    [objCont release]; 


} 
-(IBAction)MyAccountPageButtonPressed:(id)sender 
{ 

    NSString *sid = [[AppHelper mDataManager] objectForKey:@"logout"]; 


    { 
     NSDecimalNumber *session = [[AppHelper mDataManager] objectForKey:@"sid"]; 
     if ([sid isEqualToString:@"logged out"]||session==NULL) { 



      AddPlace *objCont = [[AddPlace alloc] initWithNibName:@"AddPlace" bundle:nil]; 
      objCont.modalTransitionStyle = UIModalTransitionStyleCrossDissolve ; 
      [self presentModalViewController: objCont animated: YES]; 
      [objCont release]; 

     } 

     else { 

      MyAccountPage *objCont = [[MyAccountPage alloc] initWithNibName:@"MyAccountPage" bundle:nil]; 
      objCont.modalTransitionStyle = UIModalTransitionStyleCrossDissolve ; 
      [self presentModalViewController: objCont animated: YES]; 
      [objCont release]; 

     } 



    } 

} 
-(IBAction)MyBeepsButtonPressed:(id)sender 
{ 


    NSString *sid = [[AppHelper mDataManager] objectForKey:@"logout"]; 
    NSDecimalNumber *session = [[AppHelper mDataManager] objectForKey:@"sid"]; 
    //NSString *sessionStr = [session stringValue]; 
    if ([sid isEqualToString:@"logged out"]||session==NULL) { 

     [[AppHelper mDataManager] setValue:@"MyBeeps" forKey:@"appflow"]; 
     AddPlace *objCont = [[AddPlace alloc] initWithNibName:@"AddPlace" bundle:nil]; 
     objCont.modalTransitionStyle = UIModalTransitionStyleCrossDissolve ; 
     [self presentModalViewController: objCont animated: YES]; 
     [objCont release]; 


    } 

    else { 


     MyBeeps1 *objCont = [[MyBeeps1 alloc] initWithNibName:@"MyBeeps1" bundle:nil]; 
     objCont.modalTransitionStyle = UIModalTransitionStyleCrossDissolve ; 
     [self presentModalViewController: objCont animated: YES]; 
     [objCont release]; 
    } 

} 
-(IBAction)AddBeepButtonPressed:(id)sender 
{ 
    NSString *sid = [[AppHelper mDataManager] objectForKey:@"logout"]; 
    NSDecimalNumber *session = [[AppHelper mDataManager] objectForKey:@"sid"]; 


    if([sid isEqualToString:@"logged out"]||session==NULL) { 


     [[AppHelper mDataManager] setValue:@"Addabeep" forKey:@"appflow"]; 
     AddPlace *objCont = [[AddPlace alloc] initWithNibName:@"AddPlace" bundle:nil]; 
     objCont.modalTransitionStyle = UIModalTransitionStyleCrossDissolve ; 
     [self presentModalViewController: objCont animated: YES]; 
     [objCont release]; 
    } 

    else { 
     [[AppHelper mDataManager] setValue:@"Addabeep" forKey:@"appflow"]; 
     Check20M *objCont = [[Check20M alloc] initWithNibName:@"Check20M" bundle:nil]; 
     objCont.modalTransitionStyle = UIModalTransitionStyleCrossDissolve ; 
     [self presentModalViewController: objCont animated: YES]; 
     [objCont release]; 



    } 


} 


#pragma mark - 

#pragma mark MKMapViewDelegate 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 

    if (annotation == mapView.userLocation) { 
     // NSLog(@"nil"); 
     return nil; } 



    MKPinAnnotationView *pinView = nil; 

    static NSString *defaultPinID = @"com.invasivecode.pin"; 
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 




    if (pinView == nil) 
     pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; 
    pinView.pinColor = MKPinAnnotationColorGreen; 
    pinView.frame=CGRectMake(0, 0, 30, 30); 
    pinView.canShowCallout = YES; 
    pinView.animatesDrop = YES; 


    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    MyAnnotation *temp = (MyAnnotation*)annotation; 
    infoButton.tag = [temp.beepid integerValue]; 
    [infoButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside]; 
    pinView.rightCalloutAccessoryView = infoButton; 
    [defaultPinID release]; 

    return pinView; 
} 



-(IBAction)showDetails:(id)sender{ 

    UIButton *button = (UIButton*)sender ; 

    NSLog(@"Annotation Click"); 


    BeepsDetail *objCont = [[BeepsDetail alloc] initWithNibName:@"BeepsDetail" bundle:nil]; 
    objCont.mId = [NSString stringWithFormat:@"%d",button.tag]; 
    objCont.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentModalViewController: objCont animated: YES]; 

    [objCont release]; 

} 


#pragma mark - 
#pragma mark mapView delegates 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 

} 
return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    mapView.delegate = self; 
    [self initializeView]; 
    mapView.showsUserLocation = YES; 

    [self makeAccomodationRequest]; 


    CLLocation *location = [[AppHelper appDelegate] mLatestLocation]; 
    MKCoordinateRegion region; 
    region.center.latitude = location.coordinate.latitude; 

    region.center.longitude = location.coordinate.longitude; 

    region.span.latitudeDelta = 0.001; 

    // Add a little extra space on the sides 
    region.span.longitudeDelta = 0.001; 

    // Add a little extra space on the sides 
    region = [mapView regionThatFits:region]; 
    [mapView setRegion:region animated:YES]; 








} 

// Listen to change in the userLocation 

- (void)viewDidUnload 
{ 

    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
    self.mapView = nil; 
    self.numberofbeeps =nil; 
    self.mapView = nil; 
    self.segmentedControl = nil; 
} 
- (void)dealloc 
{ 
    [mapView release]; 
    // [self.mapView removeFromSuperview]; 
    [placeName release]; 
    //[mid autorelease]; 
    [super dealloc]; 


} 
- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc. that aren't in use. 
} 




@end 
+7

發佈崩潰日誌和堆棧跟蹤。此外,泄漏不會導致崩潰,過度釋放。打開NSZombies以幫助查找與內存相關的崩潰,並添加異常斷點。 – zaph

+0

我沒有得到任何日誌除了 (gdb).. – iPhoneDev

+1

不是你的崩潰的解釋,但:你不釋放中viewDidUnload - 是故意的?另外,你應該釋放dealloc中的所有內容,而不僅僅是mapView和placeName。無論如何,就像@CocoaFu所說的那樣,泄漏本身不會導致崩潰。除了這段代碼,你還沒有在這個課上有什麼其他的... –

回答

2

下只適用於模擬器沒有設備...


這裏是尋找使用殭屍壞訪問錯誤的好方法。所有的工具都內置在Xcode中。

首先更改爲分析。點擊並按住一個運行按鈕。

profile

現在選擇殭屍。這個工具就在你要使用一個釋放對象的時候發出警告,這個對象會導致一個不好的訪問。

zombies!

現在檢測到一個殭屍,當你使用這些工具,你可以看到該對象的生命週期會看到這樣的事情(減去annotatios!)。雙擊該代碼。

enter image description here

希望它可以幫助別人!

+0

我無法找到配置文件 – iPhoneDev

+2

菜單中的殭屍您需要在模擬器上運行而不是設備來查看殭屍選項。另外,請確保您擁有最新版本的XCode。 – Robert

0

我會建議申報模式視圖控制器(objCont)作爲autorelease而不是手動釋放它們。