Memory leak with objective-c on alloc

Posted by Grunzig on Stack Overflow See other posts from Stack Overflow or by Grunzig
Published on 2011-01-10T09:02:41Z Indexed on 2011/01/10 9:53 UTC
Read the original article Hit count: 199

Filed under:

When I use Instruments to find memory leaks, a leak is detected on

Horaires *jour;     
jour= [[Horaires alloc] init]; // memory leak reported here by Instruments
self.lundi = jour;
[jour release];

and I don't know why there is a leak at this point.

Does anyone can help me? Here's the code.

// HorairesCollection.h
#import <Foundation/Foundation.h>
#import "Horaires.h"

@interface HorairesCollection : NSObject < NSCopying > {
    Horaires *lundi;
}

@property (nonatomic, retain) Horaires *lundi;
-init;
-(void)dealloc;
@end


// HorairesCollection.m
#import "HorairesCollection.h"

@implementation HorairesCollection

@synthesize lundi;

-(id)copyWithZone:(NSZone *)zone{
    DefibHoraires *another = [[DefibHoraires alloc] init];
    another.lundi = [lundi copyWithZone: zone];
    [another autorelease];
    return another;
}

-init{
    self = [super init];
    Horaires *jour;     
    jour= [[Horaires alloc] init]; // memory leak reported here by Instruments
    self.lundi = jour;
    [jour release];
    return self;
}

- (void)dealloc {
    [lundi release];
    [super dealloc];
}

@end


// Horaires.h
#import <Foundation/Foundation.h>

@interface Horaires : NSObject <NSCopying>{
    BOOL ferme;
    BOOL h24;
    NSString *h1;
}

@property (nonatomic, assign) BOOL ferme;
@property (nonatomic, assign) BOOL h24;
@property (nonatomic, retain) NSString *h1;

-init;
-(id)copyWithZone:(NSZone *)zone;
-(void)dealloc;

@end


// Horaires.m
#import "Horaires.h"
@implementation Horaires

-(BOOL) ferme {
    return ferme;
}

-(void)setFerme:(BOOL)bFerme{
    ferme = bFerme;
    if (ferme) {
        self.h1 = @"";
        self.h24 = NO;
    }
}

-(BOOL) h24 {
    return h24;
}

-(void)setH24:(BOOL)bH24{
    h24 = bH24;
    if (h24) {
        self.h1 = @"";
        self.ferme = NO;
    }
}

-(NSString *) h1 {
    return h1;
}

-(void)setH1:(NSString *)horaire{
    [horaire retain];
    [h1 release];
    h1 = horaire;
    if (![h1 isEqualToString:@""]) {
        self.h24 = NO;
        self.ferme = NO;
    }
}

-(id)copyWithZone:(NSZone *)zone{
    Horaires *another = [[Horaires alloc] init];
    another.ferme = self.ferme;
    another.h24 = self.h24;
    another.h1 = self.h1;
    [another autorelease];
    return another;
}

-init{
    self = [super init];
    return self;
}

-(void)dealloc {
    [h1 release];
    [super dealloc];
}

@end

© Stack Overflow or respective owner

Related posts about objective-c