diff --git a/examples/file/listdir.c b/examples/file/listdir.c index 98419db873f9d411fed7487961d2f84d14869bb6..3300389ab9bb2ecd8c49335b64aa3a5ab37a5c5e 100644 --- a/examples/file/listdir.c +++ b/examples/file/listdir.c @@ -13,7 +13,7 @@ void list_dir(const char* path) { - char * fullpath; + char * fullpath = RT_NULL; DIR *dir; dir = opendir(path); @@ -26,6 +26,7 @@ void list_dir(const char* path) if (fullpath == RT_NULL) { rt_kprintf("no memory\n"); + closedir(dir); return; } @@ -55,8 +56,13 @@ void list_dir(const char* path) { rt_kprintf("open %s directory failed\n", path); } - - rt_free(fullpath); + + if (RT_NULL != fullpath) + { + rt_free(fullpath); + fullpath = RT_NULL; + } + } #ifdef RT_USING_FINSH