简体   繁体   English

iPhone:在 object 上调用发布:它会立即发布吗?

[英]iPhone: calling release on an object: Does it get released right away?

I have a shopping cart in my app.我的应用程序中有一个购物车。 I also have a UIBarButton called "Empty Cart".我还有一个名为“Empty Cart”的 UIBarButton。 Im implementing functionality for this button.我正在实现此按钮的功能。 However, Im running into some problems.但是,我遇到了一些问题。 My Cart is a Singleton Data Object.我的购物车是 Singleton 数据 Object。 What I want to do is when the user presses "Empty Cart", all the objects and variable values in the cart should be released and/or made 0.我想要做的是当用户按下“空购物车”时,购物车中的所有对象和变量值都应该被释放和/或设为 0。

I have an "emptyCart" method implemented in my ViewController which calles an "emptyCart" method of the CartSingleton in turn.我在 ViewController 中实现了一个“emptyCart”方法,它依次调用 CartSingleton 的“emptyCart”方法。 My problem is as shown below the release calls to the various objects are not releasing the objects, because when I print the contents of the Cart after running "empty cart", i can still see all the items in the Cart.我的问题如下所示,对各种对象的释放调用没有释放对象,因为当我在运行“空购物车”后打印购物车的内容时,我仍然可以看到购物车中的所有项目。 Does the release message not take effect right away?发布消息不是马上生效吗? How can I instantly release all the objects in my Cart then?那我怎样才能立即释放我购物车中的所有对象呢?

Code- "Empty Cart" in Singleton:代码 - Singleton 中的“空购物车”:

-(void)emptyCart

{ {

if (self.selectedLocation != NULL)
   {
    [self.selectedLocation release];
    self.locationSelected = false;
   }

if (self.foodItemsArray != NULL)
   {
    for (int i = 0; i < [self.foodItemsArray count]; i++)
       {
        FoodItem *temp = [self.foodItemsArray objectAtIndex:i];
        if (temp.sidesArray != NULL)
           {
            [temp.sidesArray release];
           }
       }
[self.foodItemsArray release];  
   }

if (self.drinkItemsArray != NULL)
   {
    [self.drinkItemsArray release];
   }

if (self.otherItemsArray != NULL)
   {
    [self.otherItemsArray release];
   }

if (self.customerInfo != NULL)
   {
    [self.customerInfo release];
   }

self.numFoodItems = 0;
//self.totalTaxPercent = 0;
self.foodItemsTotalCost = 0;
self.drinkItemsTotalCost = 0;
self.otherItemTotalCost = 0;
self.totalCostOfAllItems = 0;
self.totalTaxesAmount = 0;
self.totalChargesWithTaxes = 0;
self.gratuity = 0;
self.miscCharges = 0;

Releasing releases right away -- meaning, it reduces the retain count, and if the retain count is 0, then the object is deallocated.立即释放 - 意思是,它减少了保留计数,如果保留计数为 0,则 object 被释放。

Your problem is that you are holding onto the objects -- not setting the variables to nil after you are done with them.您的问题是您持有对象 - 完成后没有将变量设置为 nil 。

I am also worried that you are releasing too many times我也担心你释放的次数太多

  1. Arrays release their elements when they are released Arrays 释放它们的元素时
  2. retained @properties will release the object they are set to if you set them to nil - you should be clearing them like so:如果您将它们设置为 nil,则保留的 @properties 将释放它们设置为的 object - 您应该像这样清除它们:

    self.customerInfo = nil; self.customerInfo = nil;

So, watch out.所以小心。 It looks like right now you are accessing released objects -- which will crash eventually.看起来现在您正在访问已发布的对象——最终会崩溃。 Over releasing will also cause problems.过度释放也会导致问题。

To try to find out these issues试图找出这些问题

  1. Use Build and Analyze and fix the errors it reports使用 Build and Analyze 并修复它报告的错误
  2. Turn on Zombies and see if you are accessing released objects打开 Zombies 并查看您是否正在访问已发布的对象

    http://www.loufranco.com/blog/files/debugging-memory-iphone.html http://www.loufranco.com/blog/files/debugging-memory-iphone.html

release does nothing more than decrement the retain count on an object. release只是减少 object 上的保留计数。 The runtime, when it observes that the retain reaches zero, will then call the dealloc method of your object.运行时,当它观察到保留达到零时,将调用 object 的dealloc方法。

Therefore, if you still have objects in there, this means that other objects have placed retain on the object (increasing its retain count) meaning that they will stick around.因此,如果您仍然有对象,这意味着其他对象已将保留放置在 object 上(增加其保留计数),这意味着它们将保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM