How to find out if an object is alive or not in Objective c With ARC

Hi in my iPad app. I use the search bar and tableview, after the search is done, the results are displayed in a table. To update the table with new search results, I call the reloadData method. It works fine, but in some situations the application crashes to the [tableviewobject reloaddata] method, and I get this error in the console

error for object 0x95a0300: there was no release of the pointer * set interrupt to malloc_error_break for debugging.

Watching the above error, I realized that the tableviewobject is being deleted from memory. I am sending a message to a freed object. To solve this problem, I allocated memory before passing the reloadData message to tableviewobject as such

tableviewObject=[[UITableView alloc]init]; tableviewObject.delegate=self; tableview.datasource=self; [tableviewObject reloaddata];. 

In a situation, the result data is not updated in the tableview. Now I would like to know how I can find out if an object is alive or freed from memory. if he freed, how to allocate memory for the table view object to reload the search results. Please provide a solution for this if you have any ideas. thanks in advance, please help me.

NOTE: IN MY PROJECT I AM AN ARCHIVE.

+4
source share
3 answers

Are you attracted to the fact that in the third line the name UITableView is mistakenly written?

 tableviewObject=[[UITableView alloc]init]; tableviewObject.delegate=self; tableview.datasource=self; [tableviewObject reloaddata]; 

It should be tableviewObject.datasource = self;

Also, why create a new object every time? You will lose the old! Just run it in the viewDidLoad method for the first and only time

0
source

As a more general approach to solve:

If you use ARC, you need to hold the hold in your objects, or they will be freed for you.
In the above example (suppose you were also intended to assign tableviewObject.datasource), you need some property or member of the class to hold the link to your tableviewObject. For instance:

 @interface MyViewController() //anonymous private properties inside your .m @property(nonatomic,strong) UITableView *tableviewObject; @end @implementation MyViewController @synthesize tableviewObject; // rest of your implementation ... @end 

This way you will work with self.tableviewObject, but this will remain available as long as the UIViewController is alive with this resource.

The general information to keep in mind is that if you do not want the ARC to release what someone should have used (the link or member property is considered "using" ).

In this regard, the answer thalador / user2413007 is also correct, since the output will refer to the table view in the loaded XIB, which basically has the same result.

0
source

Do not create UITableView objects in the code, you lose the connection between the view, if you create such objects, this will not help you create a property like this

 @property(nonatomic,strong)IBOutlet UITableView *tableView; 

This will solve your problem, I think.

-1
source

Source: https://habr.com/ru/post/1469130/


All Articles