On OSX, Valgrind reports this memory leak, where did it come from?

In OSX, Valgrind reports this memory leak, where did it come from? The c code is compiled with g ++ as C ++ code (I do this to overload functions).

  == 13088 == 18 bytes in 1 blocks are definitely lost in loss record 82 of 264
 == 13088 == at 0x1F25DC: malloc_zone_malloc (vg_replace_malloc.c: 267)
 == 13088 == by 0xA1AEDA: malloc_set_zone_name (in /usr/lib/system/libsystem_c.dylib)
 == 13088 == by 0xA1B4A7: _malloc_initialize (in /usr/lib/system/libsystem_c.dylib)
 == 13088 == by 0xA1B5DD: malloc_good_size (in /usr/lib/system/libsystem_c.dylib)
 == 13088 == by 0x4EFA6E: __CFStringChangeSizeMultiple (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
 == 13088 == by 0x4F3900: CFStringAppend (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
 == 13088 == by 0x506F91: _convertToURLRepresentation (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
 == 13088 == by 0x60F963: _CFURLInit (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
 == 13088 == by 0x4FF268: CFURLCreateWithFileSystemPathRelativeToBase (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
 == 13088 == by 0x4FF8EE: CFURLCreateWithFileSystemPath (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
 == 13088 == by 0x515735: _CFBundleGetMainBundleAlreadyLocked (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
 == 13088 == by 0x515663: CFBundleGetMainBundle (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
 == 13088 == by 0x539533: cacheBundleInfo (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
 == 13088 == by 0x5394B3: _CFAppVersionCheckLessThan (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
 == 13088 == by 0x56C35B: __CFInitialize (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
 == 13088 == by 0x8FE11243: ImageLoaderMachO :: doImageInit (ImageLoader :: LinkContext const &) (in / usr / lib / dyld)
 == 13088 == by 0x8FE10CB3: ImageLoaderMachO :: doInitialization (ImageLoader :: LinkContext const &) (in / usr / lib / dyld)
 == 13088 == by 0x8FE0E21F: ImageLoader :: recursiveInitialization (ImageLoader :: LinkContext const &, unsigned int, ImageLoader :: InitializerTimingList &) (in / usr / lib / dyld)
 == 13088 == by 0x8FE0E1B5: ImageLoader :: recursiveInitialization (ImageLoader :: LinkContext const &, unsigned int, ImageLoader :: InitializerTimingList &) (in / usr / lib / dyld)
 == 13088 == by 0x8FE0F1BF: ImageLoader :: runInitializers (ImageLoader :: LinkContext const &, ImageLoader :: InitializerTimingList &) (in / usr / lib / dyld)
 == 13088 == by 0x8FE03655: dyld :: initializeMainExecutable () (in / usr / lib / dyld)
 == 13088 == by 0x8FE07EF1: dyld :: _ main (macho_header const *, unsigned long, int, char const **, char const **, char const **) (in / usr / lib / dyld)
 == 13088 == by 0x8FE012EE: dyldbootstrap :: start (macho_header const *, int, char const **, long, macho_header const *) (in / usr / lib / dyld)
 == 13088 == by 0x8FE01062: _dyld_start (in / usr / lib / dyld)
 == 13088 == by 0xFFF: ???

EDIT: Also, how can I free this memory?

+4
source share
4 answers

The selection is not completely under your control; freedom is also almost impossible for you. This should be added to the list of known, detected, recorded, but ignored elements (โ€œsuppressedโ€ is jargon).

When I run the program under valgrind 3.7.0 on MacOS X 10.7.2, I get the following message:

==71989== ==71989== HEAP SUMMARY: ==71989== in use at exit: 6,191 bytes in 33 blocks ==71989== total heap usage: 33 allocs, 0 frees, 6,191 bytes allocated ==71989== ==71989== LEAK SUMMARY: ==71989== definitely lost: 0 bytes in 0 blocks ==71989== indirectly lost: 0 bytes in 0 blocks ==71989== possibly lost: 0 bytes in 0 blocks ==71989== still reachable: 6,191 bytes in 33 blocks ==71989== suppressed: 0 bytes in 0 blocks ==71989== Rerun with --leak-check=full to see details of leaked memory ==71989== ==71989== For counts of detected and suppressed errors, rerun with: -v ==71989== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1) 

This is from a program that has no explicit memory allocation. printf() may initiate some distribution, but most of these bytes are allocated in system libraries. --num-callers=N clearly deeper than the normal value for the trace ( --num-callers=N ).

See the manual on how to correctly add a suppression entry, but valgrind --help suggests:

 --num-callers=<number> show <number> callers in stack traces [12] --error-limit=no|yes stop showing new errors if too many? [yes] --error-exitcode=<number> exit code to return if errors found [0=disable] --show-below-main=no|yes continue stack traces below main() [no] --suppressions=<filename> suppress errors described in <filename> --gen-suppressions=no|yes|all print suppressions for errors? [no] 

So you can get valgrind to generate a suppression string, which you can add to the file, which you then use in subsequent runs.

 Extra options read from ~/.valgrindrc, $VALGRIND_OPTS, ./.valgrindrc 
+5
source

It looks more like it in one of the libraries that you use, if so, make sure that you use the library API correctly, but also know that sometimes the library allocates global resources, for example, during initialization, which it is not freed up, until the program exits.

For example, consider this situation:

 void init_lib(){ static char *buf=NULL; if (buf==NULL) { buf = malloc(18); } ..... } 

How can a library free this buffer before exiting? it cannot until the program exits and its memory is restored by the OS.

See Common Errors Here

Edit: technically, in the example above, buf can be free, as suggested by one of the comments below, but many libraries do not bother with this, because buf will be used for the entire duration of the program and memory will be restored when the program exits, so valgrind reports this as a memory leak.

0
source

On OS X, you need to run valgrind with --dsymutil = yes to get more useful information about the leak location

0
source

I am not sure which version of osx you are using. But in the version I run, valgrind reports these errors as ignorant.

0
source

Source: https://habr.com/ru/post/1393425/


All Articles