Invalid read of size 8, but no memory leaks

时间:2015-07-31 20:51:15

标签: c++ memory-leaks valgrind apache-kafka

I am doing C++ Kafka Client https://github.com/edenhill/librdkafka/blob/master/examples/rdkafka_example.cpp.

In my KafkaProducer class, there are several pointers.

RdKafka::Conf* m_conf;
RdKafka::Conf* m_tconf;
RdKafka::Producer* m_producer;
RdKafka::Topic* m_topic;

m_conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
m_producer = RdKafka::Producer::create(m_conf, m_errstr);
m_tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
m_topic = RdKafka::Topic::create(m_producer, m_topic_str, m_tconf, m_errstr);

In the destructor, I did the following:

if(m_producer)
    delete m_producer;

if(m_topic)
    delete m_topic;

if(m_tconf)
    delete m_tconf;

if(m_conf)
    delete m_conf;

I used valgrind to check my program, there is no leaks. But there are some problems of "Invalid read". Some memory are freed twice. But I have do idea which memory. The following are part of the valgrind output.

==4627== 2 errors in context 10 of 12:
==4627== Invalid read of size 8
==4627==    at 0x52887A7: RdKafka::log_cb_trampoline(rd_kafka_s const*, int, char const*, char const*) (in /usr/lib/x86_64-linux-gnu/librdkafka++.so.1)
==4627==    by 0x5493C8F: ??? (in /usr/lib/x86_64-linux-gnu/librdkafka.so.1)
==4627==    by 0x549A531: ??? (in /usr/lib/x86_64-linux-gnu/librdkafka.so.1)
==4627==    by 0x54A0219: ??? (in /usr/lib/x86_64-linux-gnu/librdkafka.so.1)
==4627==    by 0x54A1A13: ??? (in /usr/lib/x86_64-linux-gnu/librdkafka.so.1)
==4627==    by 0x506C181: start_thread (pthread_create.c:312)
==4627==    by 0x5EE747C: clone (clone.S:111)
==4627==  Address 0x68df7f8 is 24 bytes inside a block of size 64 free'd
==4627==    at 0x4C2C2BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4627==    by 0x407E90: KafkaProducer::disconnect() (kafkaproducer.cpp:57)
==4627==    by 0x407D29: KafkaProducer::~KafkaProducer() (kafkaproducer.cpp:32)
==4627==    by 0x407DF1: KafkaProducer::~KafkaProducer() (kafkaproducer.cpp:38)
==4627==    by 0x409DC0: KafkaProducerFactory::~KafkaProducerFactory() (kafkaproducerfactory.cpp:22)
==4627==    by 0x4045A8: main (test_kafkaproducerfactory.cpp:14)

kafkaproducer.cpp lines 56-67 (double-spacing preserved):

 if(m_producer)           // line 56
     delete m_producer;

 if(m_topic)
     delete m_topic;


 if(m_tconf)
     delete m_tconf;

 if(m_conf)
     delete m_conf;       // line 67

Any help is welcome.

UPDATE

I already found the following codes caused this problem. If I commented the following part (If I did not set the event call back function.), the problem would disappear. But I have no idea why it caused the problem. I used the same way to set delivery call back function, which did not cause any problem. It is wired.

//defination
    class MyEventCb : public RdKafka::EventCb
    {
    public:
        void event_cb (RdKafka::Event &event)
        {
        }
    private:

    };

//set the callback function
    if( m_conf->set("event_cb", &m_event_cb, m_errstr) != RdKafka::Conf::CONF_OK)
    {
        DBG_PRINT( 1, "Kafka::Failed to set event callback : %s\n", m_errstr.c_str() );
        return false;
    }
    // Class member
MyEventCb m_event_cb; // event callback

1 个答案:

答案 0 :(得分:1)

由于m_topic正在使用m_producer,我认为你不应该在m_topic之前删除m_producer ......