我是iPhone开发的新手。 我使用单音设计模式。当我分析我的代码时,它给了我内存泄漏警告,因为“具有+0保留计数的对象被返回给调用者,其中在allocWithZone方法中预期+1(欠)保留计数。”
以下是我的单音班级代码:
#import "SharedHotelMenu.h"
@implementation SharedHotelMenu
//Static instance of Singletone class
static SharedHotelMenu *_hotelMenu=nil;
// Synthesizing properties
@synthesize _hotelEmails,_hotelNames,_hotelPhoneNos;
@synthesize _menuCategoryNames;
@synthesize _menuDetailsNames,_menuDetailsPrices,_menuDetailsFavuorites;
@synthesize _menuTypeNames;
@synthesize _orderedItemName,_orderedItemPrice,_orderedItemQuantity;
@synthesize _selectedMenuContent,_selectedMenuName;
@synthesize _selectedMenuPrice,_selectedMenuQuantity;
@synthesize _menuImageFlag,_orderByFlag,_navigateViewFlag,_registerFlag;
@synthesize _hintA,_hintQ,_userAddress,_userCity,_userCountry,_userEmailID,_toEmailID;
@synthesize _userFirstName,_userGender,_userLastName,_userState;
@synthesize _userCellNo,_userZip,_userRememberFlag,_userOrderNo;
@synthesize _userName,_userPassword;
//method as the global access point
+ (id)sharedInstanceMethod {
//synchronized for thread safety when initializing
@synchronized(self) {
if(_hotelMenu == nil)
_hotelMenu = [[super allocWithZone:NULL] init];
}
return _hotelMenu;
}
+ (id)allocWithZone:(NSZone *)zone {
return [[self sharedInstanceMethod] retain];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
//sets the object so it can not be released
- (unsigned)retainCount {
return UINT_MAX;
}
- (id)autorelease {
return self;
}
//initializing shared object
- (id)init {
if (self = [super init]) {
_hotelEmails = [[NSMutableArray alloc]init];
_hotelNames = [[NSMutableArray alloc]init];
_hotelPhoneNos = [[NSMutableArray alloc]init];
_menuTypeNames = [[NSMutableArray alloc]init];
_menuDetailsPrices = [[NSMutableArray alloc]init];
_menuDetailsNames = [[NSMutableArray alloc]init];
_menuCategoryNames = [[NSMutableArray alloc]init];
_orderedItemName = [[NSMutableArray alloc]init];
_orderedItemPrice = [[NSMutableArray alloc]init];
_orderedItemQuantity = [[NSMutableArray alloc]init];
_menuDetailsFavuorites = [[NSMutableArray alloc]init];
_selectedMenuContent = [[NSString alloc]init];
_selectedMenuName = [[NSString alloc]init];
_hintQ = [[NSString alloc]init];
_hintA = [[NSString alloc]init];
_userAddress = [[NSString alloc]init];
_userCity = [[NSString alloc]init];
_userCountry = [[NSString alloc]init];
_userState = [[NSString alloc]init];
_userEmailID = [[NSString alloc]init];
_toEmailID = [[NSString alloc]init];
_userFirstName = [[NSString alloc]init];
_userLastName = [[NSString alloc]init];
_userGender = [[NSString alloc]init];
_userName = [[NSString alloc]init];
_userPassword = [[NSString alloc]init];
_userCellNo = [[NSString alloc]init];
_userZip = [[NSString alloc]init];
_userOrderNo = [[NSString alloc]init];
_selectedMenuPrice = 0;
_selectedMenuQuantity = 0;
_menuImageFlag = 0;
_orderByFlag = 0;
_navigateViewFlag = 0;
_userRememberFlag = 0;
_registerFlag = 0;
}
return self;
}
// Should never be called,
- (void)dealloc {
[super dealloc];
}
@end