reo7sp / tgbot-cpp

C++ library for Telegram bot API
http://reo7sp.github.io/tgbot-cpp
MIT License
986 stars 237 forks source link

TgBot::api giving segmentation fault when ever function is called on it. #204

Open moldenha opened 2 years ago

moldenha commented 2 years ago

I am compiling my program using the command g++ -std=c++17 main.cpp -lcurl -lwebsockets -lTgBot -lboost_system -lssl -lcrypto -lpthread -lboost_thread -ldl -o main. I am converting it from my mac to an SBC running ubuntu minimal. It compiles fine and there are no reference errors. However, when I run the code below on the mac, it works just fine, but on ubuntu it fails.

std::cout<<"checking getMe"<<std::endl;
if(bot.getApi().getMe()){
   std::cout<<"was not null"<<std::endl;
   std::cout<<"Bot username: "<< bot.getApi().getMe()->username<<std::endl;
}
else{
   std::cout<<"This returned null"<<std::endl;
}

The output on the mac:

checking getMe
was not null
Bot username: *the username*

On the SBC running ubuntu minimal:

checking getMe
Segmentation fault

The function bot.getApi().getMe() returns ‘TgBot::User::Ptr’ {aka ‘class std::shared_ptr<TgBot::User>’}. The documentation says to use c++14, but because of other parts of my code I have to use c++17, and I am using a newer version of gcc on the SBC. Could either one of those be the reason it is giving a segmentation fault? I don't see how the c++ version could be it though because c++17 just builds on c++14.

After some more testing, trying to narrow down the problem the function getApi() returns const TgBot::Api & So the code below does this:

std::cout<<"calling getApi"<<std::endl;
const TgBot::Api & cu_api = bot.getApi();
std::cout<<"got api"<<std::endl;
std::vector< TgBot::BotCommand::Ptr > m_commands = cu_api.getMyCommands();
std::cout<<"got commands"<<std::endl; 

returns

calling getApi
got api
Segmentation fault

If I run it this way:

std::cout<<"calling getApi"<<std::endl;
TgBot::Api cu_api = bot.getApi();
std::cout<<"got api"<<std::endl;
if(cu_api.getMe()){
     std::cout<<"was not null"<<std::endl;
     std::cout<<"Bot username: "<<cu_api.getMe()>username<<std::endl;
}

returns

calling getApi
got api
Segmentation fault

Which leads me to believe there is a problem with TgBot::Api clearly. What I don't understand, is when these are installed the same way on a mac, and run with the same version of c++, why does it fail on ubuntu minimal and not a mac? After running it through gdb I have found this segment fault in boost:

Thread 1 "main" received signal SIGSEGV, Segmentation fault.
0x00000055555cbc58 in boost::asio::io_context::basic_executor_type<std::allocator<void>, 0u>::basic_executor_type (this=0x7f7f7f7f7f7f7f7f, i=...) at /usr/include/boost/asio/io_context.hpp:1126
1126          bits_(0)

When upgrading boost to version 1.78.0

Thread 1 "main" received signal SIGSEGV, Segmentation fault.
0x00000055555ced0c in boost::asio::io_context::basic_executor_type<std::allocator<void>, 0ul>::basic_executor_type (this=0x7f7f7f7f7f7f7f7f, i=...) at /usr/include/boost/asio/io_context.hpp:1124
1124          target_(reinterpret_cast<uintptr_t>(&i))

Is there a way to fix this?

mrXCray commented 2 years ago

Compile lib as a static lib and link it to your app, so there will be no difference between recommended compiler/language version and those you are using in your app