Can a C++ vector of non-pointer types cause a memo

2019-09-08 06:12发布

问题:

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.