根据LeakSanitizer,在堆栈上创建纹理会导致内存泄漏。
这是假阳性吗?

我在macOS Mojave上使用sfml版本2.5.1。

main.cpp

#include <SFML/Graphics.hpp>

int main(int, char const**) {
    sf::Texture texture;
}

汇编
clang++ -Wall -Weffc++ -Werror -pedantic -g -fsanitize=address -fno-omit-frame-pointer -lstdc++ -lsfml-graphics main.cpp


➜  test_sfml_texture ASAN_OPTIONS=detect_leaks=1 ./a.out

=================================================================
==48054==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 120 byte(s) in 1 object(s) allocated from:
    #0 0x1005b1287 in wrap_calloc (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x61287)
    #1 0x7fff7690e4eb in class_createInstance (libobjc.A.dylib:x86_64h+0x64eb)
    #2 0x7fff7809cef3 in _os_object_alloc_realized (libdispatch.dylib:x86_64+0x2ef3)
    #3 0x7fff780adb37 in dispatch_source_create (libdispatch.dylib:x86_64+0x13b37)
    #4 0x7fff780b81eb in _dispatch_kq_poll (libdispatch.dylib:x86_64+0x1e1eb)
    #5 0x7fff780b7d3a in _dispatch_kq_drain (libdispatch.dylib:x86_64+0x1dd3a)
    #6 0x7fff780b72b1 in _dispatch_kq_unote_update (libdispatch.dylib:x86_64+0x1d2b1)
    #7 0x7fff780ae720 in _dispatch_source_install (libdispatch.dylib:x86_64+0x14720)
    #8 0x7fff780ae67c in _dispatch_source_activate (libdispatch.dylib:x86_64+0x1467c)
    #9 0x7fff780a2b9a in _dispatch_lane_resume_activate (libdispatch.dylib:x86_64+0x8b9a)
    #10 0x7fff713176b8 in (anonymous namespace)::RunElsewhere::instance() (SkyLight:x86_64+0x1d6b8)
    #11 0x7fff71456f59 in __SLSInitialize_block_invoke (SkyLight:x86_64+0x15cf59)
    #12 0x7fff7809d63c in _dispatch_client_callout (libdispatch.dylib:x86_64+0x363c)
    #13 0x7fff7809ed4b in _dispatch_once_callout (libdispatch.dylib:x86_64+0x4d4b)
    #14 0x7fff7130802e in CGS_CHECK_INIT (SkyLight:x86_64+0xe02e)
    #15 0x7fff714ca6a0 in SLSMainDisplayID (SkyLight:x86_64+0x1d06a0)
    #16 0x101288126 in sf::priv::VideoModeImpl::getDesktopMode() (libsfml-window.2.5.dylib:x86_64+0x1a126)
    #17 0x101282409 in sf::priv::SFContext::SFContext(sf::priv::SFContext*) (libsfml-window.2.5.dylib:x86_64+0x14409)
    #18 0x101273932 in sf::priv::GlContext::initResource() (libsfml-window.2.5.dylib:x86_64+0x5932)
    #19 0x10050e482 in sf::Texture::Texture() (libsfml-graphics.2.5.dylib:x86_64+0x3e482)
    #20 0x1004cce70 in main main.cpp:4
    #21 0x7fff780ea3d4 in start (libdyld.dylib:x86_64+0x163d4)

系统
➜  test_sfml_texture  which clang
/usr/local/opt/llvm@8/bin/clang
➜  test_sfml_texture clang++ --version
clang version 8.0.1 (tags/RELEASE_801/final)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm@8/bin

最佳答案

最好在official forum上进行报告,因为大多数维护人员都可以告诉您这是实际的泄漏还是假的阳性。

the source,我真的看不到有什么泄漏,似乎macOS本身会泄漏一些东西。我只能隐约想到的是,CGMainDisplayID返回的值也需要再次释放。

注意:如果您使用新的C++标准,尤其是智能指针,并且避免进行手动内存管理,则通常不需要定期跟踪内存泄漏,因为它们不再发生(例如,一旦智能指针用完,内存就会自动释放)范围)。

关于c++ - 堆栈分配的sf::Texture导致 “LeakSanitizer: detected memory leaks”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58500146/

10-09 20:57