铛分析器缺少内存泄漏 [英] Clang analyzer missing memory leaks

查看:102
本文介绍了铛分析器缺少内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些cpp文件上的android服务上运行clang时出现问题. 我正在通过从中调用对象实例来创建故意的内存泄漏 另一个类而不删除它,以查看clang是否创建了内存泄漏警告 是否,但在某些情况下,它不是在创建内存泄漏警告.

I have issue while running clang on an android service on some cpp file. I am creating an intentional memory leak by calling an object instance from another class without deleting it to see if clang creates memory leak warning or not but for some cases It is not creating memory leak warning.

1-如果将类声明与要创建内存泄漏的类放在同一头文件中,则clang会按以下方式捕获内存泄漏:

1- If I put a class declaration in same header file with the class that I wanted to create a memory leak, clang catching the memory leak as the following:

 class Ad
 {
    public:
    void xx();
 };

 class Example
 {
    public:
    bool getData();
 };

Example.cpp

#include "Example.h"

void Ad::xx()
{
    bool ar = false;
    ar = true;
}

bool Example::getData()
{
    char *ptrt;
    ptrt = (char*)malloc(10*sizeof(char));
    snprintf(ptrt,10,"%s","trial");

    Ad *arr = new Ad();
    arr->xx();

    return true;
}

在此示例中,clang可以在getData()函数中捕获两次内存泄漏.

In this example, clang can catch 2 memory leaks in getData() function.

2-如果我在单独的头文件中创建类Ad声明,而不是clang无法捕获内存泄漏:

2-If I create class Ad declaration in separate header file than clang can not catch memory leak:

class Ad
{
    public:
        void xx();
};

Ad.cpp

#include "Ad.h"

void Ad::xx()
{
    bool ar = false;
    ar = true;
}

Example.h

class Example
{
    public:
        bool getData();
};

Example.cpp

 #include "Example.h"
 #include "Ad.h"

bool Example::getData()
{
    Ad *arr = new Ad();
    arr->xx();
    //Clang can not catch memory leak error here..
    return true;
}

Notes:

I am exporting WITH_STATIC_ANALYZER=1 on aosp android/ folder and running mmma module_name/ .

I am using Android P for aosp. I also initialized this flags in Android.bp

cflags:[
    "-Wall",
    "-Werror",
    "-Wunused",
    "-Wunreachable-code",
    ],

有什么想法可能会发生这种情况吗?

Is there any idea why that may happen ?

推荐答案

我对clang的分析器不太熟悉;但是,我认为与第一种情况相比,第二种情况发生了两件事:

I'm not so familiar with clang's analyzer; however I think 2 things happen in the second case compared to the first:

1)编译时,您要调用clang两次,创建2个目标文件,每个目标文件都有一个功能.因此,分析器数据无法从Ad::xx传递到Example::getData.

1) When compiling, you're calling clang twice, creating 2 object files, each with one of the functions. Hence analyzer data cannot pass through from Ad::xx to Example::getData.

2)从Example.cpp clang调用的角度来看,没有任何东西可以保证Ad::xx不会存储对调用它的Ad对象的引用,因此其生存期可能比Example::getData范围.因此,没有内存泄漏警告.

2) From the point of view of the Example.cpp clang invocation, nothing guarantees that Ad::xx does not store a reference to the Ad object on which it is called, thus its lifetime could be longer than the Example::getData scope. Hence no memory leak warning.

对于两种实现,请尝试使用相同的输入文件;应该可以使它起作用.

Try using the same input file for both implementations; that should make it work.

这篇关于铛分析器缺少内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆