AVISO: Cualquier pregunta que no cumpla ninguno de estos criterios podrá ser borrada sin previo aviso.

Hola tengo una tabla que al tocar cada fila te lleva a otra vista con varios datos e imágenes diferentes en función de la row que elijas. la estructura es la siguiente:

 - (void)viewDidLoad{

[self createData];

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.}

#pragma mark -
#pragma mark Table view data source

//Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return [GrupoSections count];  //el total de elementos de la matriz
  }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [[GrupoData objectAtIndex:section] count];

 }

-( NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section{
return [GrupoSections objectAtIndex:section];

 }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.
[[cell textLabel] setText:[[[comidaData objectAtIndex:indexPath.section] objectAtIndex: indexPath.row] objectForKey:NSLocalizedString( @"name", @"nombres en la tabla")]];

[[cell imageView] setImage:[UIImage imageNamed:[[[comidaData objectAtIndex:indexPath.section] objectAtIndex: indexPath.row] objectForKey:@"picture"]]];

cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

Vista2 = [[Vista2 alloc] initWithNibName:@"vista2" bundle:nil];

vista2.informacion = [[[NSString alloc] initWithString:[[[GrupoData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:NSLocalizedString(@"text", @"texto informativo")]]autorelease];

vista2.imagen = [UIImage imageNamed:[[[GrupoData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"picture"]];

vista2.title = [[[GrupoData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];

[UIView beginAnimations:@"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:self.view cache:YES];

[vista2.view removeFromSuperview]; 
[self.view addSubview:vista2.view];

[UIView commitAnimations];
}

- (void) createData {

NSMutableArray *grupo1;
NSMutableArray *grupo2;

GrupoSections = [[NSMutableArray alloc] initWithObjects:@"Grupo1",@"Grupo2",nil];

  Grupo1 = [[NSMutableArray alloc] init];
  Grupo2 = [[NSMutableArray alloc] init];

 [Grupo1 addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys: 
              @"Fila1",@"name",@"Row1",@"englishNames",@"image1.png",@"picture",@"Descripcion de fila 1",@"text",@"Descripcion of Row 1",@"englishText",nil]];

[Group 2 addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys: 
                   @"Fila 2",@"name",@"Row 1",@"englishNames",@"image2.png",@"picture",@"Descripcion de fila 2",@"text",@"Descripcion of Row 2",@"englishText",nil]];

GrupoData = [[NSMutableArray alloc] initWithObjects:puntallana,sauces,barlovento,garafia,tijarafe,nil];

[Grupo 1 release];
[Grupo 2 release];

}

La cuestión es al pulsar varias veces adelante y atrás la app se me cae y al depurar con analyze obtengo varios potential leak of an object todos relacionados con el contador que lleva una cuenta +1. en concreto obtengo dos pistas de donde esta el problema:

1.Method returns an Objetive C object with a +1 count retain (justo encima de NSMutableDictionary.)

2.Object leaked:Allocated object is not referenced later in this execution path and has a retain count of +1 ( este aviso esta en el grupo 2 debajo del primer NSMutableDictionary del grupo1)

Creo que se debe al NSMutableDictionary pero no se como liberarlo sin que afecte a los datos, lo he intentado pero si coloco un autorelease despues del grupo2 donde están los datos de la fila 2 no me soluciona el problema, la verdad es que me doy cuenta qu estoy bastante verde en depurado de código, a alguien se le ocurre algo?

preguntado 11 Ene '12, 10:24

fidel218's gravatar image

fidel218
1113

editó 13 Ene '12, 09:46

Davix's gravatar image

Davix ♦
2.0k81636


Hola @fidel218,

El codigo que has pegado arriba tiene varias problemas, y te recomendaria que vuelvas a leer de nuevo todo lo relacionado con el memory management.

En este caso en conreto:

 [Grupo1 addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys: 
              @"Fila1",@"name",@"Row1",@"englishNames",@"image1.png",@"picture",@"Descripcion de fila 1",@"text",@"Descripcion of Row 1",@"englishText",nil]];

[Group 2 addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys: 
                   @"Fila 2",@"name",@"Row 1",@"englishNames",@"image2.png",@"picture",@"Descripcion de fila 2",@"text",@"Descripcion of Row 2",@"englishText",nil]];

estas creando un NSMutableDictionare con alloc init (retain count +1) y luego el addObject (retain count +1) , lo que hace que en total tienes un retainCount == 2. Cuando haces release al objeto que lo contiene (Grupo1 o Group2) se disminuye el retain count a +1 y ya pierdes la referencia a el ... lo que produce un memory leak.

Para solucionarlo podrias hacerlos con autorelease :

[Grupo1 addObject:[[[NSMutableDictionary alloc] initWithObjectsAndKeys: 
                  @"Fila1",@"name",@"Row1",@"englishNames",@"image1.png",@"picture",@"Descripcion de fila 1",@"text",@"Descripcion of Row 1",@"englishText",nil] autorelease]];

Lo que es importante en este caso es que no hagas el release al Grupo1 y al Grupo2 , ya que luego estas trabajando con estos objetos. Hazles release en el dealloc, o cuando ya estas seguro que no volveras a usarlos.

No se si hay mas cosas, porque el codigo que has pegado tiene pinta de ser incompleto , lo mas probable es que has borrado parte de el pensando que no esta relacionado...

Conclusion: siempre que haces un [[x aloc] init] tienes que encargarte de hacer el release. Tambien hay que hacerlo con retain] o copy] ...

enlace permanente

respondido 11 Ene '12, 11:55

StaS's gravatar image

StaS
5.1k953

Tu respuesta:
Activar/desactivar vista previa

Sobre esta pregunta

Etiquetas de la pregunta:

×6

Pregunta realizada el: 11 Ene '12, 10:24

Pregunta visitada: 236 veces

Última modificación: 13 Ene '12, 09:46

Seguir esta pregunta

Por Email:

Una vez que entres podrás suscribirte desde aquí para recibir actualizaciones

Por RSS:

Respuestas

Respuestas y Comentarios

Realizar Donación