morganstanley / modern-cpp-kafka

A C++ API for Kafka clients (i.e. KafkaProducer, KafkaConsumer, AdminClient)
Apache License 2.0
331 stars 86 forks source link

question: how can I ensure that the memorey that record.headers() points to say valid? #191

Closed jonathan-dev closed 1 year ago

jonathan-dev commented 1 year ago

I am receiving kafka messages like it is done in this example and from there I am passing the record.headers() through my application. But it seems that is the memory that it points to is invalidated after some point... how can I ensure that the memory stays alive? or what is the suggested way of dealing with this.

thanks

kenneth-jia commented 1 year ago

Hi, @jonathan-dev The value type of header is const_buffer, and the lifetime depends on the ConsumerRecord. So, either you keep the record until it has been processed, or deep-copy the memory block for header.value() before releasing the record.

jonathan-dev commented 1 year ago

Ok. make sense. Thanks for the answer. I'll close this.