Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...