My custom class object gets released even after retaining it

106 Views Asked by At

I have been trying to debug this error like for days and still can't seem to grasp what really is going on with my object. The code goes like this. - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { // extract data from clicked link

SongObject *Data = [[SongObject alloc] init]; [Data retain]; selectedSong = Data; }

selectedSong is public variable in the main class, now the crazy thing is that using the above method works everywhere else but shoulStartLoadWithRequest.

Trying to deubg it gives me "Out Of Scope", could it be by some crazy reason that "shouldStartLoadWithRequest" is autoreleasing my object even when I say don't?

Any idea's?

Edit: This is the H and M file which selectedSong is suppose to hold.

#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
2

There are 2 best solutions below

0
On BEST ANSWER

I finally found the problem the SongObject class with the NSString variables was never retained. So the selectedSong was actually never released but the variables within the class it self. Fixed it by using NSString alloc initWithString and then overriding the dealloc method and releasing them in there.

2
On

That doesn't seem possible. Be careful, cause you're increasing the reference count twice. Once in the alloc, and again with the retain. What does selectedSong looks like? is it a property with automated retain?

Also, sometimes I cannot read the object in the debugger and I use NSLog. What do you see then?

What about overriding -dealloc. Did you try setting a breakpoint there?