2011-02-10 33 views
0

我一直在試圖調試這個錯誤,就像幾天一樣,似乎仍然無法理解我的對象究竟在做什麼。代碼如下所示。 - (BOOL)web視圖:(的UIWebView *)webView的shouldStartLoadWithRequest:(的NSURLRequest *)請求navigationType:(UIWebViewNavigationType)navigationType {從點擊鏈接 //提取數據我的自定義類對象即使在保留後也會被釋放

SongObject *數據= [[SongObject的alloc] INIT]; [數據保留]; selectedSong = Data; }

selectedSong是主類中的公共變量,現在瘋狂的事情是使用上述方法在其他地方工作,但是使用了shoulStartLoadWithRequest。

試圖解除它給我「超出範圍」,可能是因爲一些瘋狂的原因,「shouldStartLoadWithRequest」會自動釋放我的對象,即使我說不要?

任何想法的?

編輯:這是H和M文件,其中選擇宋被認爲是舉行。

#import <Foundation/Foundation.h> 
#import "SongObject.h" 


@interface SongObject : NSObject<NSCopying> { 
    NSString *artist; 
    NSString *titel; 
    NSString *album; 
    NSString *genre; 
    NSString *songUrl; 
    NSString *rating; 
    NSString *image; 
    BOOL  local; 
} 
@property (readonly) NSString *getArtist; 
@property (readonly) NSString *getTitle; 
@property (readonly) NSString *getAlbum; 
@property (readonly) NSString *getGenre; 
@property (readonly) NSString *getSongURL; 
@property (readonly) NSString *getRating; 
@property (readonly) NSString *getImage; 
@property (readonly) BOOL  isLocal; 



-(void) setInfo:(NSString *) a: (NSString*) t: (NSString*) ab: (NSString*)g: (NSString*)sU: (NSString*)r: (NSString*) i: (BOOL) loc; 
@end 



#import "SongObject.h" 


@implementation SongObject 
-(id)init 
{ 
    if(self =[super init]) 
    { 
     NSLog(@"Init songobject"); 
    } 
    return self; 
} 
-(id) copyWithZone: (NSZone *) zone { 
    SongObject *newSong = [[SongObject allocWithZone:zone] init]; 
    NSLog(@"_copy: %@", [newSong self]); 
    [newSong setInfo:[self getArtist] :[self getTitle] :[self getAlbum] :[self getGenre] :[self getSongURL] :[self getRating] :[self getImage] :[self isLocal]]; 
    return(newSong); 
} 


-(void)dealloc 
{ 
    NSLog(@"Songobject deallocted from memory..."); 
    [super dealloc]; 
} 
-(void) setInfo:(NSString *) a: (NSString*) t: (NSString*) ab: (NSString*)g: (NSString*)sU: (NSString*)r: (NSString*) i: (BOOL) loc{ 
    artist = a; 
    titel = t; 
    album = ab; 
    genre = g; 
    songUrl = sU; 
    rating = r; 
    image = i; 
    local = loc; 
} 

-(NSString*)getArtist 
{ 
    return artist; 
} 

-(NSString*)getTitle 
{ 
    return titel; 
} 

-(NSString*)getAlbum 
{ 
    return album; 
} 

-(NSString*)getGenre 
{ 
    return genre; 
} 

-(NSString*)getSongURL 
{ 
    return songUrl; 
} 

-(NSString*)getRating 
{ 
    return rating; 
} 

-(NSString*)getImage 
{ 
    return image; 
} 
-(BOOL)isLocal{ 
    return local; 
} 

@end 

回答

0

我終於發現問題SongObject類與NSString變量從未保留。所以selectedSong實際上從來沒有發佈過,但它自己的類內的變量。 通過使用NSString alloc initWithString修復它,然後重寫dealloc方法並在那裏釋放它們。

0

這似乎不可能。小心,因爲你增加了兩次參考計數。一旦進入alloc,並再次保留。 selectedSong看起來像什麼?這是一個自動保留的財產嗎?

此外,有時我不能讀取調試器中的對象,我使用NSLog。那麼你看到了什麼?

重寫-dealloc怎麼樣?你有沒有嘗試在那裏設置斷點?

+0

這是selectedSong應該保存的類文件。 – 2011-02-10 20:45:34

相關問題