I have one or two files that seems to be using boost::optional. However the error thrown is very unfriendly. I just don't know where to start looking to solve this error. It isn't pointing to a line of code that I have. Instead it is starting at optional.hpp which is a boost header file and this is the only message thrown.
/usr/include/boost/optional/optional.hpp:1191: boost::optional<T>::reference_type boost::optional<T>::get()
[with T = tev::events::EventItemCategory; boost::optional<T>::reference_type = tev::events::EventItemCategory&]: Assertion `this->is_initialized()' failed.
This is what I got from gdb:
(gdb) c
Continuing.
/usr/include/boost/optional/optional.hpp:1191: boost::optional<T>::reference_type boost::optional<T>::get() [with T = tev::events::EventItemCategory; boost::optional<T>::reference_type = tev::events::EventItemCategory&]:
Assertion `this->is_initialized()' failed.
Program received signal SIGABRT, Aborted.
0x0000007f9c105064 in ?? ()
(gdb) bt
#0 0x0000007f9c105064 in ?? ()
#1 0x0000007f9c13c898 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb) q
valgrind output
==32083== Warning: invalid file descriptor -1 in syscall close()
==32083== at 0x4C8A574: close (in /lib/libpthread-2.25.so)
==32083== by 0x4A24123: setFilename(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /opt/ad/lib/libconfiguration.so)
==32083== by 0x4A25A4F: (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char
==32083== by 0x4A31FFB: Configuration() (in /opt/ad/lib/libconfiguration.so)
==32083== by 0x5484DF: GetInstance (Singleton.h:80)
==32083== by 0x5484DF: RecordingFactory::RecordingFactory() (RecordingFactory.cpp:18)
==32083== by 0x56D3C7: GetInstance (Singleton.h:39)
==32083== by 0x56D3C7: Service(bool&, std::shared_ptr<tev::events::EventServiceResource> const&, int) (Service.cpp:59)
==32083== by 0x4D391B: ABCService (ABC.h:38)
==32083== by 0x4D391B: void create_service_thread<trc::Service>(std::vector<unsigned long, std::allocator<unsigned long> >&, std::vector<ABCServiceBase*, std::allocator<ABCServiceBase*> >&, bool&,
==32083== by 0x4B3DF7: main (ABC.cpp:140)
/arm/release/usr/include/boost/optional/optional.hpp:1191: boost::optional<T>::reference_type boost::optional<T>::get()
[with T = tev::events::EventItemCategory; boost::optional<T>::reference_type = tev::events::EventItemCategory&]: Assertion `this->is_initialized()' failed.
==32083==
==32083== Process terminating with default action of signal 6 (SIGABRT)
==32083== at 0x63BA064: raise (in /lib/libc-2.25.so)
==32083==
==32083== HEAP SUMMARY:
==32083== in use at exit: 8,217,174 bytes in 9,658 blocks
==32083== total heap usage: 59,869 allocs, 50,211 frees, 13,722,597 bytes allocated
==32083==
==32083== Searching for pointers to 9,658 not-freed blocks
==32083== Checked 104,459,168 bytes
==32083==
==32083== 8 bytes in 1 blocks are still reachable in loss record 1 of 2,795
==32083==
MSVP
#include<iostream>
#include <boost/optional.hpp>
using namespace std;
class Myclass
{
public:
int a;
};
boost::optional<Myclass> func(int a)
{
boost::optional<Myclass> value;
if(a == 0)
{
/* This results in the same runtime error discussed in the question */
value->a = {200};
/* The following #if 0 , if enabled would remove the error */
#if 0
Myclass c;
c.a = 200;
value = c;
#endif
}
return value;
}
int main(int argc, char **argv)
{
boost::optional<Myclass> v = func(0);
if(v)
std::cout << v -> a << std::endl;
else
std::cout << "Uninitialized" << std::endl;
std::cin.get();
return 0;
}