Can a C++ vector of non-pointer types cause a memory leak in iOS?

跟風遠走 提交于 2019-12-12 01:28:46

问题


Here's the code:

@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.


回答1:


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.



来源:https://stackoverflow.com/questions/10869238/can-a-c-vector-of-non-pointer-types-cause-a-memory-leak-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!