why is delete being called in stdc++ library when there is no delete nor free in the code flow?

c++, gdb, linux

Solution

You are calling `std::string::append`, that ultimately results in `delete` getting called. If we go through the steps involved in `std::string::append`, it might make more sense why `delete` gets called.

Say you have:

std::string s("abc");
s.append("def");

When you create `s`, memory has to be allocated to hold `"abc"`. At the end of `s.append("def");`, there has to be enough memory associated with `s` to hold `"abcdef"`. Steps to get there:

- Get the length of `s` => `3`.

- Get the length of the input string `"def"` => `3`.

- Add them to figure out the length of the new string. => `6`.

- Allocate memory to hold the new string.

- Copy `"abc"` to the newly allocated memory.

- Append `"def"` to the newly allocated memory.

- Associate the newly allocated memory with `s`.

- Delete the old memory associated with `s`. (This is where `delete` comes into picture).

Problem

I am having a problem debugging my code and am a bit confused by the gdb output. I have attached the gdb output below. The last 2 lines, line #13 and #14 are my code, but everything else is from the C++ library. What is confusing to me is that from about line #7 upward, it appears to be calling delete. This is initialization code and there are no deletes nor frees being called in the code flow. But something is causing delete to be called somewhere in the C++ library. this is on a debian box with g++ 4.7.2 Anybody have a clue that could help me along? EDIT: thanks you guys for your help. I indeed think there is something else going on here. Since the intent of my code is to construct a string using several append() calls, I added a call to reserve() in the ctor for that string so it would be large enough to handle a few append() calls without having to get more space. This has apparently helped because it is now harder for me to force the crash. But I do agree that the cause is probably elsewhere in my code. Again, thanks for all your help. ``` Program received signal SIGABRT, Aborted. 0xb7fe1424 in __kernel_vsyscall () (gdb) bt #0 0xb7fe1424 in __kernel_vsyscall () #1 0xb7a9a941 in *__GI_raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64 #2 0xb7a9dd72 in *__GI_abort () at abort.c:92 #3 0xb7ad6e15 in __libc_message (do_abort=2, fmt=0xb7baee70 "*** glibc detected *** %s: %s: 0x%s ***\n") at ../sysdeps/unix/sysv/linux/libc_fatal.c:189 #4 0xb7ae0f01 in malloc_printerr (action=<optimized out>, str=0x6 <Address 0x6 out of bounds>, ptr=0xb71117f0) at malloc.c:6283 #5 0xb7ae2768 in _int_free (av=<optimized out>, p=<optimized out>) at malloc.c:4795 #6 0xb7ae581d in *__GI___libc_free (mem=0xb71117f0) at malloc.c:3738 #7 0xb7f244bf in operator delete(void*) () from /usr/lib/i386-linux-gnu/libstdc++.so.6 #8 0xb7f8b48b in std::string::_Rep::_M_destroy(std::allocator<char> const&) () from /usr/lib/i386-linux-gnu/libstdc++.so.6 #9 0xb7f8b4d0 in ?? () from /usr/lib/i386-linux-gnu/libstdc++.so.6 #10 0xb7f8c7a0 in std::string::reserve(unsigned int) () from /usr/lib/i386-linux-gnu/libstdc++.so.6 #11 0xb7f8caaa in std::string::append(char const*, unsigned int) () from /usr/lib/i386-linux-gnu/libstdc++.so.6 #12 0xb7f8cb76 in std::string::append(char const*) () from /usr/lib/i386-linux-gnu/libstdc++.so.6 #13 0x0804fa38 in MethodRequest::MethodRequest (this=0x80977a0) at cLogProxy.cpp:26 #14 0x0804fac0 in DebugMethodRequest::DebugMethodRequest (this=0x80977a0, ``` thanks, -Andres

Original source