How to install boost on windows - c++

I am using Windows 8.1, visual studio community 2013.
I downloaded boost 1.59.
I then open Developer Command Prompt for VS2013, run bootstrap.bat, then run b2.exe.
All .lib files are placed under ./stage/lib/.
I set the c++ include path, and linker path. I built my program successfully and run under debug mode.
Here is the error message I get:
Unhandled exception at 0x77394598 in BoostStation.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x001BFD74.
Here is the break point:
throw enable_current_exception(enable_error_info(e)); // from throw_exception.hpp
Anyone knows how to solve the problem?
Another question, Are there any .dll files generated by this build and Where can I find them?
Here is my program:
MulticastSender.h
#include <boost/asio.hpp>
#include <boost/scoped_ptr.hpp>
#include <string>
class MulticastSender
{
public:
MulticastSender(const boost::asio::ip::address& multicast_addr, const unsigned short multicast_port)
: ep_(multicast_addr, multicast_port)
{
socket_.reset(new boost::asio::ip::udp::socket(svc_, ep_.protocol()));
}
~MulticastSender()
{
socket_.reset(NULL);
}
public:
void send_data(const std::string& msg)
{
socket_->send_to(boost::asio::buffer(msg), ep_);
}
private:
boost::asio::ip::udp::endpoint ep_;
boost::scoped_ptr<boost::asio::ip::udp::socket> socket_;
boost::asio::io_service svc_;
};
main.cpp
#include "stdafx.h"
#include "MulticastSender.h"
int _tmain(int argc, _TCHAR* argv[])
{
boost::asio::ip::address multiCastGroup;
multiCastGroup.from_string("192.168.32.1");
MulticastSender outDoor(multiCastGroup, 6000);
while (true)
{
outDoor.send_data("Hello");
Sleep(1000);
}
return 0;
}

Your boost installation is ok, because obviously you're able to compile and link a program that throws a boost::exception.
Catch the exception by wrapping your code in a try/catch block, then print out the message. I changed your main-function accordingly:
#include "stdafx.h"
#include "MulticastSender.h"
#include "boost/exception.hpp"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
try
{
boost::asio::ip::address multiCastGroup;
multiCastGroup.from_string("192.168.32.1");
MulticastSender outDoor(multiCastGroup, 6000);
while (true)
{
outDoor.send_data("Hello");
Sleep(1000);
}
}
catch (const std::exception& e)
{
std::cout << boost::diagnostic_information(e) << std::endl;
}
return 0;
}
This will catch the exception that is thrown by boost and print its message before the program exits.
You should also read up on exceptions in general: http://www.cplusplus.com/doc/tutorial/exceptions/

Related

C++: Application crashes with abort() on logging function

here is my code which is run on windows:
Log.h
#include <iostream>
#include <sstream>
#include <fstream>
#include <shared_mutex>
#include <thread>
#include <iomanip>
class Log {
public:
static std::ofstream myfile;
static std::shared_mutex m_smutex;
static bool isLogEnabled();
static void close() {
Log::myfile.close();
}
template< typename... Args >
static void debug(const char* format, Args... args) {
std::unique_lock<std::shared_mutex> lock(Log::m_smutex);
if (isLogEnabled()) {
if (!Log::myfile.is_open()) {
Log::myfile.open("C:\\tmp\\log.txt", std::ios_base::app);
}
...
Log::myfile.close();
}
else {
if (Log::myfile.is_open()) {
Log::myfile.flush();
Log::myfile.close();
}
}
}
};
You can see I've deleted most of the code(...) because I narrowed down the problem.
Log.cpp
#include <Windows.h>
#include "Log.h"
std::ofstream LogLine::myfile;
std::shared_mutex LogLine::m_smutex;
bool LogLine::isLogEnabled() {
CHAR regeditPath[MAX_PATH] = { 0 };
CHAR variable[] = "DEBUG_LOGS";
GetEnvironmentVariableA(variable, regeditPath, MAX_PATH);
return GetLastError() == ERROR_ENVVAR_NOT_FOUND ? false : true;
}
The process needs to be run always, that's why I open and close the file everytime I log something. I want to be able to delete logs even when the process is still running. Maybe this is silly because there might be a flag which would allow me to delete the logs while file is open by some process.
Example usage:
Log::debug("little test %d", 10);
As you can see I use mutex because I want to call debug function from different threads. The problem is that after some time I get error code Abort() has been called but I can't understand why.
P.S
I created this code inside custom Credential Provider to enable debugging, but I'm checking in the internet and it seems like nobody is using logging there. Is it because it's forbidden? Maybe this is reason of the crash?

How To Debug The Threw Exceptions From QThread?

Consider the following code(The system specification is mentioned down below):
#include <stdexcept>
#include <thread>
void f()
{
throw std::runtime_error("Something");
}
int main()
{
std::thread thr(f);
thr.join();
}
I easily could run it with a debugger and have the stack trace from where the exception threw. Like this:
But the same code fails if I use a QThread instead of std::thread:
#include <QCoreApplication>
#include <QThread>
void f()
{
throw std::runtime_error("Something");
}
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
auto const thr = QThread::create(f);
thr->start();
return QCoreApplication::exec();
}
Like this:
It's possible to debug it like std::thread? and have the stack frames from where the exception threw?
Environment:
OS: Fedora 35
Kernel: Linux 5.15.10
Compiler: GCC 11.2.1
Qt: 5.15.2
QtCreator: 6.0.1

Boost test setup error: memory access violation

I am getting the above error while running the executable after compiling and running the following file.
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/bind.hpp>
#include <boost/test/unit_test_log.hpp>
#include <boost/filesystem/fstream.hpp>
#include "index/DatabaseGroup.hpp"
using namespace boost::unit_test;
namespace indexing {
class ForwardDBTest {
/// A pointer to the database group object.
DatabaseGroup& databaseGroup;
std::string databaseName;
public:
~ForwardDBTest() {
}
;
ForwardDBTest(DatabaseGroup& databaseGroup_, std::string dbName) :
databaseGroup(databaseGroup_), databaseName(dbName) {
}
void boostTestCreateDB() {
databaseGroup.createDatabase(databaseName, databaseName);
}
};
class testSuites: public test_suite {
public:
testSuites() :
test_suite("test_suite") {
std::string db_location = "home/girijag/ripe/ripe_db";
std::cout << "hello" << std::endl;
int concurrency = 0;
std::string db_cache_policy = "AllMem";
boost::shared_ptr<DatabaseGroup> db = boost::shared_ptr<DatabaseGroup>(
new DatabaseGroup(db_location, concurrency, db_cache_policy));
std::string dbName = "DB1";
boost::shared_ptr<ForwardDBTest> instance(
new ForwardDBTest(*db, dbName));
test_case* boostTestCreateDB_test_case = BOOST_CLASS_TEST_CASE(
&ForwardDBTest::boostTestCreateDB, instance);
add(boostTestCreateDB_test_case);
}
~testSuites() {
}
;
};
test_suite* init_unit_test_suite(int argc, char** argv) {
test_suite* suite(BOOST_TEST_SUITE("Master Suite"));
suite->add(new testSuites());
return suite;
}
}'
Please let me know how should i resolve this?
i am getting errors as below:-
Test setup error: memory access violation at address: 0x00000021: no mapping at fault address
I have been struggling from past 2 days to figure out whats my issue
There are a number of disturbing things in the code, and some formatting seems have to been lost when posting the question, otherwise there is no chance it compiles. (For example, }’ ?!)
For starters, you shouldn’t place init_unit_test_suite(int, char**) in the indexing namespace, and subsequently there is no point in defining BOOST_TEST_MAIN - you will end up with multiple definition of the said init_unit_test_suite(int, char**) method.
In your case, the suite should be simply registered in the master test suite, there is no need to return a pointer to it from the method.
Here’s a minimal example that you can work with an extend for your purpose. It follows your structure, but omits non-relevant details:
#include <boost/test/included/unit_test.hpp>
#include <iostream>
using namespace boost::unit_test;
namespace indexing {
class ForwardDBTest {
public:
void boostTestCreateDB() { std::cout << __FUNCTION__ << std::endl; }
};
class TestSuite : public test_suite {
public:
TestSuite() : test_suite("test_suite") {
boost::shared_ptr<ForwardDBTest> instance(new ForwardDBTest);
add(BOOST_CLASS_TEST_CASE(&ForwardDBTest::boostTestCreateDB, instance));
}
};
} // namespace indexing
test_suite* init_unit_test_suite(int, char**) {
framework::master_test_suite().add(new indexing::TestSuite);
return 0;
}
/* Output:
Running 1 test case...
boostTestCreateDB
*** No errors detected
*/

Cpp Pantheios Log Library, Debug Assertion Failed Error

I have a cpp project, a cpp cli project and a c# win forms project.
I use pantheios log library in my cpp native project. When i try to write log, i take this error :
Here is my codes :
Log.hpp
#ifndef INCLUDE_LOG_HPP
#define INCLUDE_LOG_HPP
#define PANTHEIOS_NO_INCLUDE_OS_AND_3PTYLIB_STRING_ACCESS // Faster compilation
/* Pantheios Header Files */
#include <pantheios/pantheios.hpp> // Pantheios C++ main header
#include <pantheios/inserters/args.hpp> // for pantheios::args
#include <pantheios/backends/bec.file.h> // be.file header
#include "Include/utility.hpp"
/* Standard C/C++ Header Files */
#include <exception> // for std::exception
#include <new> // for std::bad_alloc
#include <string> // for std::string
#include <stdlib.h>
#include <sstream>
#define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
namespace Mtx
{
namespace log
{
class MTXMANAGER Logger
{
public:
void WriteLogIn(const std::string & log_text);
Logger();
~Logger();
};
}
}
#endif
Log.cpp
#include "Log.hpp"
namespace Mtx
{
namespace log
{
PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("mtx");//
Logger::Logger()
{
char path[MAX_PATH];
GetModuleFileName( NULL, path, MAX_PATH );
std::string::size_type pos = std::string( path ).find_last_of( "\\" );
strcpy(path,std::string( path ).substr( 0, pos).c_str());
std::strcat (path,"\\mtx-%D__.log");
/////
pantheios_be_file_setFilePath(PSTR(path), PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_SHARE_ON_WINDOWS, PANTHEIOS_BEID_ALL);
}
Logger::~Logger()
{
}
void Logger::WriteLogIn(const std::string & log_text)
{
pantheios::log_INFORMATIONAL(PSTR(" [1] "),PSTR(log_text));
}
}
}
I take the error at this line :
pantheios::log_INFORMATIONAL(PSTR(" [1] "),PSTR(log_text));
How can i fix this error?
I am afraid I don't have a direct answer for you, but comparing what I have in my solution (which is similar in many aspects with your setup - .NET DLL calling a C++-native DLL, which has Pantheios-logging), here is what I have:
I have a project LOG, which has an InitInstance() and ExitInstance() (and ctors for the CWinApp-derived class - CLogApp)
CLogApp ctor/dtor are empty
The code in InitInstance() and ExitInstance():
BOOL CLogApp::InitInstance()
{
CWinApp::InitInstance();
int panres = pantheios::pantheios_init();
if( panres < 0 )
{
OutputDebugStringA("Could not initialise the Pantheios logging libraries!\n");
util::onBailOut(pantheios::emergency, "Failed to initialise the Pantheios libraries", PANTHEIOS_FE_PROCESS_IDENTITY, /*pantheios::*/pantheios_getInitCodeString(panres));
return FALSE;
}
else
{
pantheios_be_file_setFilePath(CErrorHandler::getLogPath().c_str(), PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_LOCAL);
PANTHEIOS_TRACE_NOTICE("STARTING LOGGING");
}
return TRUE;
}
int CLogApp::ExitInstance()
{
PANTHEIOS_TRACE_NOTICE("STOPPING LOGGING");
pantheios_uninit();
return 0;
}
I am not sure if this will help, but this code has been working for me for many years now.

catching an exception in a loaded shared library

Is catching an exception thrown in a loaded shared library portable?
I've noticed that it works with dlfcn.h, but I wonder if this behaviour is in general expected, for example when using LoadLibrary on windows instead?
Example code:
main.cpp:
#include <stdexcept>
#include <cstdio>
#include <dlfcn.h>
typedef void(*dummy_t)();
int main()
{
dummy_t f;
void* handle;
handle = dlopen("module.so", RTLD_LAZY);
f = (dummy_t)dlsym(handle, "modulemain");
try
{
f();
}
catch(std::runtime_error& e)
{
fprintf(stderr, "caught exception: %s\n", e.what());
}
dlclose(handle);
}
module.cpp:
#include <stdexcept>
extern "C" void modulemain()
{
throw std::runtime_error("some terrible error occured");
}
Yes, that should work fine under Windows.