非指针类型的C ++向量会导致iOS中的内存泄漏吗? [英] Can a C++ vector of non-pointer types cause a memory leak in iOS?
问题描述
代码如下:
@interface myClass {
std::vector<float> myVector
}
@end
根据仪器泄漏.这是堆栈跟踪:
It's leaking according to instruments. Here's the stack trace:
1 libstdc++.6.dylib operator new(unsigned long)
2 __gnu_cxx::new_allocator<float>::allocate(unsigned long, void const*)
3 std::_Vector_base<float, std::allocator<float> >::_M_allocate(unsigned long)
我猜我应该在堆上分配向量,但是我仍然不明白为什么会发生这种情况.我也可能无法正确地取消分配该类.
I'm guessing I should be allocating the vector on the heap, but I still don't understand why this occurs. It's also possible I'm failing to dealloc the class properly.
推荐答案
检查是否调用了您的类的dealloc.在这种情况下,这似乎是一种可以称为泄漏的方法.还有一个问题:为什么不使用本机的Objective-C容器?您很需要将浮点数存储在NSNumbers中,但是您将能够对所有实例使用标准的保留/释放内存管理模型.
Check if your class's dealloc is called. It seems to be the one way that can call leak in this case. And just one question: why don't you use native objective-c containers? You well need to store your floats in NSNumbers, but you will be able to use standard retain/release memory management model for all of your instances.
这篇关于非指针类型的C ++向量会导致iOS中的内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!