I am writing a c++ code. As a part of it, I use a C library quickjs
.
This library allows creating dynamic memory using its custom js_mallocz
call (which internally uses malloc), and freeing it later with js_free
call.
I have a struct like this :
struct Person{
std::string id;
unsigned short age;
};
Which I have created using js_mallocz
call, which is freed using the proper call.
//while assigning
auto person = (Person*) js_mallocz(ctx,sizeof(Person));
sole::uuid u4 =sole::uuid4();
std::string u = u4.base62();
person->id=u;
person->age=40;
//while freeing
js_free(ctx,person);
A code like this would give an error like :
24 bytes in 1 blocks are definitely lost in loss record 1 of 2
==30236== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==30236== by 0x49D93FE: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.29)
==30236== by 0x11F93B: assign (basic_string.h:1370)
==30236== by 0x11F93B: operator= (basic_string.h:700)
I can't use new operator as I am required to use the memory allocation done by the third party lib quickjs. How do one free std::string in such struct created using malloc ?