开发者

IS this class Many part of memory Leak

I am much confuse about my class.

Speci开发者_StackOverflow中文版ally about Memory Management.

Please Guide me about NSString Concept at here.

My Class is.

#import <Foundation/Foundation.h>


@interface itinerary_detail : NSObject {
    NSString *itinerary_title;
    NSString *itinerary_creator;
    NSString *itinerary_identifiere;

    NSString *itinerary_created;
    NSString *itinerary_modified;    
}
@property (retain) NSString *itinerary_title;
@property (retain) NSString *itinerary_creator;
@property (retain) NSString *itinerary_identifiere;
@property (retain) NSString *itinerary_created;
@property (retain) NSString *itinerary_modified;

-(void) itinerary_initialization;
-(void) itinerary_title:(NSString *) xml_value;
-(void) itinerary_creator:(NSString *) xml_value;
-(void) itinerary_identifiere:(NSString *) xml_value;
-(void) itinerary_created:(NSString *) xml_value;
-(void) itinerary_modified:(NSString *) xml_value;
@end

and My .m class is

#import "itinerary_detail.h"
@implementation itinerary_detail
@synthesize itinerary_title,itinerary_creator,itinerary_identifiere,itinerary_created,itinerary_modified;
-(void) itinerary_initialization
{
    itinerary_title=@"null";
    itinerary_creator=@"null";
    itinerary_identifiere=@"null";
    itinerary_created=@"null";
    itinerary_modified=@"null";
}

-(void) itinerary_title:(NSString *) xml_value
{
    itinerary_title=xml_value; 
}
-(void) itinerary_creator:(NSString *) xml_value
{
    itinerary_creator=xml_value; 
}
-(void) itinerary_identifiere:(NSString *) xml_value
{
    itinerary_identifiere=xml_value;
}
-(void) itinerary_created:(NSString *) xml_value
{
    itinerary_created=xml_value;
}
-(void) itinerary_modified:(NSString *) xml_value
{
    itinerary_modified=xml_value;
}
-(void) dealloc
{
    [itinerary_title release];
    [itinerary_creator release];
    [itinerary_identifiere release];
    [itinerary_created release];
    [itinerary_modified release];
    [super dealloc];
}
@end

My question about.

1- Is this type Deceleration of NSString in this class of Memory Leak Issue. If Yes Please How i Will change this.

2- I am Using This class into Other class Like that

itinerary_detail *check=[[itinerary_detail alloc] init];
[check itinerary_initialization];
[check release];

my question is this right way . or this is also a Memory Leak Issue.

Please Guide Me How to Deceleration Of this class and How to handle all memory Leak Issues.

Please Help Me


The problem come from the fact that you don't use the property but directly access the member variable. replace itinerary_title=xml_valueby self.itinerary_title=xml_value

btw, string properties are usually (copy) and not (retain) and why do you create all those methods while the synthesize will do it for you.

remove the methods from the .h file and from the .m file and set the property as

@property (copy) NSString* myString;


Your code shows that you need to get the basics of Cocoa and Objective-C right, before writing an actual program. Read Cocoa Fundamentals, OOP with Objective-C, etc. Resist the urge to start writing programs right now; the time you'll spend to learn the basics will greatly reduce your headache later.

Your code should look like:

@interface ItineraryDetail : NSObject {
    NSString *itineraryTitle; 
    ...
}
@property (retain) NSString *itineraryTitle;
@end

and

@implementation ItineraryDetail
@synthesize itineraryTitle, ... ;

-(id)init{
     self=[super init];
     if(self){
         itineraryTitle=nil;
     }
     return self;
}
-(void) dealloc
{
    [itineraryTitle release];
    [super dealloc];
}
@end

and

ItineraryDetail *check=[[ItineraryDetail alloc] init];
  ... use it ...
[check release];

A few points:

  1. In Objective-C, you don't usually name_like_this. You NameLikeThis. This is not an absolute rule, but it's customary, and you should follow it in general.

  2. You don't write a method like ...Initialize separately. Rather, it's implemented using init, with [super init] inside it.

  3. When you synthesize a property named foo via @synthesize foo, the setter setFoo: and the getter foo: are automatically generated, so you don't have to provide them manually. And you mistakenly used the name foo: for the setter! That will totally confuse the system.

  4. The nil value for NSString (or any object in Objective-C in general) is not @"null" but just nil. And the ivars are set to nil automatically by the system, so you don't really do that in the initialization method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜