Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I encountered this problem while doing my assignments. When you have a class B that inherits class A, and B initializes variables in A by calling the constructor of A in B's constructor, the variable that is supposed to be initialized in A's constructor remains uninitialized. This doesn't seem to happen if we create an object of class A, even though seemingly the only difference is the inheritance and constructor chain.
Here is a minimal example:
#include <iostream>
#include <string>
class A {
public:
A(std::string s)
: s_(s) {}
~A(){}
void Print() const {
std::cout << s_ + "123";
}
private:
const std::string& s_;
};
class B : public A {
public:
B(std::string s)
: A(s) {}
};
int main()
{
//A a = A("123");
//a.Print();
B b = B("123");
b.Print();
}
In Print(), you can remove the extra string literal. This way the issue is focused on the variable not being initialized. But with the extra literal, according to valgrind, a million bytes are still reachable. This seems very weird.
Valgrind without literal in Print():
g++ -c -g -std=c++17 -Wall -Wextra -pedantic main.cpp -o main.o
g++ main.o -o main
valgrind ./main
==22551== Memcheck, a memory error detector
==22551== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==22551== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==22551== Command: ./main
==22551==
==22551== error calling PR_SET_PTRACER, vgdb might block
==22551== Conditional jump or move depends on uninitialised value(s)
==22551== at 0x4F4FA9A: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551== at 0x545C928: fwrite (iofwrite.c:35)
==22551== by 0x4F4FB83: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551== at 0x54689B4: _IO_file_xsputn##GLIBC_2.2.5 (fileops.c:1226)
==22551== by 0x545C9E6: fwrite (iofwrite.c:39)
==22551== by 0x4F4FB83: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551== at 0x5468A85: _IO_file_xsputn##GLIBC_2.2.5 (fileops.c:1275)
==22551== by 0x545C9E6: fwrite (iofwrite.c:39)
==22551== by 0x4F4FB83: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551== at 0x5468210: _IO_file_write##GLIBC_2.2.5 (fileops.c:1198)
==22551== by 0x5468B9E: new_do_write (fileops.c:457)
==22551== by 0x5468B9E: _IO_file_xsputn##GLIBC_2.2.5 (fileops.c:1277)
==22551== by 0x545C9E6: fwrite (iofwrite.c:39)
==22551== by 0x4F4FB83: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Syscall param write(buf) contains uninitialised byte(s)
==22551== at 0x54ED264: write (write.c:27)
==22551== by 0x546822C: _IO_file_write##GLIBC_2.2.5 (fileops.c:1203)
==22551== by 0x5468B9E: new_do_write (fileops.c:457)
==22551== by 0x5468B9E: _IO_file_xsputn##GLIBC_2.2.5 (fileops.c:1277)
==22551== by 0x545C9E6: fwrite (iofwrite.c:39)
==22551== by 0x4F4FB83: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Syscall param write(count) contains uninitialised byte(s)
==22551== at 0x54ED264: write (write.c:27)
==22551== by 0x546822C: _IO_file_write##GLIBC_2.2.5 (fileops.c:1203)
==22551== by 0x5468B9E: new_do_write (fileops.c:457)
==22551== by 0x5468B9E: _IO_file_xsputn##GLIBC_2.2.5 (fileops.c:1277)
==22551== by 0x545C9E6: fwrite (iofwrite.c:39)
==22551== by 0x4F4FB83: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Syscall param write(buf) points to uninitialised byte(s)
==22551== at 0x54ED264: write (write.c:27)
==22551== by 0x546822C: _IO_file_write##GLIBC_2.2.5 (fileops.c:1203)
==22551== by 0x5468B9E: new_do_write (fileops.c:457)
==22551== by 0x5468B9E: _IO_file_xsputn##GLIBC_2.2.5 (fileops.c:1277)
==22551== by 0x545C9E6: fwrite (iofwrite.c:39)
==22551== by 0x4F4FB83: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551== Address 0x1ffefff910 is on thread 1's stack
==22551== in frame #5, created by A::Print() const (main.cpp:14)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551== at 0x5468BB0: new_do_write (fileops.c:458)
==22551== by 0x5468BB0: _IO_file_xsputn##GLIBC_2.2.5 (fileops.c:1277)
==22551== by 0x545C9E6: fwrite (iofwrite.c:39)
==22551== by 0x4F4FB83: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551== at 0x5468BEB: _IO_file_xsputn##GLIBC_2.2.5 (fileops.c:1279)
==22551== by 0x545C9E6: fwrite (iofwrite.c:39)
==22551== by 0x4F4FB83: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551== at 0x545CA65: fwrite (iofwrite.c:45)
==22551== by 0x4F4FB83: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551== at 0x545CA6A: fwrite (iofwrite.c:45)
==22551== by 0x4F4FB83: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551==
==22551== Conditional jump or move depends on uninitialised value(s)
==22551== at 0x4F4FB91: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25)
==22551== by 0x109BAF: A::Print() const (main.cpp:15)
==22551== by 0x1099EB: main (main.cpp:32)
==22551==
==22551==
==22551== HEAP SUMMARY:
==22551== in use at exit: 0 bytes in 0 blocks
==22551== total heap usage: 2 allocs, 2 frees, 73,216 bytes allocated
==22551==
==22551== All heap blocks were freed -- no leaks are possible
==22551==
==22551== For counts of detected and suppressed errors, rerun with: -v
==22551== Use --track-origins=yes to see where uninitialised values come from
==22551== ERROR SUMMARY: 13 errors from 13 contexts (suppressed: 0 from 0)
Valgrind with literal in Print():
(I had to cut the middle out to fit into the character limit, the whole thing is here: https://pastebin.com/UQmB0mXj)
g++ -c -g -std=c++17 -Wall -Wextra -pedantic main.cpp -o main.o
g++ main.o -o main
valgrind ./main
==22561== Memcheck, a memory error detector
==22561== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==22561== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==22561== Command: ./main
==22561==
==22561== error calling PR_SET_PTRACER, vgdb might block
==22561== Conditional jump or move depends on uninitialised value(s)
==22561== at 0x10AC50: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) (basic_string.tcc:217)
==22561== by 0x10A7DD: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct_aux<char*>(char*, char*, std::__false_type) (basic_string.h:236)
==22561== by 0x10A472: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*) (basic_string.h:255)
==22561== by 0x10A15B: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:440)
==22561== by 0x10A05E: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*) (basic_string.h:5928)
==22561== by 0x109E66: A::Print() const (main.cpp:15)
==22561== by 0x109C8B: main (main.cpp:32)
==22561==
==22561== Conditional jump or move depends on uninitialised value(s)
==22561== at 0x10AD84: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_create(unsigned long&, unsigned long) (basic_string.tcc:137)
==22561== by 0x10AC69: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) (basic_string.tcc:219)
==22561== by 0x10A7DD: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct_aux<char*>(char*, char*, std::__false_type) (basic_string.h:236)
==22561== by 0x10A472: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*) (basic_string.h:255)
==22561== by 0x10A15B: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:440)
==22561== by 0x10A05E: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*) (basic_string.h:5928)
==22561== by 0x109E66: A::Print() const (main.cpp:15)
==22561== by 0x109C8B: main (main.cpp:32)
==22561==
...
had to cut here to fit into character limit
...
==22561==
==22561== Conditional jump or move depends on uninitialised value(s)
==22561== at 0x10A9F0: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy(char*, char const*, unsigned long) (basic_string.h:337)
==22561== by 0x10AED7: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy_chars(char*, char*, char*) (basic_string.h:382)
==22561== by 0x10ACB0: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) (basic_string.tcc:225)
==22561== by 0x10A7DD: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct_aux<char*>(char*, char*, std::__false_type) (basic_string.h:236)
==22561== by 0x10A472: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*) (basic_string.h:255)
==22561== by 0x10A15B: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:440)
==22561== by 0x10A05E: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*) (basic_string.h:5928)
==22561== by 0x109E66: A::Print() const (main.cpp:15)
==22561== by 0x109C8B: main (main.cpp:32)
==22561==
==22561== Conditional jump or move depends on uninitialised value(s)
==22561== at 0x109DE1: std::char_traits<char>::copy(char*, char const*, unsigned long) (char_traits.h:348)
==22561== by 0x10AA1D: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy(char*, char const*, unsigned long) (basic_string.h:340)
==22561== by 0x10AED7: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy_chars(char*, char*, char*) (basic_string.h:382)
==22561== by 0x10ACB0: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) (basic_string.tcc:225)
==22561== by 0x10A7DD: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct_aux<char*>(char*, char*, std::__false_type) (basic_string.h:236)
==22561== by 0x10A472: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*) (basic_string.h:255)
==22561== by 0x10A15B: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:440)
==22561== by 0x10A05E: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*) (basic_string.h:5928)
==22561== by 0x109E66: A::Print() const (main.cpp:15)
==22561== by 0x109C8B: main (main.cpp:32)
==22561==
==22561== Conditional jump or move depends on uninitialised value(s)
==22561== at 0x4C366E6: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22561== by 0x109E05: std::char_traits<char>::copy(char*, char const*, unsigned long) (char_traits.h:350)
==22561== by 0x10AA1D: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy(char*, char const*, unsigned long) (basic_string.h:340)
==22561== by 0x10AED7: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy_chars(char*, char*, char*) (basic_string.h:382)
==22561== by 0x10ACB0: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) (basic_string.tcc:225)
==22561== by 0x10A7DD: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct_aux<char*>(char*, char*, std::__false_type) (basic_string.h:236)
==22561== by 0x10A472: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*) (basic_string.h:255)
==22561== by 0x10A15B: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:440)
==22561== by 0x10A05E: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*) (basic_string.h:5928)
==22561== by 0x109E66: A::Print() const (main.cpp:15)
==22561== by 0x109C8B: main (main.cpp:32)
==22561==
==22561== Conditional jump or move depends on uninitialised value(s)
==22561== at 0x4C36711: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22561== by 0x109E05: std::char_traits<char>::copy(char*, char const*, unsigned long) (char_traits.h:350)
==22561== by 0x10AA1D: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy(char*, char const*, unsigned long) (basic_string.h:340)
==22561== by 0x10AED7: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy_chars(char*, char*, char*) (basic_string.h:382)
==22561== by 0x10ACB0: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) (basic_string.tcc:225)
==22561== by 0x10A7DD: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct_aux<char*>(char*, char*, std::__false_type) (basic_string.h:236)
==22561== by 0x10A472: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*) (basic_string.h:255)
==22561== by 0x10A15B: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:440)
==22561== by 0x10A05E: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*) (basic_string.h:5928)
==22561== by 0x109E66: A::Print() const (main.cpp:15)
==22561== by 0x109C8B: main (main.cpp:32)
==22561==
==22561== Conditional jump or move depends on uninitialised value(s)
==22561== at 0x4C367EE: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22561== by 0x109E05: std::char_traits<char>::copy(char*, char const*, unsigned long) (char_traits.h:350)
==22561== by 0x10AA1D: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy(char*, char const*, unsigned long) (basic_string.h:340)
==22561== by 0x10AED7: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy_chars(char*, char*, char*) (basic_string.h:382)
==22561== by 0x10ACB0: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) (basic_string.tcc:225)
==22561== by 0x10A7DD: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct_aux<char*>(char*, char*, std::__false_type) (basic_string.h:236)
==22561== by 0x10A472: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*) (basic_string.h:255)
==22561== by 0x10A15B: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:440)
==22561== by 0x10A05E: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*) (basic_string.h:5928)
==22561== by 0x109E66: A::Print() const (main.cpp:15)
==22561== by 0x109C8B: main (main.cpp:32)
==22561==
==22561== Invalid read of size 8
==22561== at 0x4C367EE: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22561== by 0x109E05: std::char_traits<char>::copy(char*, char const*, unsigned long) (char_traits.h:350)
==22561== by 0x10AA1D: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy(char*, char const*, unsigned long) (basic_string.h:340)
==22561== by 0x10AED7: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy_chars(char*, char*, char*) (basic_string.h:382)
==22561== by 0x10ACB0: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) (basic_string.tcc:225)
==22561== by 0x10A7DD: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct_aux<char*>(char*, char*, std::__false_type) (basic_string.h:236)
==22561== by 0x10A472: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*) (basic_string.h:255)
==22561== by 0x10A15B: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:440)
==22561== by 0x10A05E: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*) (basic_string.h:5928)
==22561== by 0x109E66: A::Print() const (main.cpp:15)
==22561== by 0x109C8B: main (main.cpp:32)
==22561== Address 0x1fff001000 is not stack'd, malloc'd or (recently) free'd
==22561==
==22561==
==22561== Process terminating with default action of signal 11 (SIGSEGV)
==22561== Access not within mapped region at address 0x1FFF001000
==22561== at 0x4C367EE: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22561== by 0x109E05: std::char_traits<char>::copy(char*, char const*, unsigned long) (char_traits.h:350)
==22561== by 0x10AA1D: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy(char*, char const*, unsigned long) (basic_string.h:340)
==22561== by 0x10AED7: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy_chars(char*, char*, char*) (basic_string.h:382)
==22561== by 0x10ACB0: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) (basic_string.tcc:225)
==22561== by 0x10A7DD: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct_aux<char*>(char*, char*, std::__false_type) (basic_string.h:236)
==22561== by 0x10A472: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*) (basic_string.h:255)
==22561== by 0x10A15B: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:440)
==22561== by 0x10A05E: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*) (basic_string.h:5928)
==22561== by 0x109E66: A::Print() const (main.cpp:15)
==22561== by 0x109C8B: main (main.cpp:32)
==22561== If you believe this happened as a result of a stack
==22561== overflow in your program's main thread (unlikely but
==22561== possible), you can try to increase the size of the
==22561== main thread stack using the --main-stacksize= flag.
==22561== The main thread stack size used in this run was 8388608.
==22561==
==22561== HEAP SUMMARY:
==22561== in use at exit: 1,089,515 bytes in 1 blocks
==22561== total heap usage: 2 allocs, 1 frees, 1,162,219 bytes allocated
==22561==
==22561== LEAK SUMMARY:
==22561== definitely lost: 0 bytes in 0 blocks
==22561== indirectly lost: 0 bytes in 0 blocks
==22561== possibly lost: 0 bytes in 0 blocks
==22561== still reachable: 1,089,515 bytes in 1 blocks
==22561== suppressed: 0 bytes in 0 blocks
==22561== Rerun with --leak-check=full to see details of leaked memory
==22561==
==22561== For counts of detected and suppressed errors, rerun with: -v
==22561== Use --track-origins=yes to see where uninitialised values come from
==22561== ERROR SUMMARY: 197 errors from 12 contexts (suppressed: 0 from 0)
src.make:32: recipe for target 'valgrind-run' failed
make: *** [valgrind-run] Segmentation fault (core dumped)
My questions are:
Why does the compiler not give an error because of A's constructor? How can we set a reference with the value?
Why does this only give errors when there is inheritance involved?
Why does adding a literal in Print() have such a big effect? And why are a million bytes allocated because of this?
Why does the compiler not give an error because of A's constructor?
Presumably because A's constructor is well-formed, and thus the compiler must accept it in order to conform to the standard.
You are allowed to bind a reference to a local variable. The reference will be invalid after the constructor returns, but if the program never indirects through the reference after returning from the constructor, then that is technically no problem. It is not trivial for the compiler to prove that the program will do that (this problem is generally analogous to the halting problem).
A compiler does warn about it through:
warning: binding reference member 's_' to stack allocated parameter 's' [-Wdangling-field]
How can we set a reference with the value?
You have bound the reference to the local variable. It is unclear what you're trying to do, but probably you should not be using a reference member to achieve that.
Why ...
Why ...
Because behaviour of the program is undefined. Any change can affect the behaviour of the program in any way. The behaviour can even change without change to the program. Or it might not change. Nothing about the behaviour of the program is guaranteed.
I'm trying to do this:
int main(){
std::unique_ptr<std::thread> up(nullptr);
up = std::make_unique<std::thread>(upload, url, path);
return 0;
}
where, upload is basically a static non-member function and url and path are std::string. upload is defined as:
static void upload(const string url, const string path){ int sync_status;}
But this is always getting reported as a memory leak under Valgrind:
249 (80 direct, 169 indirect) bytes in 1 blocks are definitely lost in loss record 151 of 169
==12088== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12088== by 0x1395F9: _S_make_state<std::thread::_Invoker<std::tuple<void (*)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >), std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> , std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > (thread:197)
==12088== by 0x1395F9: thread<void (&)(std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char>), std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> &> (thread:126)
==12088== by 0x1395F9: std::_MakeUniq<std::thread>::__single_object std::make_unique<std::thread, void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> ), std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&>(void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >), std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> &, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (unique_ptr.h:825)
==12088== by 0x131D8D: main
Your code ends up invoking std::thread::~thread which calls std::terminate because the thread is neither detached nor joined. This is why valgrind reports an error.
Either join or detach the thread.
I am trying to control de workflow of a program I am developing. To do so I have a map< unsigned int, list < unsigned int > > in which the first key will be the id and the second ( the list ) will be used to know if I end correctly all tasks. The only operations I use on this list are:
myMap[iD].size()
myMap[iD].push_back(foo) <- ( foo is an unsigned int )
for (std::list<unsigned int>::iterator it=myMap[iD].begin(); it != myMap[iD].end(); ++it){
myMap[iD].erase(it)
}
The length of my map can grow to 1452 elements and each element list size can be from the order of 1000 ~ 5000.
When I run the program sometimes I receive a segmentation fault and some times a bad allocation error. My guess is that this come from the push_back because:
If I don't push back any element the program works fine.
The storage for the new elements is allocated using the container's allocator, which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed). http://www.cplusplus.com/reference/list/list/push_back/
This is the only part of the code where I use the map:
if (FOO != 0){
if (PID != 0){
if ( myMap.size() + 5 < myMap.max_size()){
if (myMap[PID].size() > 1000) myMap[PID].pop_front();
myMap[PID].push_back(EVENTVALUE);
}
}
} else {
if (PID != 0 and foo2 != 0 and myMap.find(PID) != myMap.end()) {
for (std::list<unsigned int>::iterator it=myMap[PID].begin(); it != myMap[PID].end(); ++it){
if (*it == foo2){
cout << " erasing pid: " << PID << endl;
myMap[PID].erase(it);
if ( myMap[PID].size() == 0 ) myMap.erase(PID);
break;
}
}
}
}
I've also tried to use the tool Valgrind and this is the output:
==4092== Invalid read of size 8
==4092== at 0x4F09EB8: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) (in /usr/lib64/libstdc++.so.6.0.21)
==4092== by 0x40CCA9: construct<std::basic_string<char>, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&> (new_allocator.h:120)
==4092== by 0x40CCA9: _S_construct<std::basic_string<char>, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&> (alloc_traits.h:254)
==4092== by 0x40CCA9: construct<std::basic_string<char>, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&> (alloc_traits.h:393)
==4092== by 0x40CCA9: std::vector<std::string, std::allocator<std::string> >::push_back(std::string const&) (stl_vector.h:905)
==4092== by 0x4157AC: foo::foo(std::basic_ofstream<char, std::char_traits<char> >&) (foo.cc:1743)
==4092== by 0x404F49: main (foo.cc:3159)
==4092== Address 0x6157d08 is 0 bytes after a block of size 8 alloc'd
==4092== at 0x4C29670: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==4092== by 0x40DB77: allocate (new_allocator.h:104)
==4092== by 0x40DB77: _M_allocate (stl_vector.h:168)
==4092== by 0x40DB77: void std::vector<std::string, std::allocator<std::string> >::_M_emplace_back_aux<std::string>(std::string&&) (vector.tcc:404)
==4092== by 0x408F3E: push_back (stl_vector.h:920)
==4092== by 0x408F3E: split(std::string const&, char, int) (foo.cc:416)
==4092== by 0x41577F: lustreLine::toPRV(std::basic_ofstream<char, std::char_traits<char> >&) (foo.cc:1741)
==4092== by 0x404F49: main (foo.cc:3159)
==4092==
==4092== Invalid read of size 4
==4092== at 0x4F09EBB: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) (in /usr/lib64/libstdc++.so.6.0.21)
==4092== by 0x40CCA9: construct<std::basic_string<char>, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&> (new_allocator.h:120)
==4092== by 0x40CCA9: _S_construct<std::basic_string<char>, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&> (alloc_traits.h:254)
==4092== by 0x40CCA9: construct<std::basic_string<char>, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&> (alloc_traits.h:393)
==4092== by 0x40CCA9: std::vector<std::string, std::allocator<std::string> >::push_back(std::string const&) (stl_vector.h:905)
==4092== by 0x4157AC: foo::foo(std::basic_ofstream<char, std::char_traits<char> >&) (foo.cc:1743)
==4092== by 0x404F49: main (foo.cc:3159)
==4092== Address 0xfffffffffffffff8 is not stack'd, malloc'd or (recently) free'd
==4092==
==4092==
==4092== Process terminating with default action of signal 11 (SIGSEGV)
==4092== Access not within mapped region at address 0xFFFFFFFFFFFFFFF8
==4092== at 0x4F09EBB: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) (in /usr/lib64/libstdc++.so.6.0.21)
==4092== by 0x40CCA9: construct<std::basic_string<char>, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&> (new_allocator.h:120)
==4092== by 0x40CCA9: _S_construct<std::basic_string<char>, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&> (alloc_traits.h:254)
==4092== by 0x40CCA9: construct<std::basic_string<char>, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&> (alloc_traits.h:393)
==4092== by 0x40CCA9: std::vector<std::string, std::allocator<std::string> >::push_back(std::string const&) (stl_vector.h:905)
==4092== by 0x4157AC: foo::foo(std::basic_ofstream<char, std::char_traits<char> >&) (fpp.cc:1743)
==4092== by 0x404F49: main (foo.cc:3159)
==4092== If you believe this happened as a result of a stack
==4092== overflow in your program's main thread (unlikely but
==4092== possible), you can try to increase the size of the
==4092== main thread stack using the --main-stacksize= flag.
==4092== The main thread stack size used in this run was 8388608.
[...]
( If more output is need just ask )
I've had to change some variable names for privacy, hope this is not a problem.
Thanks for reading and have a nice day!
The first for loop is wrong:
for (std::list<unsigned int>::iterator it=myMap[iD].begin(); it != myMap[iD].end(); ++it){
myMap[iD].erase(it);
}
because the iterator it becomes invalid after each call to erase.
You could re-write it as:
for (auto it = myMap[iD].begin(); it != myMap[iD].end(); )
{
it = myMap[iD].erase(it);
}
or better yet just
myMap[iD].clear();
There seem to be other for loops in your question where you make a similar mistake - in general you need to use the pattern:
for (auto it = foo.begin(); it != foo.end(); )
{
if (some_condition)
it = foo.erase(it); // erase map entry, update iterator
else
++it; // bump iterator
}
See the example on the cppreference.com entry for std::map::erase.
I am new to valgrind, so this could be missintepreting the output.
Valgrind seems to think bytes are being lost in the following lines:
if ( mysqlpp::StoreQueryResult result = query.store() ){
for ( size_t i = 0; i < result.num_rows(); ++i ){
hash = std::string( result[i]["hash"].data(), result[i]["hash"].length() );
label = std::string( result[i]["label"].data(), result[i]["label"].length() );
keywords = std::string( result[i]["keywords"].data(), result[i]["keywords"].length() );
task_raw = std::string( result[i]["task"].data(), result[i]["task"].length() );
solution_raw = std::string( result[i]["solution"].data(), result[i]["solution"].length() );
In fact it complains every time there is an std::string being constructed this way^^. I cannot figure out if this is a false-positive, if the way I'm copying is not the right way, or if the result is being leaked somehow.
Interestingly enough, it complains about another piece of code that doesn't use mysql++, but again a string constructor is used:
for ( auto & f : frames ){
for ( unsigned int i = 0; i < f.getTask().size(); i++ ){
if ( i + 1 < f.getTask().size() ){
auto t = f.getTask()[i].value();
auto n = f.getTask()[i+1].value();
pairs.push_back( std::string( t + n ) );
The line where a string is being constructed and pushed back, are possibly lost. Then a detailed output of malloc, operator new, std::string, etc are given.
The same story appears again, when copying a string from a std::istringstream:
std::copy( std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(), std::back_inserter<std::vector<std::string> >(tokens));
Are those false-positives? Does it have to do with using C++0x? Or are those legit leaks that may be originating from somewhere I have not yet detected to be leaking?
EDIT
Below is a sample of the reoccurring valgrind warning:
==18141== 9,555 bytes in 147 blocks are possibly lost in loss record 250 of 256
==18141== at 0x4C28BED: malloc (vg_replace_malloc.c:263)
==18141== by 0x7CA74C: operator new(unsigned long) (in trunk/Build/icarus)
==18141== by 0x7B4208: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /trunk/Build/icarus)
==18141== by 0x7B5904: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in trunk/Build/icarus)
==18141== by 0x7B5A7C: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, unsigned long, std::allocator<char> const&) (in trunk/Build/icarus)
==18141== by 0x610D65: Frame::mySQLloadFrameFromExample(std::string const&) (Frame.cpp:80)
==18141== by 0x6103FB: Frame::Frame(std::string const&) (Frame.cpp:5)
==18141== by 0x6054DB: Domain::mySQLloadFrames() (Domain.cpp:90)
==18141== by 0x60489D: Domain::Domain(std::string) (Domain.cpp:7)
==18141== by 0x628876: DomainController::loadDomainData() (DomainController.cpp:33)
==18141== by 0x628582: DomainController::DomainController() (DomainController.cpp:5)
==18141== by 0x627002: Controller::Controller() (Controller.cpp:26)
They are almost all identical, with the only difference being the actual line in code (i.e: the string constructor line).
The second type of warning is:
==18141== 3,562 bytes in 137 blocks are possibly lost in loss record 242 of 256
==18141== at 0x4C28BED: malloc (vg_replace_malloc.c:263)
==18141== by 0x7CA74C: operator new(unsigned long) (in trunk/Build/icarus)
==18141== by 0x7B4208: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in trunk/Build/icarus)
==18141== by 0x7B4EFA: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned long) (in trunk/Build/icarus)
==18141== by 0x7B4FDF: std::string::reserve(unsigned long) (in trunk/Build/icarus)
==18141== by 0x784F2F: std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (in trunk/Build/icarus)
==18141== by 0x61208E: std::istream_iterator<std::string, char, std::char_traits<char>, long>::_M_read() (stream_iterator.h:121)
==18141== by 0x613405: std::istream_iterator<std::string, char, std::char_traits<char>, long>::operator++() (stream_iterator.h:95)
==18141== by 0x6131BC: std::back_insert_iterator<std::vector<std::string, std::allocator<std::string> > > std::__copy_move<false, false, std::input_iterator_tag>::__copy_m<std::istream_iterator<std::string, char, std::char_traits<char>, long>, std::back_insert_iterator<std::vector<std::string, std::allocator<std::string> > > >(std::istream_iterator<std::string, char, std::char_traits<char>, long>, std::istream_iterator<std::string, char, std::char_traits<char>, long>, std::back_insert_iterator<std::vector<std::string, std::allocator<std::string> > >) (stl_algobase.h:298)
==18141== by 0x612D09: std::back_insert_iterator<std::vector<std::string, std::allocator<std::string> > > std::__copy_move_a<false, std::istream_iterator<std::string, char, std::char_traits<char>, long>, std::back_insert_iterator<std::vector<std::string, std::allocator<std::string> > > >(std::istream_iterator<std::string, char, std::char_traits<char>, long>, std::istream_iterator<std::string, char, std::char_traits<char>, long>, std::back_insert_iterator<std::vector<std::string, std::allocator<std::string> > >) (stl_algobase.h:384)
==18141== by 0x6121FD: std::back_insert_iterator<std::vector<std::string, std::allocator<std::string> > > std::__copy_move_a2<false, std::istream_iterator<std::string, char, std::char_traits<char>, long>, std::back_insert_iterator<std::vector<std::string, std::allocator<std::string> > > >(std::istream_iterator<std::string, char, std::char_traits<char>, long>, std::istream_iterator<std::string, char, std::char_traits<char>, long>, std::back_insert_iterator<std::vector<std::string, std::allocator<std::string> > >) (stl_algobase.h:422)
==18141== by 0x611C6A: std::back_insert_iterator<std::vector<std::string, std::allocator<std::string> > > std::copy<std::istream_iterator<std::string, char, std::char_traits<char>, long>, std::back_insert_iterator<std::vector<std::string, std::allocator<std::string> > > >(std::istream_iterator<std::string, char, std::char_traits<char>, long>, std::istream_iterator<std::string, char, std::char_traits<char>, long>, std::back_insert_iterator<std::vector<std::string, std::allocator<std::string> > >) (stl_algobase.h:454)
Which seems to be related with the std::istringstream code mentioned above.