c++ - Memory Allocation Error Caused by Inserting into std::vector -
as part of university project i'm writing text editor scratch. i'm having issues code i've written build maintain core data structure.
below sourcecode, error gcc/g++ giving me, , stacktrace got gdb. go more detail design below of that.
yes, feel bad printing , verbose declarations in document::insertchar. thought might initialisation issue @ first. , i'm still learning reins gdb.
importantly though, temp.push_back(lnd); , lines.insert(iter, nline); (lnd originall nline) both cause same error.
sourcecode
document.cpp : http://pastebin.com/lgghmir8 line.cpp : http://pastebin.com/bbhbnxut (code never tested on non linux platform, builds g++ may not other compilers)
error:
test: malloc.c:2388: sysmalloc: assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. aborted (core dumped)
stacktrace:
(gdb) backtrace #0 0x00007ffff71d45f8 in raise () /usr/lib/libc.so.6 #1 0x00007ffff71d5a7a in abort () /usr/lib/libc.so.6 #2 0x00007ffff7218468 in __malloc_assert () /usr/lib/libc.so.6 #3 0x00007ffff721a056 in sysmalloc () /usr/lib/libc.so.6 #4 0x00007ffff721b0a6 in _int_malloc () /usr/lib/libc.so.6 #5 0x00007ffff721c3d4 in malloc () /usr/lib/libc.so.6 #6 0x00007ffff7ae70e8 in operator new (sz=24) @ /build/gcc/src/gcc-5.3.0/libstdc++-v3/libsupc++/new_op.cc:50 #7 0x0000000000401f15 in __gnu_cxx::new_allocator<line>::allocate (this=0x7fffffffe950, __n=1) @ /usr/include/c++/5.3.0/ext/new_allocator.h:104 #8 0x0000000000401d9e in __gnu_cxx::__alloc_traits<std::allocator<line> >::allocate (__a=..., __n=1) @ /usr/include/c++/5.3.0/ext/alloc_traits.h:182 #9 0x0000000000401b90 in std::_vector_base<line, std::allocator<line> >::_m_allocate (this=0x7fffffffe950, __n=1) @ /usr/include/c++/5.3.0/bits/stl_vector.h:170 #10 0x0000000000401654 in std::vector<line, std::allocator<line> >::_m_insert_aux (this=0x7fffffffe950, __position=<error reading variable: cannot access memory @ address 0x0>, __x=...) @ /usr/include/c++/5.3.0/bits/vector.tcc:353 #11 0x000000000040139e in std::vector<line, std::allocator<line> >::push_back (this=0x7fffffffe950, __x=...) @ /usr/include/c++/5.3.0/bits/stl_vector.h:925 #12 0x0000000000400fff in document::insertchar (this=0x7fffffffe9b0, chr=10 '\n') @ document.cpp:67 #13 0x00000000004011be in main (argc=1, argv=0x7fffffffeae8) @ document.cpp:204 a brief introduction design thing, using std::vector hold lines (mainly convenience). , class line, similar vector, it's imlementation of dynamic array. reason holding line metadata in there.
vector seems not when try use insert insert line. actually, beyond document::document(). time try inserting line object vector, whether push_back or insert. fails.
i... don't quite understand why, , to. failing - going have implement own version @ least understand what's going on. (shame can't o(1) on insert , search operations... linked list have been nice if didn't need fast searching...)
okay, had sleep. came this.
delete [] mdata called in copy constructor. mdata initialised null in copy constructor's initialisation list. problem a. more importantly;
std::copy(ln.mdata, **mdata**+mcapacity, mdata); no idea memory address mdata @ at point. it's sure hell not intended. root of allocation issue. person directed me @ copy constructor.
Comments
Post a Comment