Constructeur de commodité
geekspirit
Membre
Bonjour,
J'essaie de faire un constructeur de commodité mais j'ai une erreur :
La classe concernée :
WebData.h
WebData.m
Lorsque j'enlève l'autorelease de
Je n'ai plus d'erreur :-(
Aidez moi svp
Merci
J'essaie de faire un constructeur de commodité mais j'ai une erreur :
<br />malloc: *** error for object 0x397d550: double free<br />*** set a breakpoint in malloc_error_break to debug<br />
La classe concernée :
WebData.h
<br />#import "JSON.h"<br />#import <Foundation/Foundation.h><br /><br /><br />@interface WebData : NSObject {<br /> <br />}<br /><br />// Public funtion<br />+(NSDictionary*)webDataWithParam:(NSDictionary*)param;<br /><br />@end<br />
WebData.m
<br />#import "WebData.h"<br /><br />@interface WebData ()<br /><br />- (NSDictionary*)initWithParam:(NSDictionary*)param;<br />- (NSString*)getHttpAt:(NSString *)url withParam:(NSDictionary*)param;<br /><br />@end<br /><br /><br /><br />@implementation WebData<br /><br /><br /><br /><br /># pragma -<br /># pragma Object methode<br /><br />// Méthode de commodité<br />+(NSDictionary*)webDataWithParam:(NSDictionary*)param { return [[[WebData alloc] initWithParam:param] autorelease]; }<br /><br /><br />// Méthode d'initialisation<br />- (NSDictionary*)initWithParam:(NSDictionary*)param{<br /> <br /> // Url du web service<br /> NSString *url = @"http://xxxxxxxxx/Data.php";<br /> <br /> // Récupération du code source<br /> NSString *dataSource = [self getHttpAt:url withParam:param];<br /> <br /> // Creation de l'objet Json<br /> SBJSON *JsonObj = [[SBJSON alloc] init];<br /> <br /> // Récupération des données<br /> NSDictionary *resData = [NSDictionary dictionaryWithDictionary:[JsonObj objectWithString:dataSource error:nil]];<br /> <br /> // Vide la mémoire<br /> [JsonObj release]; <br /> <br /> return resData;<br /> <br />}<br /><br /><br />// Retourne le source d'une page web<br />- (NSString*)getHttpAt:(NSString *)url withParam:(NSDictionary*)param{<br /> <br /> // Création des paramètres<br /> NSMutableString *strParam = [NSMutableString string];<br /> <br /> // Parcours des Key Value<br /> for (id k in param) { [strParam appendFormat:@"&%@=%@",k,[param objectForKey:k]]; } <br /> <br /> // Suppression du premier &<br /> strParam = [strParam substringFromIndex:1]; <br /> <br /> // Encodage des paramètres<br /> NSData *postData = [strParam dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];<br /> <br /> // Taille des paramètres<br /> NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];<br /> <br /> // Création de la requête<br /> NSMutableURLRequest *httpRequest = [[[NSMutableURLRequest alloc] init] autorelease];<br /> [httpRequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];<br /> [httpRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];<br /> [httpRequest setURL:[NSURL URLWithString:url]];<br /> [httpRequest setHTTPMethod:@"POST"];<br /> [httpRequest setTimeoutInterval:10];<br /> [httpRequest setHTTPBody:postData];<br /> <br /> // Création du NSURLConnection<br /> NSError *error; NSURLResponse *response;<br /> NSData *data = [NSURLConnection sendSynchronousRequest:httpRequest returningResponse:&response error:&error];<br /> <br /> return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]autorelease];<br /> <br />}<br /><br /><br /><br /><br /><br /># pragma -<br /># pragma Memory methode<br /><br />// Deallocates the memory occupied by the receiver.<br />- (void)dealloc {<br /> <br /> [super dealloc];<br />}<br /><br /><br />@end<br />
Lorsque j'enlève l'autorelease de
<br />+(NSDictionary*)webDataWithParam:(NSDictionary*)param { return [[[WebData alloc] initWithParam:param] autorelease]; }<br />
Je n'ai plus d'erreur :-(
Aidez moi svp
Merci
Connectez-vous ou Inscrivez-vous pour répondre.
Réponses
Tu ne retournes pas d'instance de WebData, mais un NSDictionary
cf. Objective-C 2.0 programming guide, toussa toussa (same player shoot again ::) ).
Par contre je suis curieux de savoir pourquoi je ne respecte pas la regle de nommage pour le init ? C'est la deuxième fois qu'on me le dit, sa m'alerte quelque peut.
Merci
Pas les règles de nommage, les règles d'implémentation.