English 中文(简体)
优良方法
原标题:Obj-C Memory Management Setter Method

I am new to objective-c and I ve downloaded the code from here.
Ran Chapter 10, 10.01 CarPartsInit xcode project file.

我不清楚的是,对一套方法的记忆管理是否如此。

- (void) setEngine: (Engine *) newEngine
{
    [newEngine retain] 
    [engine release];
    engine = newEngine;
}

应在项目结束时将发动机编号retainCount成0。

I ran some NSLog and when the program ends... the retainCount for engine was at 1... Should it go to 0 and be freed? Or is this a memory leak?

问题回答

不要担心保留罪状。 你们甚至看着他们。 他们只是混淆不清,其实是私人执行细节。 案例: 保留的任何物体都不算零。 为什么? 因为没有点。 保留0的物体是可转让的物体,而且发送电文处理已转让物体(可能报告1的旧价值,或报告可能完全不同,或可能只是破坏你的方案)。 只要你在

没有必要在方案退出之前释放所有物品。 运行系统自动收回使用的所有方案。

通常不担心方案期间存在的物体。 如果你制造某种东西,那么你真的只有记忆泄露,但在公布之前就失去了参考。

注:如果方案在终止之前对每个物体进行清理,则你可以归入声称某种“错误”的方案。 你可以忽视这些人。

任何案件(或分配)都应以交易方式释放。

-(void) dealloc {
  [engine release]; // no need to set to nil in dealloc
  [super dealloc];
}

让我们研究应该做些什么。

  1. someone pass in a newEngine, and you wanna use it. So you need to "retain" it.
  2. you car maybe already have an engine. So you release it. (Note, send release message to null does nothing)
  3. then you assign passed-in argument "newEngine" to the car s property "engine". Done.

如果你看上去大麻是如何保留增加和减少的,则用超标准方法“释放”印刷一些东西。 但是,要叫我释放。

While you have initialized your class, the engines memory count will increase with one (saying you do initialize it).
In your setter, the new value needs to be retained and the old value released (retain counter increases with new value, then counter decreases to get rid of the old value). The retain-counter is therefor still 1.

在结束该方案时,你还需要释放在你经营中的发动机,以确保不会有任何记忆故障。

A great way to watch for memory leaks is using the "Leaks"-tool in xcode:
run -> Start with Performance Tool -> Leaks

Other methods is to use the "Build and analyze" tool to find potential memory leaks:
Build > Build and Analyze

我希望我能这样做,我的客观目标是一线虚伪。





相关问题
Windows Mobile 6 Emulator change storage?

How do i change the size of the Windows Mobile 6 Emulator. Its fixed at 32mb. I read this post: Increasing Windows Mobile 5 Emulator Storage But it only helps for the 5.0 version. Isnt there any way ...

CUDA Memory Allocation accessible for both host and device

I m trying to figure out a way to allocate a block of memory that is accessible by both the host (CPU) and device (GPU). Other than using cudaHostAlloc() function to allocate page-locked memory that ...

RAM memory reallocation - Windows and Linux

I am working on a project involving optimizing energy consumption within a system. Part of that project consists in allocating RAM memory based on locality, that is allocating memory segments for a ...

Should I send retain or autorelease before returning objects?

I thought I was doing the right thing here but I get several warnings from the Build and Analyze so now I m not so sure. My assumption is (a) that an object I get from a function (dateFromComponents: ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

doubts regarding Memory management in .net

I m learning about Memory management in C# from the book "Professional C#" The presence of the garbage collector means that you will usually not worry about objects that you no longer need; ...

Objective-C returning alloc d memory in a function == bad?

This is on the iPhone. So what if I have a function like - (SomeObject*)buildObject; Do I need to pass in a variable that I have already alloc d outside like - (void)assignObject(SomeObject** out);...

热门标签