为什么以下代码会导致os x el capitan中的内存泄漏?我该如何解决?
注意:它不会导致Linux环境中的内存泄漏。如果我将x
更改为int
,则无内存泄漏。
1 #include <iostream>
2
3 using namespace std;
4
5 int main() {
6 float x;
7 cin >> x;
8 return 0;
9 }
Valgrind结果:
==1727== LEAK SUMMARY:
==1727== definitely lost: 80 bytes in 1 blocks
==1727== indirectly lost: 304 bytes in 8 blocks
==1727== possibly lost: 2,064 bytes in 1 blocks
==1727== still reachable: 4,096 bytes in 1 blocks
==1727== suppressed: 20,247 bytes in 192 blocks
==1727== Rerun with --leak-check=full to see details of leaked memory
Valgrind with --leak-check = full
==1753== HEAP SUMMARY:
==1753== in use at exit: 26,791 bytes in 203 blocks
==1753== total heap usage: 266 allocs, 63 frees, 32,479 bytes allocated
==1753==
==1753== 384 (80 direct, 304 indirect) bytes in 1 blocks are definitely lost in loss record 58 of 71
==1753== at 0x100008EBB: malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==1753== by 0x1002AB8D6: __Balloc_D2A (in /usr/lib/system/libsystem_c.dylib)
==1753== by 0x1002AC21F: __d2b_D2A (in /usr/lib/system/libsystem_c.dylib)
==1753== by 0x1002AEF53: __strtodg (in /usr/lib/system/libsystem_c.dylib)
==1753== by 0x1002B0468: __strtopx (in /usr/lib/system/libsystem_c.dylib)
==1753== by 0x1002F2DB6: strtold_l (in /usr/lib/system/libsystem_c.dylib)
==1753== by 0x100036550: float std::__1::__num_get_float<float>(char const*, char const*, unsigned int&) (in /usr/lib/libc++.1.dylib)
==1753== by 0x10003616C: std::__1::num_get<char, std::__1::istreambuf_iterator<char, std::__1::char_traits<char> > >::do_get(std::__1::istreambuf_iterator<char, std::__1::char_traits<char> >, std::__1::istreambuf_iterator<char, std::__1::char_traits<char> >, std::__1::ios_base&, unsigned int&, float&) const (in /usr/lib/libc++.1.dylib)
==1753== by 0x10001CBBB: std::__1::basic_istream<char, std::__1::char_traits<char> >::operator>>(float&) (in /usr/lib/libc++.1.dylib)
==1753== by 0x100000F6E: main (in ./memory_leak)
==1753==
==1753== 2,064 bytes in 1 blocks are possibly lost in loss record 65 of 71
==1753== at 0x10000917C: malloc_zone_malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==1753== by 0x1005E0EFD: _objc_copyClassNamesForImage (in /usr/lib/libobjc.A.dylib)
==1753== by 0x1005D4182: protocols() (in /usr/lib/libobjc.A.dylib)
==1753== by 0x1005D4093: readClass(objc_class*, bool, bool) (in /usr/lib/libobjc.A.dylib)
==1753== by 0x1005D1C13: gc_init (in /usr/lib/libobjc.A.dylib)
==1753== by 0x1005D924E: objc_initializeClassPair_internal(objc_class*, char const*, objc_class*, objc_class*) (in /usr/lib/libobjc.A.dylib)
==1753== by 0x1005E6132: layout_string_create (in /usr/lib/libobjc.A.dylib)
==1753== by 0x1005D483C: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==1753== by 0x1005D4300: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==1753== by 0x1005D42E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==1753== by 0x1005D42E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==1753== by 0x1005D42E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==1753==
==1753== LEAK SUMMARY:
==1753== definitely lost: 80 bytes in 1 blocks
==1753== indirectly lost: 304 bytes in 8 blocks
==1753== possibly lost: 2,064 bytes in 1 blocks
==1753== still reachable: 4,096 bytes in 1 blocks
==1753== suppressed: 20,247 bytes in 192 blocks
==1753== Reachable blocks (those to which a pointer was found) are not shown.
==1753== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==1753==
==1753== For counts of detected and suppressed errors, rerun with: -v
==1753== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 16 from 16)