Ios4 我的自定义类对象即使在保留后也会被释放

Ios4 我的自定义类对象即使在保留后也会被释放,ios4,release,retain,iphone,Ios4,Release,Retain,Iphone,我已经试着调试这个错误好几天了,但似乎仍然不能理解我的对象到底发生了什么。 代码是这样的。 -(BOOL)webView:(UIWebView*)webView应加载WithRequest:(NSURLRequest*)请求导航类型:(UIWebViewNavigationType)导航类型{ //从单击的链接中提取数据 SongObject*数据=[[SongObject alloc]init]; [数据保留]; selectedSong=数据; } selectedSong是主类中的公共变量

我已经试着调试这个错误好几天了,但似乎仍然不能理解我的对象到底发生了什么。 代码是这样的。 -(BOOL)webView:(UIWebView*)webView应加载WithRequest:(NSURLRequest*)请求导航类型:(UIWebViewNavigationType)导航类型{ //从单击的链接中提取数据

SongObject*数据=[[SongObject alloc]init]; [数据保留]; selectedSong=数据; }

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
#导入
#导入“SongObject.h”
@接口对象:NSObject{
NSString*艺术家;
NSString*滴度;
NSString*相册;
NSString*体裁;
NSString*songUrl;
NSString*额定值;
NSString*图像;
布尔当地;
}
@属性(只读)NSString*GetArtister;
@属性(只读)NSString*getTitle;
@属性(只读)NSString*getAlbum;
@属性(只读)NSString*getgreen;
@属性(只读)NSString*getSongURL;
@属性(只读)NSString*getRating;
@属性(只读)NSString*getImage;
@属性(只读)BOOL isLocal;
-(void)setInfo:(NSString*)a:(NSString*)t:(NSString*)ab:(NSString*)g:(NSString*)sU:(NSString*)r:(NSString*)i:(BOOL)loc;
@结束
#导入“SongObject.h”
@SongObject的实现
-(id)init
{
if(self=[super init])
{
NSLog(@“Init songobject”);
}
回归自我;
}
-(id)copyWithZone:(NSZone*)区{
SongObject*newSong=[[SongObject allocWithZone:zone]init];
NSLog(@“_copy:%@,[newSong self]);
[newSong setInfo:[self-getArtist]:[self-getitle]:[self-getAlbum]:[self-getGenre]:[self-getSongURL]:[self-getRating]:[self-getImage]:[self-isLocal];
返回(新闻歌曲);
}
-(无效)解除锁定
{
NSLog(@“Songobject已从内存中解除分配…”);
[super dealoc];
}
-(void)setInfo:(NSString*)a:(NSString*)t:(NSString*)ab:(NSString*)g:(NSString*)sU:(NSString*)r:(NSString*)i:(BOOL)loc{
艺术家=a;
滴度=t;
相册=ab;
类型=g;
songUrl=sU;
评级=r;
图像=i;
本地=loc;
}
-(NSString*)GetArtister
{
回归艺术家;
}
-(NSString*)getTitle
{
返回滴度;
}
-(NSString*)getAlbum
{
返回相册;
}
-(NSString*)GetGene
{
回归体裁;
}
-(NSString*)getSongURL
{
返回songUrl;
}
-(NSString*)getRating
{
回报率;
}
-(NSString*)获取图像
{
返回图像;
}
-(BOOL)isLocal{
返回本地;
}
@结束

这似乎是不可能的。小心,因为您将引用计数增加了两次。一次在alloc中,一次在retain中。selectedSong看起来像什么?它是自动保留的财产吗

此外,有时我无法在调试器中读取对象,我使用NSLog。那么你看到了什么


覆盖-解除锁定怎么样。您尝试在那里设置断点了吗?

我终于发现了一个问题:带有NSString变量的SongObject类从未被保留。因此,selectedSong实际上从未发布过,但类中的变量是它自己发布的。
通过使用NSString alloc initWithString,然后重写dealloc方法并在其中释放它们,修复了此问题。

这是selectedSong应保存的类文件。同样,在使用NSLog时,如NSLog(@“Artister:,[selectedSong GetArtister]);不返回任何内容。