Using Leaks (the valgrind equivalent) on Mac (Shown on M1) to detect memory leaks

  Рет қаралды 23,719

Mike Shah

Mike Shah

Күн бұрын

Пікірлер: 91
@gulizarilgen3229
@gulizarilgen3229 2 ай бұрын
I've worked 3 hours to find an alternative to valgrind in mac and this video literally made my day thanks
@MikeShah
@MikeShah 2 ай бұрын
Cheers, happy to hear that!
@SubramaniyamKMV
@SubramaniyamKMV 10 ай бұрын
Thanks for this Video. Nice one. Since too many issues in installation of valgrind on MAC M1(ARM)
@MikeShah
@MikeShah 10 ай бұрын
Cheers!
@rongqirichiewang1511
@rongqirichiewang1511 27 күн бұрын
Actually helped me to find out places I forgot to free, tho I didn't manage to get the line numbers
@MikeShah
@MikeShah 26 күн бұрын
Cheers!
@ayodd
@ayodd 4 ай бұрын
THANK YOU!! My class is supposed to use Valgrind but I can't get it to work on my Mac. Is there anything Valgrind does that this doesn't?
@MikeShah
@MikeShah 4 ай бұрын
Generally speaking, both will gives you memory reports. Historically valgrind is seen as a more 'heavy weight' tool that provides more information versus leaks. Both are still actively developed as I understand though, so there may be other tradeoffs or tools incorporated in both frameworks to provide different bug profiles.
@brunojacob7504
@brunojacob7504 2 жыл бұрын
Cool thank you for this video, It's been a while that I've been looking for an alternative to valgrind on mac
@MikeShah
@MikeShah 2 жыл бұрын
Cheers, you are most welcome!
@vkatasonov
@vkatasonov 6 ай бұрын
Does it work for you? Because I have constant segfault trying to access to leaks -atExit-
@Dream-hz3ti
@Dream-hz3ti 2 жыл бұрын
Idk why after export MallocStackLogging I have warning, for example: -> export MallocStackLogging=1 -> ls . ls(5316) MallocStackLogging: could not tag MSL-related memory as no_footprint, so those pages will be included in process footprint - (null) ls(5316) MallocStackLogging: stack logs being written to /tmp/stack-logs.5316.104788000.ls.1J0PCm.index ls(5316) MallocStackLogging: recording malloc and VM allocation stacks to disk using standard recorder ls(5316) MallocStackLogging: stack logs deleted from /tmp/stack-logs.5316.104788000.ls.1J0PCm.index
@Hili7672hdh
@Hili7672hdh 2 жыл бұрын
Me too ! I did not find a solution in the internet!
@jean-baptistecrismer9474
@jean-baptistecrismer9474 Жыл бұрын
Does someone have a solution for this ??
@damondouglas
@damondouglas Жыл бұрын
Thank you for this helpful video! Is it correct that there is no library to apply assert like methods to detect leaks? Essentially there are only terminal based tools? The reason I ask is thinking about this in the context of a CI/CD automation, would someone need to setup the command line utility to run and check the code base or could it just be one of the series of tests?
@MikeShah
@MikeShah Жыл бұрын
I suspect folks could use leaks in a CI/CD system, and the terminal tools make that a bit easier. Something folks might do is actually use a technique known as interpositioning to overload calls to malloc, or in C++ overload the 'new' operator in a test build to also do logging. I'll have q video eventually on this 🙂
@maurasweeney2604
@maurasweeney2604 Жыл бұрын
Thank you for the video! I'm having a hard time finding exactly where the leaks are in my code because there aren't any lines mentioned in the leak report, and in my class we just started using cmake files so I'm not sure how to run with debug symbols. Is there a way to use debug symbols with cmake, or another way to show the line numbers?
@MikeShah
@MikeShah Жыл бұрын
CMake should allow for debug builds which contain debugging symbols. Otherwise can add arguments with-g I believe in cmake to get debug
@johannsebastianbach3411
@johannsebastianbach3411 2 ай бұрын
not to be pedantic, i want to share this for new programmers: you might not always want to/care enough to free. most unix cmd utilities don't free resources unless it is necessary logically, due to the assumption that the utility will run and exit really quickly. if you have a long running program, however, obviously you should free resources
@MikeShah
@MikeShah 2 ай бұрын
Indeed, many applications only allocate (or just a specific 'bump allocator' that is never freed). :) As you say, knowing your problem, scope, and lifetime/maintenance of the program might make it unnecessary to free.
@paulocoutinhox
@paulocoutinhox 2 жыл бұрын
TOP! Im using macOS Monterey but the leaks tool show everything but not the source code with the leak. Do you have any tips (leaks Report Version: 3.0)?
@MikeShah
@MikeShah 2 жыл бұрын
I'm assuming you've compiled with debug symbols?
@אוריןלוי-ב9ה
@אוריןלוי-ב9ה 2 жыл бұрын
Very helpful, thanks! can you explain more about "export MallocStackLogging=1 "?
@MikeShah
@MikeShah 2 жыл бұрын
Enabling the feature essentially logs additional information every time malloc is called (specifically the state of the call stack) developer.apple.com/library/archive/documentation/Performance/Conceptual/ManagingMemory/Articles/MallocDebug.html
@dwolrdcojp
@dwolrdcojp 2 жыл бұрын
If anyone needs this to turn off the MallocStackLogging in the terminal... set MallocStackLogging=0 doesn't turn it off. "unset MallocStackLogging"
@MikeShah
@MikeShah 2 жыл бұрын
Perfect!
@roguechlnchllla6564
@roguechlnchllla6564 Жыл бұрын
Are there any C++ IDEs free for students that have Leaks support? I've been having trouble finding any IDEs for later MacBook versions that support any sort of memory leak detection.
@MikeShah
@MikeShah Жыл бұрын
I haven't used xcode in some time, but I believe that has a memory analyzer (maybe using leaks) integrated
@TheBloodlessOne
@TheBloodlessOne 2 жыл бұрын
Thanks for this video, this is really helpful! I was wondering if you've encountered an issue that says a process isn't debuggable because "for security, cannot load non-system library"?
@MikeShah
@MikeShah 2 жыл бұрын
Hmm, maybe need to open the process once or run 'chmod u+x ./prog' before hand to give permissions? You can try running as 'sudo' if you are admin on the machine as well. Will have to look into this more.
@TheBloodlessOne
@TheBloodlessOne 2 жыл бұрын
@@MikeShah Thanks for the quick response! I tried using sudo and gave the executable file full permissions but was still getting the same results. The full message is the following: Can't examine target process's malloc zone asan_0x103979378, so memory analysis will be incomplete or incorrect. Reason: for security, cannot load non-system library (editing out file path)/libclang_rt.asan_osx_dynamic.dylib Process is not debuggable. Due to security restrictions, leaks can only show or save contents of readonly memory of restricted processes. I've also sent a question to Apple's developer support, I'll let you know if they're able to sort it out!
@TheBloodlessOne
@TheBloodlessOne 2 жыл бұрын
​@@MikeShah I think I figured it out! I'll post my findings here in case anyone else runs into the same issue. I had compiled my C executable with the address sanitizer, which is what prevented leaks from accessing the memory and why the error message mentioned the asan dylib. When I compile the C code without the address sanitizer enabled, the leaks output works like it does for you in the demonstration. Thanks again for your help!
@MikeShah
@MikeShah 2 жыл бұрын
@@TheBloodlessOne awesome, nice fix!
@TheBloodlessOne
@TheBloodlessOne Жыл бұрын
@@MikeShah Sorry to resurrect this old thread! But I was using leaks on code that was experiencing a segmentation fault, and noticed something strange that the segmentation fault message wasn't displaying when leaks was enabled. Is this something you've ever run into, or do you know if there's a way to stop leaks from suppressing those kinds of messages? Thanks for your help!
@echohuang3158
@echohuang3158 2 жыл бұрын
Very clear explaination!
@MikeShah
@MikeShah 2 жыл бұрын
Cheers!
@StandardLoop
@StandardLoop 5 ай бұрын
How to handle if my excutable uses a dynamic library?
@MikeShah
@MikeShah 5 ай бұрын
Good question -- I would assume leaks hooks into various 'loadlibrary/dlload' functions to intercept calls to malloc on those libraries -- but I'm not sure. I'll have to cook something up to see if we can detect a leak
@StandardLoop
@StandardLoop 5 ай бұрын
I have a util dynamic C library I made myself and I have a different source code repo that uses it. When I run in the repo that depends on the util dynamic library I get this leaks - -atExit -- ./$(EXECUTABLE_NAME) dyld[45586]: Library not loaded: libstandardloop-util.dylib It cannot locate my util dynamic lib. I have DYLD_LIBRARY_PATH set and DYLD_FALLBACK_LIBRARY_PATH set aswell. I have tried to set it before running the leaks command as well.
@StandardLoop
@StandardLoop 5 ай бұрын
Running the excutable directly is totattly functional, but running the executable with leaks causes issues. I used man leaks but couldn't really see a flag to add to assist. Let me know if you find anything.
@anderslagerqvist2642
@anderslagerqvist2642 Жыл бұрын
Is there a way to locate invalid reads too? (e.g. Reading freed memory).
@marcello_s
@marcello_s 2 жыл бұрын
HI Mike ! Thanks for your video. I tried to do the exact same thing as you mentioned in your video with a file. It works but i have a problem. there aren't any lines mentioned for the leaks so I can't check in my code where exactly i need to look at..
@MikeShah
@MikeShah 2 жыл бұрын
Hmm, I'm assuming you compiled with debug symbols as well? You also are unlikely to get line numbers from external libraries as well (as they were likely not compiled with debug)
@marcello_s
@marcello_s 2 жыл бұрын
@@MikeShah hi! I’m learning to program at the moment with the class CS50, I use their libraries. But I don’t use their VE, I use their library locally on my mac. To be able to compile my program that use their libraries, I created a makefile file that I put in the same folder as my C file in order to compile it. It enables me to compile my program by entering into the terminal make name_of_file The content of my makefile is: LDLIBS += -lcs50 CC := clang
@marcello_s
@marcello_s 2 жыл бұрын
@@MikeShah basically I get the exact same thing as you, but where the leak is mentionned, Everything is the same as you, start etc, but there is just not any line mentioned .. ahaha
@MikeShah
@MikeShah 2 жыл бұрын
@@marcello_s Ah yes, somewhere look to see if there is a '-g' that would indicate a 'debug build' The convention is to use something like: $(ARGS) as a variable and add in '-g'. You could otherwise try: make run ARGS="-g"
@marcello_s
@marcello_s 2 жыл бұрын
@@MikeShah i don’t think I get it. I’m sorry I’m new to programming. I really don’t know what I should do right now to fix this :/
@alexreade6310
@alexreade6310 2 жыл бұрын
how do you turn off the stack logging afterwards?
@MikeShah
@MikeShah 2 жыл бұрын
Try: export MallocStackLogging=0 or 'unset MallocStackLogging'
@adampaul7905
@adampaul7905 2 жыл бұрын
How to installe it ? Homebrew does not have a « leaks » package
@MikeShah
@MikeShah 2 жыл бұрын
Check /usr/bin to see if it i on your mac already (might be installed alongside xcode). Otherwise you can try 'brew install leaks'
@emielia.
@emielia. Жыл бұрын
thank you for making this video! it was very helpful ^^
@MikeShah
@MikeShah 11 ай бұрын
You are most welcome!
@100vivasvan
@100vivasvan 3 ай бұрын
Thanks! absolute saver
@MikeShah
@MikeShah 3 ай бұрын
Cheers!
@jean-baptistecrismer9474
@jean-baptistecrismer9474 Жыл бұрын
Does someone have a solution for this ??? export MallocStackLogging=1 env(93734) MallocStackLogging: could not tag MSL-related memory as no_footprint, so those pages will be included in process footprint - (null) env(93734) MallocStackLogging: stack logs being written to /tmp/stack-logs.93734.104660000.env.Fnog2c.index env(93734) MallocStackLogging: recording malloc and VM allocation stacks to disk using standard recorder bash(93734) MallocStackLogging: could not tag MSL-related memory as no_footprint, so those pages will be included in process footprint - (null) bash(93734) MallocStackLogging: stack logs being written to /tmp/stack-logs.93734.1006f8000.bash.gLUjp0.index bash(93734) MallocStackLogging: recording malloc and VM allocation stacks to disk using standard recorder bash(93734) MallocStackLogging: stack logs deleted from /tmp/stack-logs.93734.104660000.env.Fnog2c.index bash(93736) MallocStackLogging: stack logs deleted from /tmp/stack-logs.93734.1006f8000.bash.gLUjp0.index head(93737) MallocStackLogging: could not tag MSL-related memory as no_footprint, so those pages will be included in process footprint - (null)
@ahmetuzun2798
@ahmetuzun2798 2 жыл бұрын
when I alloc in static variable in c without free the leaks tool don't show me the leak ....
@MikeShah
@MikeShah 2 жыл бұрын
Interesting, so if you have: static int* p = NULL; Then do something like p = malloc(sizeof(int)*10) leaks does not catch the memory leak?
@ahmetuzun2798
@ahmetuzun2798 2 жыл бұрын
@@MikeShah yep
@ahmetuzun2798
@ahmetuzun2798 2 жыл бұрын
static int *o = NULL; o = malloc(300); 0 leaks for 0 total leaked bytes
@MikeShah
@MikeShah 2 жыл бұрын
@@ahmetuzun2798 Try: #include int main(){ for(int i=0; i < 10; i++){ static int* o = NULL; o = malloc(300); } return 0; } I wonder if the memory in the heap is already being freed when exiting main. Perhaps leaks is a bit more conservative and only providing a report on memory it can prove that has been left out of scope. (Static variables themselves probably aren't 'freed' until the very end of a process, and in a sense, a static variable will always exist in the binary, so it will always hold onto at least 1 reference to the memory, thus why you'll only see 9 of the 10 leaks reported)
@ahmetuzun2798
@ahmetuzun2798 2 жыл бұрын
@@MikeShah okkkkk thxxx
@oscarnicolasm
@oscarnicolasm Жыл бұрын
Amazing, thank you!
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@natnaelmanyazewal8400
@natnaelmanyazewal8400 2 жыл бұрын
very helpful 👏
@MikeShah
@MikeShah 2 жыл бұрын
Awesome!
@krisinigor8214
@krisinigor8214 2 жыл бұрын
very helpful! thank you!
@MikeShah
@MikeShah 2 жыл бұрын
Cheers!
@XavierCatalaBalta
@XavierCatalaBalta Жыл бұрын
Thanks a lot! I'm combining your instructions with | grep "Call stack" to get rid off the rest of information. The only thing is that using 'C' I don't get the exact line, but... it's a great leap anyway!
@MikeShah
@MikeShah Жыл бұрын
Cheers, glad to hear it! Hmm, maybe using -g3 will provide more debug information to valgrind?
@XavierCatalaBalta
@XavierCatalaBalta Жыл бұрын
@@MikeShah Thanks for your quick answer! The issue is I cannot use valgrind due to restrictions of installation (I'm in the 42 school of Barcelona). Also when my program is compiled with other auxiliar programs (used as sources of functions) I don't get the info about what program caused the leak. I'm exploring other options of the leaks command.
@MikeShah
@MikeShah Жыл бұрын
@@XavierCatalaBaltaAh, got it!
@batuhanipci
@batuhanipci 2 жыл бұрын
Nice video!
@MikeShah
@MikeShah 2 жыл бұрын
Thank you!
@jirkadolezal8127
@jirkadolezal8127 Жыл бұрын
👍👍👍👍👍👍👍👍👍👍👍👍
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@Local_Nerd
@Local_Nerd Жыл бұрын
Thanks
@MikeShah
@MikeShah Жыл бұрын
Cheers!
NEVER lose dotfiles again with GNU Stow
14:33
typecraft
Рет қаралды 66 М.
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 36 МЛН
find memory errors quickly. (-fsanitize, addresssanitizer)
9:44
Jacob Sorber
Рет қаралды 19 М.
Where Does Bad Code Come From?
42:21
Molly Rocket
Рет қаралды 208 М.
Выделение и освобождение динамической памяти в Си
27:39
Git Tutorial For Dummies
19:25
Nick White
Рет қаралды 1,3 МЛН
Trump announced the end date of the war / Emergency plane landing
14:05
Why I Switched to Mac (as a Linux user)
22:53
Wolfgang's Channel
Рет қаралды 626 М.
An unexpected memory leak in JS
13:56
Theo - t3․gg
Рет қаралды 63 М.
Провальные провалы
29:25
GreenGrass
Рет қаралды 238 М.
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН