Skip to content

Commit

Permalink
Added valgrind to container and updated sample and README to show exa…
Browse files Browse the repository at this point in the history
…mple of checking for memory leaks
  • Loading branch information
jophippe committed Jul 17, 2024
1 parent 89c41b4 commit 4def68f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion NativeBlink/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM mcr.microsoft.com/azurespheresdk:latest

RUN apt update && \
apt install -y libncurses5 gdb && \
apt install -y libncurses5 gdb valgrind && \
apt autoremove -y && \
apt clean -y
8 changes: 6 additions & 2 deletions NativeBlink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ cmake_minimum_required(VERSION 3.20)
project(HelloWorld_HighLevelApp C)
include(CTest)

azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
azsphere_configure_tools(TOOLS_REVISION "24.03")
azsphere_configure_api(TARGET_API_SET "17")

add_executable(${PROJECT_NAME} main.c led.c)
target_link_libraries(${PROJECT_NAME} applibs gcc_s c)

if(LEAK)
target_compile_definitions(${PROJECT_NAME} PUBLIC LEAK)
endif()

# TARGET_HARDWARE and TARGET_DEFINITION relate to the hardware definition targeted by this sample.
# When using this sample with other hardware, replace TARGET_HARDWARE with the name of that hardware.
# For example, to target the Avnet MT3620 Starter Kit, use the value "avnet_mt3620_sk".
Expand Down
8 changes: 8 additions & 0 deletions NativeBlink/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "x86-Debug-Leak",
"displayName": "x86-Debug-Leak",
"inherits": "x86-Debug",
"cacheVariables": {
"LEAK": true
}
}
],
"buildPresets": [
Expand Down
26 changes: 26 additions & 0 deletions NativeBlink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ When debugging, open the Terminal Window (Ctrl + `) to see program output, which

![Screenshot that shows how to run the CTests.](.media/run_ctests.png)

### Check for memory leaks

1. Use the Toolbar to select the configure preset "x86-Debug-Leak", and wait for CMake to finish generating the cache.

(Alternatively, open the Command Palette, run the command "CMake: Select Configure Preset", select the "x86-Debug-Leak" Preset.)

2. Use the Toolbar to build.

(Alternatively, open the Command Palette and run the command "CMake: Build" to build.)

3. Open the Terminal Window (Ctrl + `) and run the following command to start running Valgrind on the app:
```
valgrind --leak-check=full --show-leak-kinds=definite out/x86-Debug-Leak/HelloWorld_HighLevelApp
```

4. After the app runs for a while, stop the app (Ctrl + C) and observe the output from Valgrind. Note that Valgrind correctly identifies memory that is definitely lost in main():
```
HEAP SUMMARY:
in use at exit: 146,511 bytes in 7 blocks
total heap usage: 7 allocs, 0 frees, 146,511 bytes allocated
8 bytes in 2 blocks are definitely lost in loss record 2 of 6
at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x10931F: main (main.c:66)
```

## Project expectations

### Expected support for the code
Expand Down
6 changes: 6 additions & 0 deletions NativeBlink/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// - gpio (digital input for button)
// - log (displays messages in the Device Output window during debugging)

#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
Expand Down Expand Up @@ -60,5 +61,10 @@ int main(void)
nanosleep(&sleepTime, NULL);
LED_Off(fd);
nanosleep(&sleepTime, NULL);
#ifdef LEAK
// Intentionally leak here to show an example of using valgrind to detect memory leaks
int* leak = (int*)malloc(sizeof(int));
*leak = 1;
#endif
}
}

0 comments on commit 4def68f

Please sign in to comment.