如果函数返回UnsafeMutablePointer,则我们有责任销毁并取消分配吗? [英] If a function returns an UnsafeMutablePointer is it our responsibility to destroy and dealloc?

查看:143
本文介绍了如果函数返回UnsafeMutablePointer,则我们有责任销毁并取消分配吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我要编写这段代码:

For example if I were to write this code:

var t = time_t()
time(&t)
let x = localtime(&t) // returns UnsafeMutablePointer<tm>

println("\(x.memory.tm_hour): \(x.memory.tm_min): \(x.memory.tm_sec)")

...是否还需要执行以下操作?

...would it also be necessary to also do the following?

x.destroy()
x.dealloc(1)

还是我们没有分配内存,因此不需要关闭它?

Or did we not allocate the memory and so therefore don't need to dismiss it?

如果我们想象一个函数返回 UnsafeMutablePointer

If we imagine a function that returns an UnsafeMutablePointer:

func point() -> UnsafeMutablePointer<String> {
    let a = UnsafeMutablePointer<String>.alloc(1)
    a.initialize("Hello, world!")
    return a
}

调用此函数将导致指向一个对象的指针,除非我们自己进行肮脏的工作,否则该对象将永远不会被破坏。

Calling this function would result in a pointer to an object that will never be destroyed unless we do the dirty work ourselves.

我在这里问的问题:是从 localtime()收到的指针吗?

模拟器和游乐场都使我们能够向返回的指针发送一个 dealloc(1)调用,但是我们应该这样做还是稍后会通过其他方法对返回的指针进行释放吗?

The question I'm asking here: Is a pointer received from a localtime() call any different?
The simulator and the playground both enable us to send one dealloc(1) call to the returned pointer, but should we be doing this or is the deallocation going to happen for a returned pointer by some other method at a later point?

此刻,我对我们确实需要销毁的假设有误

At the moment I'm erring towards the assumption that we do need to destroy and dealloc.

最后一个假设是错误的。我不需要释放,因为我没有创建对象。

The last assumption was wrong. I don't need to release, because I didn't create object.

我在Apple开发人员论坛上收到了对同一查询的一些答案​​

I received some answers to the same query on the Apple dev forums.


通常,您的问题的答案是肯定的。如果收到指向内存的指针,您将负责在C中进行释放,那么当您从swift调用时,您仍然有责任释放它……[但是]在这种情况下,您不需要执行任何操作。 (JQ)

In general, the answer to your question is yes. If you receive a pointer to memory which you would be responsible for freeing in C, then you are still responsible for freeing it when calling from swift ... [But] in this particular case you need do nothing. (JQ)

例程本身为结果维护静态内存,您无需释放它们。 (如果这样做的话,那可能是一件坏事)……通常,您不知道是否需要释放UnsafePointer指向的内容……这取决于该指针在何处获取其值。 (ST)

the routine itself maintains static memory for the result and you do not need to free them. (it would probably be a "bad thing" if you did) ... In general, you cannot know if you need to free up something pointed to by an UnsafePointer.... it depends on where that pointer obtains its value. (ST)

UnsafePointer的dealloc()与free()不兼容。将alloc()与dealloc()以及malloc和co配对。与free()。如前所述,您要调用的函数应该告诉您是否是释放结果的响应……只有在指针引用的内存中包含非平凡的内容时,destroy()才是必需的*强引用或Swift结构或枚举。通常,如果它来自C,则可能不需要destroy()。 (实际上,您可能不应该销毁它,因为它不是由Swift初始化的。)... *非无关紧要的内容不是Swift的正式术语。我以类似于普通可复制的C ++概念(尽管不一定是普通)来使用它。 (STE)

UnsafePointer's dealloc() is not compatible with free(). Pair alloc() with dealloc() and malloc and co. with free(). As pointed out previously, the function you're calling should tell you whether it's your response to free the result ... destroy() is only necessary if you have non-trivial content* in the memory referred to by the pointer, such as a strong reference or a Swift struct or enum. In general, if it came from C, you probably don't need to destroy() it. (In fact, you probably shouldn't destroy() it, because it wasn't initialized by Swift.) ... * "non-trivial content" is not an official Swift term. I'm using it by analogy with the C++ notion of "trivially copyable" (though not necessarily "trivial"). (STE)






最终更新:



我现在写了一篇博客文章,概述了我的发现和假设,这些发现和假设涉及从StackOverflow,Apple Dev Forums,Twitter和Apple有关分配和释放内存的旧文档(ARC之前的版本)获取不安全指针的信息。 。请参见此处

推荐答案

从Swift库 UnsafeMutablePointer< T>


指向 T 类型的对象的指针。这种类型不提供自动的
内存管理,因此用户必须小心分配
和可用内存

A pointer to an object of type T. This type provides no automated memory management, and therefore the user must take care to allocate and free memory appropriately.

指针可以处于以下状态之一:

The pointer can be in one of the following states:


  • 未分配内存(例如,指针为null或先前已释放
    的内存);

  • 已分配内存,但值尚未初始化;


struct UnsafeMutablePointer<T> : RandomAccessIndexType, Hashable, NilLiteralConvertible { /**/}

这篇关于如果函数返回UnsafeMutablePointer,则我们有责任销毁并取消分配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆