Frage zur Speicherverwaltung

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Frage zur Speicherverwaltung

    Hallo,

    ich hab ne Frage zur Speicherverwaltung beim iPhone.

    Folgendes: Ich hab ne UITabBarApplication, in jeder "Tab" befindet sich ein UINavigationController.

    Nun hab ich in der ersten Tab eine UITableView.

    in der Methode:

    Quellcode

    1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath


    habe ich jetzt nur mal besipsielhaft:

    neuerController = [[NeuerController alloc] initWithNibName:@"NeuerController" bundel:nil];
    [self.navigationController pushViewController: neuerController animated:YES];

    --- macht was ....

    und dann

    [neuerController release]; das alles in der o.g. Methode.


    neuerController wird mehrmals alloziiert und releaseD (freigegeben) und alles in der Methode didSelectRowAtIndexPath

    Meine Frage nun: ist damit die Speicherverwaltung erledigt? - irgendwie bekomme ich in Instruments nach 3 bis 4 mal hin-und- her bewegen von RootView zu neuerController ein Leak.

    Woran kanns liegen??

    ''EDIT ###

    Ich glaube es liegt mehr an der UITableView als an den Controllern. Denn allein beim scrollen durch die TableView tritt ein Leak auf.
    Wie kann ich denn das beheben???

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Miralem23 ()

  • pushViewController:
    Erhöht nochmal den retianCount und somit ist der Controller nicht freigegeben.
    release gibt nur dann den Speicher frei, wenn der retainCount des Objekts auf 1 steht.

    Versuchs mal so:
    [self.navigationController popViewControllerAnimated:NO];
    neuerController = [[NeuerController alloc] initWithNibName:@"NeuerController" bundel:nil];
    [self.navigationController pushViewController: neuerController animated:YES];

    --- macht was ....

    und dann

    [neuerController release];
    Inos ist ein Gott aus Gothic, dem Spiel.

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von Inos ()

  • Miralem23 schrieb:

    Meine Frage nun: ist damit die Speicherverwaltung erledigt? - irgendwie bekomme ich in Instruments nach 3 bis 4 mal hin-und- her bewegen von RootView zu neuerController ein Leak.

    Woran kanns liegen??

    Beachtest Du die Speicherverwaltungsregeln? developer.apple.com/iphone/lib…ef/doc/uid/TP40004447-SW1

    Der Retain-Count sollte Dich nicht interessieren, da auch andere Objekte ein Objekt halten können.
    „Meine Komplikation hatte eine Komplikation.“
  • Inos schrieb:

    Versuchs mal so:
    [self.navigationController popViewControllerAnimated:NO];
    neuerController = [[NeuerController alloc] initWithNibName:@"NeuerController" bundel:nil];
    [self.navigationController pushViewController: neuerController animated:YES];

    Das würde ich nicht machen. Das dürfte beim ersten Aufruf der Methode tableView:didSelectRowAtIndexPath: den "RootView-Controller", also self, entsorgen.

    Michael
  • Michael schrieb:

    Inos schrieb:

    Versuchs mal so:
    [self.navigationController popViewControllerAnimated:NO];
    neuerController = [[NeuerController alloc] initWithNibName:@"NeuerController" bundel:nil];
    [self.navigationController pushViewController: neuerController animated:YES];

    Das würde ich nicht machen. Das dürfte beim ersten Aufruf der Methode tableView:didSelectRowAtIndexPath: den "RootView-Controller" entsorgen.

    Michael

    Nein.
    "This method removes the top view controller from the stack and makes the new top of the stack the active view controller. If the view controller at the top of the stack is the root view controller, this method does nothing. In other words, you cannot pop the last item on the stack."
    Inos ist ein Gott aus Gothic, dem Spiel.
  • Inos schrieb:

    Nein.
    "This method removes the top view controller from the stack and makes the new top of the stack the active view controller. If the view controller at the top of the stack is the root view controller, this method does nothing. In other words, you cannot pop the last item on the stack."

    Ah, ok. Das wusste ich nicht. Ich würde es trotzdem nicht so machen, denn wenn man nur eine Ebene tiefer navigiert ist, geht das schief.

    Michael
  • Michael schrieb:

    Inos schrieb:

    Nein.
    "This method removes the top view controller from the stack and makes the new top of the stack the active view controller. If the view controller at the top of the stack is the root view controller, this method does nothing. In other words, you cannot pop the last item on the stack."

    Ah, ok. Das wusste ich nicht. Ich würde es trotzdem nicht so machen, denn wenn man nur eine Ebene tiefer navigiert ist, geht das schief.

    Michael

    Michael schrieb:

    Inos schrieb:

    Nein.
    "This method removes the top view controller from the stack and makes the new top of the stack the active view controller. If the view controller at the top of the stack is the root view controller, this method does nothing. In other words, you cannot pop the last item on the stack."

    Ah, ok. Das wusste ich nicht. Ich würde es trotzdem nicht so machen, denn wenn man nur eine Ebene tiefer navigiert ist, geht das schief.

    Michael

    Hast recht, ich weiss ja nicht was er genau da macht.
    Inos ist ein Gott aus Gothic, dem Spiel.
  • Quellcode

    1. #import <UIKit/UIKit.h>
    2. #import "NeurController.h"
    3. @interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
    4. NeurController* neuerController;
    5. IBOutlet UITableView* tableViewHier;
    6. }
    7. @property (nonatomic,retain) NeurController* neuerController;
    8. @property (nonatomic,retain) IBOutlet UITableView* tableViewHier;
    9. @end
    Alles anzeigen


    Quellcode

    1. #import "FirstViewController.h"
    2. @implementation FirstViewController
    3. @synthesize neuerController, tableViewHier;
    4. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    5. - (void)viewDidLoad {
    6. [super viewDidLoad];
    7. }
    8. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    9. return 1;
    10. }
    11. -(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    12. return 50;
    13. }
    14. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    15. static NSString *CellIdentifier = @"Cell";
    16. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    17. if (cell == nil) {
    18. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    19. }
    20. cell.textLabel.text = @"Neu";
    21. cell.detailTextLabel.text = @"neurController";
    22. return cell;
    23. }
    24. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    25. switch (indexPath.row) {
    26. case 0:
    27. neuerController = [[NeurController alloc]initWithNibName:@"NeurController" bundle:nil];
    28. [self.navigationController pushViewController:neuerController animated:YES];
    29. [neuerController release];
    30. break;
    31. case 1:
    32. neuerController = [[NeurController alloc]initWithNibName:@"NeurController" bundle:nil];
    33. [self.navigationController pushViewController:neuerController animated:YES];
    34. [neuerController release];
    35. break;
    36. }
    37. }
    38. - (void)didReceiveMemoryWarning {
    39. // Releases the view if it doesn't have a superview.
    40. [super didReceiveMemoryWarning];
    41. // Release any cached data, images, etc that aren't in use.
    42. }
    43. - (void)viewDidUnload {
    44. // Release any retained subviews of the main view.
    45. // e.g. self.myOutlet = nil;
    46. }
    47. - (void)dealloc {
    48. [super dealloc];
    49. }
    50. @end
    Alles anzeigen
  • Miralem23 schrieb:

    Beachtest Du die Speicherverwaltungsregeln?
    neuerContoller hat @property(nonatomic, retain)

    meinst du das?

    Da gehört eindeutig mehr dazu. Ganz wichtig ist, dass die Methodenaufrufe zur Erhöhung (retain) und zur Erniedrigung (release und autorelease) des Retain-Counts richtig balanciert sind. Bitte lies Dir die Speicherverwaltungsregeln durch, die ich oben als Link angehängt habe.

    Miralem23 schrieb:

    init und alloc sowie release sind dann auch vorhanden.

    Der Init-Methoden haben nichts mit der Speicherverwaltung beziehungsweise mit der Änderung des Retain-Counts zu tun. Das etwas vorhanden ist, heißt aber noch nicht, dass es auch richtig verwendet wird.
    „Meine Komplikation hatte eine Komplikation.“