Crash when replacing std::map value - c++

I have this map
std::map<IPv4Address,std::vector<Agent>> report;
Where agent is defined as the following
typedef struct
{
IPv4Address Address;
AgentType Type;
std::map <IPv4Address, IPRoute> RoutingTable;
MetricType metric;
int UpdateReceived = 0;
}Agent;
I am sending this Agent struct through tcp sockets and saving the values in the report std::map
int receive = recv(as.socket, (void *) &agent, sizeof(agent),0);
The routing table is initially empty. When the routing table size becomes >=1 the app crashes when adding to the map as seen below:
mutex.lock();
PrintInfo("Mutex locked");
if(report.find(as.ip) != report.end())
{
//f tells us if the agent was connected before to the router
bool f = false;
std::vector<Agent> tmpv =report[as.ip];
int tmp;
PrintInfo("Vector loop");
for(std::size_t i=0 ; i < tmpv.size() ; i++)
{
if(tmpv[i].Type == agent.Type)
{
f = true;
tmp = i;
break;
}
}
PrintInfo("Vector loop End");
if(f)
{
PrintInfo("Found -> Replacing");
--> This line crashes report[as.ip][tmp] = agent;
}
else
{
PrintInfo("Not Found -> Adding");
report[as.ip].push_back(agent);
}
PrintInfo("After add");
}

After serializing the struct using boost library everything worked fine.
Here is an example of serialization :
Struct :
typedef struct
{
template<class Archive>
void serialize(Archive &ar,const unsigned int version)
{
ar & Address & Type & InterceptionDuration & RoutingTable & metric & UpdateReceived;
}
IPv4Address Address;
AgentType Type;
double InterceptionDuration;
std::map <IPv4Address, IPRoute> RoutingTable;
MetricType metric;
int UpdateReceived = 0;
}Agent;
Serializing :
std::string SerializeAgent(Agent a)
{
std::ostringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream);
archive << a;
std::string s = archive_stream.str();
return s;
}
Deserializing :
Agent DeserializeAgent(std::string s)
{
Agent a;
std::string r(&s[0],s.size());
std::istringstream archive_stream(r);
boost::archive::text_iarchive archive(archive_stream);
archive >> a;
return a;
}
Sending through socket :
std::string s = SerializeAgent(agent);
send(reporterSendingSocket,s.c_str(),s.size(),0);
Receiving through socket :
std::vector<char> response(REPORT_MAX_BUFFER_SIZE);
int receive = recv(as.socket, (void *) &response[0], response.size(),0);

Related

Should be a virtual destructor? But how?

A program that stores a phone company's consumers data in a linked list. At the end it displays the bill for each human. I have the following codes:
class BaseTypeOfContract
{
private:
int minutePrice;
int SMSPrice;
public:
void setminutePrice(int x) { minutePrice = x; }
void setSMSPrice(int x) { SMSPrice = x; }
virtual int calculateBill(int talkedMinutes, int sentSMS) = 0;
int getminutePrice() const { return minutePrice; }
int getSMSPrice() const { return SMSPrice; }
};
class SMSBaseType : public BaseTypeOfContract
{
private:
int freeSMS;
public:
SMSBaseType(int minutePrice, int SMSPrice, int freeSMS)
{
setminutePrice(minutePrice);
setSMSPrice(SMSPrice);
setfreeSMS(freeSMS);
}
public:
void setfreeSMS(int free) { this->freeSMS = free; }
virtual int calculateBill(int talkedMinutes, int sentSMS)
{
int billedSMS = (freeSMS > sentSMS) ? 0 : sentSMS - freeSMS;
return talkedMinutes * getminutePrice() + billedSMS * getSMSPrice();
}
};
class Base : public BaseTypeOfContract
{
public:
Base()
{
setminutePrice(30);
setSMSPrice(10);
}
virtual int calculateBill(int talkedMinutes, int sentSMS) { return talkedMinutes * getminutePrice() + sentSMS * getSMSPrice();}
};
class SMSMax : public SMSBaseType
{
public:
SMSMax() : SMSBaseType(20, 5, 150) {}
};
class MobiNET: public SMSBaseType
{
public:
MobiNET() : SMSBaseType(10, 15, 25) {}
};
Client's class:
class Client
{
public:
std::string name;
std::string phoneNumber;
BaseTypeOfContract* typeOfContract;
int talkedMinutes;
int sentSMS;
Client *next;
public:
Client(){}
Client(std::string n, std::string p, int bp, int ks) : name(n), phoneNumber(p), talkedMinutes(bp), sentSMS(ks) {}
void preSetPlan(std::string s)
{
if (s == "MobiNET")
this->typeOfContract = new MobiNET();
else if (s == "SMSMax")
this->typeOfContract = new SMSMax();
else this->typeOfContract = new Base();
}
std::string getname() const { return name; }
std::string getphoneNumber() const { return phoneNumber; }
void setname(std::string n) { name = n; }
void setphoneNumber(std::string pn) { phoneNumber = pn; }
void settalkedMinutes(int bp) { talkedMinutes = bp; }
void setsentSMS(int SSMS) { sentSMS = SSMS; }
int getBill() const { return this->typeOfContract->calculateBill(talkedMinutes, sentSMS); }
};
I read the data from 2 files. First file contains the name, phone number, type of contract. Second file contains the phone number, talked minutes and sent SMS.
Client* file_read_in()
{
std::ifstream ClientData;
ClientData.open("clients.txt");
Client *first = new Client;
first = NULL;
while (!ClientData.eof())
{
std::string name, phoneNumber, typeOfContract;
ClientData >> name;
ClientData >> phoneNumber;
ClientData >> typeOfContract;
std::ifstream ClientTalkedSent;
ClientTalkedSent.open("used.txt");
while(!ClientTalkedSent.eof())
{
std::string phoneNumber2;
ClientTalkedSent >> phoneNumber2;
if (phoneNumber2 == phoneNumber)
{
int talkedMinutes, sentSMS;
ClientTalkedSent >> talkedMinutes;
ClientTalkedSent >> sentSMS;
Client* tmp = new Client(name, phoneNumber, talkedMinutes, sentSMS);
tmp->preSetPlan(typeOfContract);
tmp->next = NULL;
if (first == NULL)
{
first = tmp;
}
else
{
Client *cond = first;
while (cond->next != NULL) cond = cond->next;
cond->next = tmp;
}
}
}
ClientTalkedSent.close();
}
ClientData.close();
return first;
}
And the main:
int main()
{
Client* first = file_read_in();
while(first != NULL)
{
std::cout << first->getname() << " " << first->getphoneNumber() << " " << first->getBill() << std::endl;
first = first->next;
}
return 0;
}
My problem that I should free the allocated memory but I got on idea how. Which class' destructor should do the dirty job. I would appreciate if someone could use my code, to show how the "destructor inheritance" works.
Sorry for my bad english and thanks for the help. This site helped me alot of times, but for this problem I did not find a solution.
If you have a pointer BaseTypeOfContract* typeOfContract; that is used to point to different derived classes, then BaseTypeOfContract needs to have a virtual destructor for delete typeOfContract to work.
And as Client seems to create the objects pointed to, it also ought to be responsible for cleaning them up. Either by using delete typeOfContract; in its destructor, or by storing a smart pointer to get the work done automatically.
The other part is that each Client stores a pointer to the next Client. That seems like not the best design. In real life it is not at all like each person knowing who is the next person that buys a cell phone in the same store. :-)
You would be much better of with a container, like std::vector<Client>, that would also handle the lifetime of the Client objects.

STXXL Dealing with strings

i am dealing with large dataset. May i ask you how it is possible to store strings in the classes i want to use with stxxl? I have read several discussions and everywhere was said that string is not POD type therefore it cannot be stored in the stxxl::vector, but i am not sure,because i tried it and i checked the data and everything seems to be fine. i have also saw an approach here https://github.com/Project-OSRM/osrm-backend/blob/725b93a961625a7b04d54806d7e0f80252a6bcd0/extractor/extraction_containers.hpp and they use stxxl::vector, so maybe the library got updated to support std::string?
class HighWay
{
private:
uint64_t id;
string name;
int speed;
string attributes; //additional attributes of way
string edges; //written uint64_t from,uint64_t to, int distance written as string
string nodes; //vector<uint64_t> written as string
public:
HighWay() = default;
void setId(uint64_t _id) {
id = boost::lexical_cast<string>(_id);
}
void setName(string _name) {
name = _name;
}
void setSpeed(int _speed) {
speed = _speed;
}
void setAttributes(string _attributes) {
attributes = _attributes;
}
void setEdges(string _edges) {
edges = _edges;
}
void setNodes(vector<uint64_t>refs) {
stringstream s;
uint64_t i = 0;
for (; i < refs.size()-1;i++) {
s << boost::lexical_cast<uint64_t>(refs[i]) << " ";
}
s << boost::lexical_cast<uint64_t>(refs[i]);
nodes = s.str();
}
uint64_t getId() {
return id;
}
string getName() {
return name;
}
int getSpeed() {
return speed;
}
string getAttributes() {
return attributes;
}
string getEdges() {
return edges;
}
std::vector<int64_t> getNodes() {
stringstream s(nodes);
uint64_t node;
std::vector<int64_t> result;
while (s >> node) {
result.push_back(static_cast<int64_t>(node));
}
return result;
}
};
I have also created code which stores the strings as POD,storing the string in vector of char and in map remembering lower and upper bound index in the array. But this approach leads to many std::maps used in the application.
//class to store in map
struct TypeName{
uint64_t start;
uint64_t end;
};
std::istream& operator >> (std::istream& i, TypeName& entry)
{
i >> entry.start;
i >> entry.end;
return i;
}
std::ostream& operator << (std::ostream& i, const TypeName& entry)
{
i << entry.start << " ";
i << entry.end;
return i;
}
struct PoiCategories{
uint64_t start;
uint64_t end;
};
std::istream& operator >> (std::istream& i,PoiCategories& entry)
{
i >> entry.start;
i >> entry.end;
return i;
}
std::ostream& operator << (std::ostream& i, const PoiCategories& entry)
{
i << entry.start << " ";
i << entry.end;
return i;
}
//object i want to store
struct Poi {
Poi() = default;
uint64_t id;
char type;
uint64_t id_in_pois; //id in vector pois
void addCategories(
vector<int> &kats, //categories to insert
stxxl::vector<uint64_t> &categories, //vector to store category
std::unordered_map <uint64_t, PoiCategories> &idPoi_categories //index to vector categories to retrieve all categories for Poi
)
{
size_t start = categories.size();
for (auto & kat : kats) {
categories.push_back(kat);
}
size_t end = categories.size() - 1;
idPoi_categories.insert(make_pair(id, PoiCategories{start, end }));
}
vector<int> getCategories(
stxxl::vector<uint64_t> &categories,
std::unordered_map <uint64_t, PoiKategorie> &idPoi_categories
)
{
std::vector<int> result;
PoiCategories bounds = idPoi_categories.find(id)->second;
for (size_t i = bounds.start; i <= bounds.end; i++) {
result.push_back(categories[i]);
}
return result;
}
};
Problem in my application is that i am storing a few string data, which are mainly names of streets and POIs. Maybe i am using wrong library. If so,can you recommend me a better approach to store data while preprocessing?
It's indeed banned, but the symptoms of violating the no-POD rule are generally unpredictable. It may very well appear to work as long as the strings all fit in memory, but in that case you didn't need the STXXL anyway.

Maximum Size that a Boost Message Queue can Handle?

What is the maximum size a Boost Message Queue can Handle?
When I am sending and receiving the below structure by using Boost binary serialization it is working fine.
typedef struct {
int msg_type;
char msg_name[100];
union {
struct {
int ID;
std::string ReportedTime;
char ReceivedAt[200];
int Number;
int Priority;
} mess1;
struct {
char host_ip[20];
char mac_addr[30];
char time_stamp[100];
} mess2;
} struct_type;
}msg_struct;
#include <boost/serialization/is_bitwise_serializable.hpp>
BOOST_IS_BITWISE_SERIALIZABLE(msg_struct)
But when I making the size of ReceivedAt[2000] to 2000 or adding a new char array variable.
It is throwing the following exception and the core is getting dumped.
terminate called after throwing an instance of 'boost::interprocess::interprocess_exception'
what(): boost::interprocess_exception::library_error
Aborted (core dumped)
Edit
Send
int ControlQueue::pushMessage(msg_struct* msg)
{
int q_size = vamsgq->get_num_msg();
int retVal = 0;
lockQueue();
//imp->ID=1;
//strcpy(imp->ReceivedAt,"10-07-14");
std::stringstream oss;
boost::archive::binary_oarchive oa(oss);
strncpy(msg->msg_name,"msg_name",sizeof(msg->msg_name));
oa << boost::serialization::make_array(msg, 1);
std::string serialized_string(oss.str());
vamsgq->send(serialized_string.data(), serialized_string.size(), 0);
std::cout <<"\n sendig type="<< msg->msg_name << std::endl;
if((retVal = pthread_cond_signal(&m_qCondSignal)) != 0)
{
cout<<__FILE__<<__LINE__<<
"{ControlQueue %x} Unable to send Cond Signal",this);
}
unlockQueue();
return 1;
}
Recieve:
msg_struct* ControlQueue::getMsg()
{
int retVal = 0;
message_queue::size_type recvd_size;
unsigned int priority;
lockQueue();
while(vamsgq->get_num_msg()==0)
{
if((retVal = pthread_cond_wait(&m_qCondSignal, &m_qMutex)) != 0)
{
cout<<__FILE__<<__LINE__<<"getMsg {ControlQueue } Unable to Cond Signal";
unlockQueue();
return NULL;
}
}
msg_struct *l_msg_struct = NULL;
if(vamsgq->get_num_msg())
{
l_msg_struct=new msg_struct();
std::stringstream iss;
std::string serialized_string;
serialized_string.resize(MAX_SIZE);
vamsgq->receive(&serialized_string[0], MAX_SIZE, recvd_size, priority);
iss << serialized_string;
boost::archive::binary_iarchive ia(iss);
ia >> boost::serialization::make_array(l_msg_struct, 1);
std::cout <<"Recieving="<< l_msg_struct->msg_name << std::endl;
}
else
{
cout<<__FILE__<<__LINE__<<"getMsg {ControlQueue } m_MsgQ empty..";
}
unlockQueue();
return l_msg_struct;
}
The Send and Receive both are running in different threads the only problem what I am getting is after increasing the size of structure or adding an char array variable.
And the exception is getting thrown by the vamsgq->send() method, Creation method(message queue) is working fine. And I am also increasing the size of Message that message queue is going to store.
Is there any complete documentation online for the boost::message_queue.
You broke the requirement for
BOOST_IS_BITWISE_SERIALIZABLE(msg_struct)
by no longer keeping the msg_struct a POD type:
static_assert(boost::is_pod<msg_struct>::value, "msg_struct must be POD");
will fail to compile. (In fact, with the msg_struct shown using std::string, I think default construction should fail to compile, so the new msg_struct() line in your example confuses me).
Also you don't show the construction of the message-queue, so I can't tell how you determine the maximum message size. This might be inadequately dimensioned. See below for two approaches that use/check the size limitations.
Here's what would work:
[Solution A] All POD data, no serialization
#include <boost/serialization/serialization.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
typedef struct {
int msg_type;
char msg_name[100];
union {
struct {
int ID;
char ReportedTime[100];
char ReceivedAt[2000];
int Number;
int Priority;
} mess1;
struct {
char host_ip[20];
char mac_addr[30];
char time_stamp[100];
} mess2;
} struct_type;
} msg_struct;
static_assert(boost::is_pod<msg_struct>::value, "msg_struct must be POD");
#include <boost/serialization/is_bitwise_serializable.hpp>
BOOST_IS_BITWISE_SERIALIZABLE(msg_struct)
namespace ipc = boost::interprocess;
int main() {
ipc::message_queue queue(ipc::open_or_create, "myqueue", 100, sizeof(msg_struct));
msg_struct outgoing;
outgoing.msg_type = 1;
strncpy(outgoing.msg_name, "outgoing.msg_name", sizeof(outgoing.msg_name));
outgoing.struct_type.mess1.ID = 42;
strncpy(outgoing.struct_type.mess1.ReportedTime, "outgoing.struct_type.mess1.ReportedTime", sizeof(outgoing.struct_type.mess1.ReportedTime));
strncpy(outgoing.struct_type.mess1.ReceivedAt, "outgoing.struct_type.mess1.ReceivedAt", sizeof(outgoing.struct_type.mess1.ReceivedAt));
outgoing.struct_type.mess1.Number = 123;
outgoing.struct_type.mess1.Priority = 234;
queue.send(&outgoing, sizeof(outgoing), 1);
}
As you can see, this is not using Boost Serialization because the struct is POD anyways.
[Solution B] Highlevel C++ types, using binary serialization
Alternatively, you can use Boost Serialization all the way and check that the message size doesn't exceed the maximum message size when sending:
#include <boost/serialization/serialization.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/variant.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
typedef struct {
int msg_type;
std::string msg_name;
struct mess1_t {
int ID;
std::string ReportedTime;
std::string ReceivedAt;
int Number;
int Priority;
private:
friend class boost::serialization::access;
template <typename Ar>
void serialize(Ar& ar, unsigned)
{
ar & ID;
ar & ReportedTime;
ar & ReceivedAt;
ar & Number;
ar & Priority;
}
};
struct mess2_t {
std::string host_ip;
std::string mac_addr;
std::string time_stamp;
private:
friend class boost::serialization::access;
template <typename Ar>
void serialize(Ar& ar, unsigned)
{
ar & host_ip;
ar & mac_addr;
ar & time_stamp;
}
};
boost::variant<mess1_t, mess2_t> message_data;
private:
friend class boost::serialization::access;
template <typename Ar>
void serialize(Ar& ar, unsigned)
{
ar & msg_type;
ar & msg_name;
ar & message_data;
}
} msg_struct;
namespace ipc = boost::interprocess;
int main() {
ipc::message_queue queue(ipc::open_or_create, "myqueue", 100, 4*1024);
msg_struct outgoing { 1, "outgoing.msg_name", msg_struct::mess1_t {
42,
"outgoing.struct_type.mess1.ReportedTime",
"outgoing.struct_type.mess1.ReceivedAt",
123,
234 }
};
std::ostringstream oss;
boost::archive::binary_oarchive oa(oss);
oa << outgoing;
assert(oss.str().size() <= queue.get_max_msg_size());
queue.send(&outgoing, sizeof(outgoing), 1);
}

Classes and Queue's

I am trying to figure out how to work with Queues and Classes.
How can i insert information into this class with a queue?
I created the queue queue<Processes> PrinterDevices
How do i go about insert stuff on this queue into a class or reading from it?
class Processes
{
public:
void setPID (int a)
{
PID = a;
}
int retrievePID()
{
return PID;
}
void setFilename (string input)
{
Filename = input;
}
string retrieveFilename()
{
return Filename;
}
void setMemstart (int a)
{
Memstart = a;
}
int retrieveMemstart()
{
return Memstart;
}
void setRW (char a)
{
rw = a;
}
int retrieveRW()
{
return rw;
}
void setFilelength (string input)
{
Filelength = input;
}
string retrieveFilelength()
{
return Filelength;
}
private:
int PID;
string Filename;
int Memstart;
char rw;
string Filelength;
};
queue<Processes> PrinterDevices;
Processess obj;
//Populate object through setter methods
To add this object to queue PrinterDevices
`PrinterDevices.push(obj);`
Similarly you can create more object..
To traverse...
while(!PrinterDevices.empty())
{
Processes obj = PrinterDevices.front();
//Add code to use obj;
PrinterDevices.pop();//Remove the object from queue which is already used above
}

C++ Class Inheritance

I have a question about inheritance. From this source:
gSpan.h
struct Edge {
int from;
int to;
int elabel;
unsigned int id;
Edge(): from(0), to(0), elabel(0), id(0) {};
};
class Vertex
{
public:
typedef std::vector<Edge>::iterator edge_iterator;
int label;
std::vector<Edge> edge;
void push (int from, int to, int elabel) //elabel代表edge label
{
edge.resize (edge.size()+1);
edge[edge.size()-1].from = from;
edge[edge.size()-1].to = to;
edge[edge.size()-1].elabel = elabel;
return;
}
};
class Graph: public std::vector<Vertex> {
public:
typedef std::vector<Vertex>::iterator vertex_iterator;
Graph (bool _directed)
{
directed = _directed;
};
bool directed;
Graph(): edge_size_(0), directed(false) {};
};
gSpan.cpp
std::istream &gSpan::read (std::istream &is)
{
Graph g(directed);
while (true) {
g.read (is);
if (g.empty()) break;
TRANS.push_back (g);
}
return is;
}
graph.cpp
std::istream &Graph::read (std::istream &is)
{
std::vector <std::string> result;
char line[1024];
while (true) {
if (result.empty()) {
// do nothing
} else if (result[0] == "t") {
...
} else if (result[0] == "v" && result.size() >= 3) {
unsigned int id = atoi (result[1].c_str());
this->resize (id + 1);
(*this)[id].label = atoi (result[2].c_str());
...
Why we can use (*this)[id].label in graph.cpp? (*this) is a Graph object.... if we want to use (*this)[id].label shouldn't we have to declare std::vector<Vertex>?
class Graph: public std::vector<Vertex>
This means that the Graph class inherited std::vector<Vertex>, so you can do anything with it that you'd be able to do if it were a vector. And so by creating an instance of Graph, you're effectively declaring a new vector.