0
내 프로그램 실행의 끝에서 다음과 같은 오류를 받고 있어요

발생 :사용 더블 무료 또는 손상 오류

* Error in `./bin/test': double free or corruption (out): 0x00007ffd34dab0d0 * Aborted

나는 현재 객체 기반의 API를 테스트하고을 (--gen 객체 -api)는 Flatbuffer에서 C++ 용입니다. 내 메인 파일은 다음과 같습니다 :

int main(int argc, char* argv[]) { 

    flatbuffers::FlatBufferBuilder builder; 
    auto dataTest = event::ByteBufferT(); 
    dataTest.Bytes.push_back(1); 
    dataTest.Bytes.push_back(2); 

    event::EventDataUnion unionD; 
    unionD.type = event::EventData_ByteBuffer; 
    unionD.value = &dataTest; 

    auto eventOffset = event::CreateEvent(builder, 
         builder.CreateString("byteDataEvent"), 10, 
         event::EventData_ByteBuffer, unionD.Pack(builder)); 

    builder.Finish(eventOffset); 

    auto eventOutput = event::GetEvent(builder.GetBufferPointer()); 
    auto vec(eventOutput->data_as_ByteBuffer()->Bytes()); 

    for (auto it = vec->begin(); it != vec->end(); ++it) { 
     std::cout << "ByteData: " << int(*it) << std::endl; 
    } 

    return 0; 
} 

나는 어디에서 오류가 발생하는지 알고 있지만 어떻게 피할 수 있는지 알지 못합니다. Valgrind의 출력은 다음과 같습니다가 :

==6242== Invalid free()/delete/delete[]/realloc() 
==6242== at 0x4C2A360: operator delete(void*) (vg_replace_malloc.c:507) 
==6242== by 0x4031C3: event::EventDataUnion::Reset() (event_generated.h:725) 
==6242== by 0x4028F5: event::EventDataUnion::~EventDataUnion() (event_generated.h:78) 
==6242== by 0x401320: main (main.cpp:118) 
==6242== Address 0xfff0000d0 is on thread 1's stack 
==6242== in frame #3, created by main (main.cpp:8) 
==6242== 
==6242== Invalid free()/delete/delete[]/realloc() 
==6242== at 0x4C2A360: operator delete(void*) (vg_replace_malloc.c:507) 
==6242== by 0x406373: __gnu_cxx::new_allocator<signed char>::deallocate(signed char*, unsigned long) (new_allocator.h:110) 
==6242== by 0x405FC6: std::allocator_traits<std::allocator<signed char> >::deallocate(std::allocator<signed char>&, signed char*, unsigned long) (alloc_traits.h:383) 
==6242== by 0x40585D: std::_Vector_base<signed char, std::allocator<signed char> >::_M_deallocate(signed char*, unsigned long) (stl_vector.h:178) 
==6242== by 0x404DA4: std::_Vector_base<signed char, std::allocator<signed char> >::~_Vector_base() (stl_vector.h:160) 
==6242== by 0x403C5E: std::vector<signed char, std::allocator<signed char> >::~vector() (stl_vector.h:425) 
==6242== by 0x403109: event::ByteBufferT::~ByteBufferT() (event_generated.h:260) 
==6242== by 0x40132C: main (main.cpp:114) 
==6242== Address 0x5a021d0 is 0 bytes inside a block of size 2 free'd 
==6242== at 0x4C2A360: operator delete(void*) (vg_replace_malloc.c:507) 
==6242== by 0x406373: __gnu_cxx::new_allocator<signed char>::deallocate(signed char*, unsigned long) (new_allocator.h:110) 
==6242== by 0x405FC6: std::allocator_traits<std::allocator<signed char> >::deallocate(std::allocator<signed char>&, signed char*, unsigned long) (alloc_traits.h:383) 
==6242== by 0x40585D: std::_Vector_base<signed char, std::allocator<signed char> >::_M_deallocate(signed char*, unsigned long) (stl_vector.h:178) 
==6242== by 0x404DA4: std::_Vector_base<signed char, std::allocator<signed char> >::~_Vector_base() (stl_vector.h:160) 
==6242== by 0x403C5E: std::vector<signed char, std::allocator<signed char> >::~vector() (stl_vector.h:425) 
==6242== by 0x403109: event::ByteBufferT::~ByteBufferT() (event_generated.h:260) 
==6242== by 0x4031BB: event::EventDataUnion::Reset() (event_generated.h:725) 
==6242== by 0x4028F5: event::EventDataUnion::~EventDataUnion() (event_generated.h:78) 
==6242== by 0x401320: main (main.cpp:118) 
==6242== 
==6242== 
==6242== HEAP SUMMARY: 
==6242==  in use at exit: 0 bytes in 0 blocks 
==6242== total heap usage: 5 allocs, 7 frees, 1,219 bytes allocated 
==6242== 
==6242== All heap blocks were freed -- no leaks are possible 
==6242== 
==6242== For counts of detected and suppressed errors, rerun with: -v 
==6242== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 2 from 1) 

내 flatbuffer 파일 (event.fbs) 다음과 같습니다

// event.fbs 
namespace event; 

table TableData { 
    param1:string; 
    param2:uint; 
} 

table ByteBuffer { 
    Bytes:[byte]; 
} 

struct StructData { 
    x:float; 
    y:float; 
} 

union EventData { 
    TableData, 
    StructData, 
    ByteBuffer, 
    String:string, 
} 

table Event { 
    name:string (key); 
    timestamp:ulong = -1; 
    data:EventData; 
} 

root_type Event; 

내 출력 오류를 제외하고, 올바른 :

ByteData: 1 
ByteData: 2 
*** Error in `./bin/test': double free or corruption (out): 0x00007ffd34dab0d0 *** 
Aborted 

업데이트 :

개체 API가 없으면 이런 식으로 알 겠어? :

flatbuffers::FlatBufferBuilder builder; 
signed char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
auto byteBuffer = event::CreateByteBuffer(builder, builder.CreateVector(inv_data, 10)); 
auto event = event::CreateEvent(builder, builder.CreateString("byteDataEvent"), 10, event::EventData_ByteBuffer, byteBuffer.Union()); 
builder.Finish(event); 

그리고 난 빌더를 통과 할까?

답변

0

오류는 여기 가능성 :

unionD.value = &dataTest; 

노조 객체가 가리키는 값을 자신의 것으로 기대하고, 마지막에 삭제됩니다. 그러나 지역 변수로도 삭제됩니다.

개체 API (ByteBufferEventData)와 표준 API (Event)의 두 가지 API 사용을 혼합합니다. 이것은 완벽하게 훌륭하지만 의도 한 바가 아닐 수 있습니다 (전자는 편의/저장 용으로, 후자는 속도 용). 객체 API는 일반적으로 자식을 소유하는 객체의 독립적 인 트리입니다.

+0

감사합니다. 질문을 업데이트하고 객체 API가없는 예제를 추가했습니다. 가장 빠른 솔루션입니까? – Akinna

+0

그래, 추가 메모리를 할당하지 않으면 가장 빠릅니다. – Aardappel

+0

참고 : "빌더를 전달하지"않을 것입니다. 빌더가 만든 버퍼를 전달하려고합니다 (예 : 'builder.GetBufferPointer()'와'builder.GetSize()' – Aardappel

관련 문제