iPhone:在对象上调用释放:它会立即释放吗?
我的应用程序中有一个购物车。我还有一个名为“Empty Cart”的 UIBarButton。我正在实现这个按钮的功能。但是,我遇到了一些问题。我的购物车是一个单例数据对象。我想要做的是,当用户按下“清空购物车”时,购物车中的所有对象和变量值都应被释放和/或设为 0。
我在 ViewController 中实现了一个“emptyCart”方法,该方法调用“emptyCart”依次调用 CartSingleton 的方法。我的问题是如下所示,对各种对象的释放调用没有释放对象,因为当我在运行“空购物车”后打印购物车的内容时,我仍然可以看到购物车中的所有项目。发布消息不是立即生效吗?那么如何才能立即释放我购物车中的所有物品呢?
单例中的代码-“空购物车”:
-(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;
I have a shopping cart in my app. I also have a UIBarButton called "Empty Cart". Im implementing functionality for this button. However, Im running into some problems. My Cart is a Singleton Data 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.
I have an "emptyCart" method implemented in my ViewController which calles an "emptyCart" method of the CartSingleton in turn. 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:
-(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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
释放立即释放——这意味着,它减少了保留计数,如果保留计数为 0,则释放该对象。
你的问题是你持有这些对象——在使用完它们之后没有将变量设置为零。
我还担心你释放太多次数
retained @properties 将释放它们设置的对象,如果你将它们设置为 nil - 你应该像这样清除它们:
self.customerInfo = nil;
所以,要小心。看起来你现在正在访问已释放的对象——这最终会崩溃。过度释放也会带来问题。
要尝试找出这些问题,
打开“僵尸”并查看是否正在访问已发布的对象
http://www.loufranco.com/blog/files/ debug-memory-iphone.html
Releasing releases right away -- meaning, it reduces the retain count, and if the retain count is 0, then the object is deallocated.
Your problem is that you are holding onto the objects -- not setting the variables to nil after you are done with them.
I am also worried that you are releasing too many times
retained @properties will release the object they are set to if you set them to nil - you should be clearing them like so:
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
Turn on Zombies and see if you are accessing released objects
http://www.loufranco.com/blog/files/debugging-memory-iphone.html
release
只是减少对象的保留计数。当运行时观察到保留达到零时,它将调用对象的dealloc方法。因此,如果其中仍然有对象,则意味着其他对象已在该对象上放置了保留(增加其保留计数),这意味着它们将保留下来。
release
does nothing more than decrement the retain count on an object. The runtime, when it observes that the retain reaches zero, will then call thedealloc
method of your object.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.