diff options
author | George Hazan <george.hazan@gmail.com> | 2016-05-13 16:19:44 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2016-05-13 16:19:44 +0000 |
commit | 79030d54a0f7f1cc1556ae1d34e3ce4b730ecc77 (patch) | |
tree | fa235efeec2f3d11fef169c1b67368508e6c84c4 /protocols/FacebookRM | |
parent | 2257c9ecd7d0bf1af7c7421c443a3e57a10fc181 (diff) |
- memory leak fixed;
- manual memory management replaced with the automated one
git-svn-id: http://svn.miranda-ng.org/main/trunk@16822 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/FacebookRM')
-rw-r--r-- | protocols/FacebookRM/src/json.cpp | 102 | ||||
-rw-r--r-- | protocols/FacebookRM/src/json.h | 4 | ||||
-rw-r--r-- | protocols/FacebookRM/src/process.cpp | 101 | ||||
-rw-r--r-- | protocols/FacebookRM/src/proto.h | 2 |
4 files changed, 97 insertions, 112 deletions
diff --git a/protocols/FacebookRM/src/json.cpp b/protocols/FacebookRM/src/json.cpp index 7250db8f14..901b28ada6 100644 --- a/protocols/FacebookRM/src/json.cpp +++ b/protocols/FacebookRM/src/json.cpp @@ -396,7 +396,7 @@ void parseAttachments(FacebookProto *proto, std::string *message_text, const JSO } } -int facebook_json_parser::parse_messages(std::string *pData, std::vector< facebook_message* >* messages, std::map< std::string, facebook_notification* >* notifications) +int facebook_json_parser::parse_messages(std::string *pData, std::vector<facebook_message>* messages, std::map< std::string, facebook_notification* >* notifications) { // remove old received messages from map for (std::map<std::string, int>::iterator it = proto->facy.messages_ignore.begin(); it != proto->facy.messages_ignore.end();) { @@ -466,16 +466,15 @@ int facebook_json_parser::parse_messages(std::string *pData, std::vector< facebo message_text = utils::text::trim(message_text, true); - facebook_message* message = new facebook_message(); - message->isChat = other_user_id.empty(); - message->isIncoming = (id != proto->facy.self_.user_id); - message->isUnread = message->isIncoming; - message->message_text = message_text; - message->time = utils::time::from_string(timestamp.as_string()); - message->user_id = message->isChat ? id : other_user_id; - message->message_id = message_id; - message->thread_id = thread_id; - + facebook_message message; + message.isChat = other_user_id.empty(); + message.isIncoming = (id != proto->facy.self_.user_id); + message.isUnread = message.isIncoming; + message.message_text = message_text; + message.time = utils::time::from_string(timestamp.as_string()); + message.user_id = message.isChat ? id : other_user_id; + message.message_id = message_id; + message.thread_id = thread_id; messages->push_back(message); } else if (cls == "ReadReceipt") { @@ -572,21 +571,21 @@ int facebook_json_parser::parse_messages(std::string *pData, std::vector< facebo message_text = utils::text::trim(message_text, true); - facebook_message* message = new facebook_message(); - message->isChat = other_user_id.empty(); - message->isIncoming = (id != proto->facy.self_.user_id); - message->isUnread = message->isIncoming; - message->message_text = message_text; - message->time = utils::time::from_string(timestamp.as_string()); - message->user_id = (!message->isChat && !message->isIncoming) ? other_user_id : id; - message->message_id = message_id; - message->thread_id = thread_id; - - if (message->user_id.empty()) { - proto->debugLogA(" !!! JSON: deliver message event with empty user_id (thread_id %s)\n%s", message->thread_id.empty() ? "empty too" : "exists", (*it).as_string().c_str()); - - if (!message->thread_id.empty()) { - message->user_id = proto->ThreadIDToContactID(message->thread_id); // TODO: Check if we have contact with this user_id in friendlist and otherwise do something different? + facebook_message message; + message.isChat = other_user_id.empty(); + message.isIncoming = (id != proto->facy.self_.user_id); + message.isUnread = message.isIncoming; + message.message_text = message_text; + message.time = utils::time::from_string(timestamp.as_string()); + message.user_id = (!message.isChat && !message.isIncoming) ? other_user_id : id; + message.message_id = message_id; + message.thread_id = thread_id; + + if (message.user_id.empty()) { + proto->debugLogA(" !!! JSON: deliver message event with empty user_id (thread_id %s)\n%s", message.thread_id.empty() ? "empty too" : "exists", (*it).as_string().c_str()); + + if (!message.thread_id.empty()) { + message.user_id = proto->ThreadIDToContactID(message.thread_id); // TODO: Check if we have contact with this user_id in friendlist and otherwise do something different? } } @@ -856,17 +855,16 @@ int facebook_json_parser::parse_messages(std::string *pData, std::vector< facebo std::string id = action_["other_user_fbid"].as_string(); std::string message_id = action_["message_id"].as_string(); - facebook_message* message = new facebook_message(); - message->isChat = false; - message->isUnread = true; - message->isIncoming = (id != proto->facy.self_.user_id); - message->message_text = message_text; - message->time = utils::time::from_string(action_["timestamp"].as_string()); - message->user_id = id; - message->message_id = message_id; - message->thread_id = thread_id; - message->type = CALL; - + facebook_message message; + message.isChat = false; + message.isUnread = true; + message.isIncoming = (id != proto->facy.self_.user_id); + message.message_text = message_text; + message.time = utils::time::from_string(action_["timestamp"].as_string()); + message.user_id = id; + message.message_id = message_id; + message.thread_id = thread_id; + message.type = CALL; messages->push_back(message); } else { @@ -930,7 +928,7 @@ int facebook_json_parser::parse_unread_threads(std::string *data, std::vector< s return EXIT_SUCCESS; } -int facebook_json_parser::parse_thread_messages(std::string *data, std::vector< facebook_message* >* messages, std::map< std::string, facebook_chatroom* >* chatrooms, bool unreadOnly) +int facebook_json_parser::parse_thread_messages(std::string *data, std::vector< facebook_message >* messages, std::map< std::string, facebook_chatroom* >* chatrooms, bool unreadOnly) { std::string jsonData = data->substr(9); @@ -1039,31 +1037,29 @@ int facebook_json_parser::parse_thread_messages(std::string *data, std::vector< if (unreadOnly && !isUnread) continue; - facebook_message* message = new facebook_message(); - message->message_text = message_text; - message->time = utils::time::from_string(timestamp.as_string()); - message->thread_id = thread_id; - message->message_id = message_id; - message->isIncoming = (author_id != proto->facy.self_.user_id); - message->isUnread = isUnread; + facebook_message message; + message.message_text = message_text; + message.time = utils::time::from_string(timestamp.as_string()); + message.thread_id = thread_id; + message.message_id = message_id; + message.isIncoming = (author_id != proto->facy.self_.user_id); + message.isUnread = isUnread; if (chatrooms->find(thread_id) != chatrooms->end()) { // this is chatroom message - message->isChat = true; - message->user_id = author_id; + message.isChat = true; + message.user_id = author_id; } else { // this is standard message - message->isChat = false; + message.isChat = false; auto iter = thread_ids.find(thread_id); if (iter != thread_ids.end()) - message->user_id = iter->second; // TODO: Check if we have contact with this ID in friendlist and otherwise do something different? + message.user_id = iter->second; // TODO: Check if we have contact with this ID in friendlist and otherwise do something different? else if (!other_user_id.empty()) - message->user_id = other_user_id; - else { - delete message; + message.user_id = other_user_id; + else continue; - } } messages->push_back(message); diff --git a/protocols/FacebookRM/src/json.h b/protocols/FacebookRM/src/json.h index c49ef65193..b900487e36 100644 --- a/protocols/FacebookRM/src/json.h +++ b/protocols/FacebookRM/src/json.h @@ -33,9 +33,9 @@ public: int parse_buddy_list(std::string*, List::List< facebook_user >*);
int parse_friends(std::string*, std::map< std::string, facebook_user* >*);
int parse_notifications(std::string*, std::map< std::string, facebook_notification* >*);
- int parse_messages(std::string*, std::vector< facebook_message* >*, std::map< std::string, facebook_notification* >*);
+ int parse_messages(std::string*, std::vector< facebook_message >*, std::map< std::string, facebook_notification* >*);
int parse_unread_threads(std::string*, std::vector< std::string >*);
- int parse_thread_messages(std::string*, std::vector< facebook_message* >*, std::map< std::string, facebook_chatroom* >*, bool unreadOnly);
+ int parse_thread_messages(std::string*, std::vector< facebook_message >*, std::map< std::string, facebook_chatroom* >*, bool unreadOnly);
int parse_thread_info(std::string* data, std::string* user_id);
int parse_user_info(std::string* data, facebook_user* fbu);
int parse_chat_info(std::string* data, facebook_chatroom* fbc);
diff --git a/protocols/FacebookRM/src/process.cpp b/protocols/FacebookRM/src/process.cpp index 991a180136..d9d52b0b38 100644 --- a/protocols/FacebookRM/src/process.cpp +++ b/protocols/FacebookRM/src/process.cpp @@ -391,7 +391,7 @@ void FacebookProto::ProcessUnreadMessage(void *pParam) CODE_BLOCK_TRY - std::vector<facebook_message*> messages; + std::vector<facebook_message> messages; std::map<std::string, facebook_chatroom*> chatrooms; facebook_json_parser* p = new facebook_json_parser(this); @@ -512,7 +512,7 @@ void FacebookProto::LoadLastMessages(void *pParam) CODE_BLOCK_TRY - std::vector<facebook_message*> messages; + std::vector<facebook_message> messages; std::map<std::string, facebook_chatroom*> chatrooms; facebook_json_parser* p = new facebook_json_parser(this); @@ -610,7 +610,7 @@ void FacebookProto::SyncThreads(void*) CODE_BLOCK_TRY - std::vector<facebook_message*> messages; + std::vector<facebook_message> messages; std::map<std::string, facebook_chatroom*> chatrooms; facebook_json_parser* p = new facebook_json_parser(this); @@ -809,19 +809,17 @@ void FacebookProto::ProcessOnThisDay(void*) facy.handle_success(__FUNCTION__); } -void FacebookProto::ReceiveMessages(std::vector<facebook_message*> messages, bool check_duplicates) +void FacebookProto::ReceiveMessages(std::vector<facebook_message> &messages, bool check_duplicates) { bool naseemsSpamMode = getBool(FACEBOOK_KEY_NASEEMS_SPAM_MODE, false); // TODO: make this checking more lightweight as now it is not effective at all... if (check_duplicates) { // 1. check if there are some message that we already have (compare FACEBOOK_KEY_MESSAGE_ID = last received message ID) - for (std::vector<facebook_message*>::size_type i = 0; i < messages.size(); i++) { - - MCONTACT hContact = messages[i]->isChat - ? ChatIDToHContact(messages[i]->thread_id) - : ContactIDToHContact(messages[i]->user_id); + for (size_t i = 0; i < messages.size(); i++) { + facebook_message &msg = messages[i]; + MCONTACT hContact = msg.isChat ? ChatIDToHContact(msg.thread_id) : ContactIDToHContact(msg.user_id); if (hContact == NULL) continue; @@ -829,22 +827,22 @@ void FacebookProto::ReceiveMessages(std::vector<facebook_message*> messages, boo if (lastId == NULL) continue; - if (!messages[i]->message_id.compare(lastId)) { + if (!msg.message_id.compare(lastId)) { // Equal, ignore all older messages (including this) from same contact for (std::vector<facebook_message*>::size_type j = 0; j < messages.size(); j++) { - bool equalsId = messages[i]->isChat - ? (messages[j]->thread_id == messages[i]->thread_id) - : (messages[j]->user_id == messages[i]->user_id); + bool equalsId = msg.isChat + ? (messages[j].thread_id == msg.thread_id) + : (messages[j].user_id == msg.user_id); - if (equalsId && messages[j]->time <= messages[i]->time) - messages[j]->flag_ = 1; + if (equalsId && messages[j].time <= msg.time) + messages[j].flag_ = 1; } } } // 2. remove all marked messages from list - for (std::vector<facebook_message*>::iterator it = messages.begin(); it != messages.end();) { - if ((*it)->flag_ == 1) + for (std::vector<facebook_message>::iterator it = messages.begin(); it != messages.end();) { + if ((*it).flag_ == 1) it = messages.erase(it); else ++it; @@ -854,17 +852,16 @@ void FacebookProto::ReceiveMessages(std::vector<facebook_message*> messages, boo std::set<MCONTACT> *hChatContacts = new std::set<MCONTACT>(); for (std::vector<facebook_message*>::size_type i = 0; i < messages.size(); i++) { - if (messages[i]->isChat) { - if (!m_enableChat) { - delete messages[i]; + facebook_message &msg = messages[i]; + if (msg.isChat) { + if (!m_enableChat) continue; - } // Multi-user message - debugLogA(" < Got chat message ID: %s", messages[i]->message_id.c_str()); + debugLogA(" < Got chat message ID: %s", msg.message_id.c_str()); facebook_chatroom *fbc; - std::string thread_id = messages[i]->thread_id.c_str(); + std::string thread_id = msg.thread_id.c_str(); auto it = facy.chat_rooms.find(thread_id); if (it != facy.chat_rooms.end()) { @@ -872,10 +869,8 @@ void FacebookProto::ReceiveMessages(std::vector<facebook_message*> messages, boo } else { // In Naseem's spam mode we ignore outgoing messages sent from other instances - if (naseemsSpamMode && !messages[i]->isIncoming) { - delete messages[i]; + if (naseemsSpamMode && !msg.isIncoming) continue; - } // We don't have this chat loaded in memory yet, lets load some info (name, list of users) fbc = new facebook_chatroom(thread_id); @@ -904,48 +899,45 @@ void FacebookProto::ReceiveMessages(std::vector<facebook_message*> messages, boo if (!hChatContact) { // hopefully shouldn't happen, but who knows? debugLog(_T("!!! No hChatContact for %s"), fbc->thread_id.c_str()); - delete messages[i]; continue; } // We don't want to save (this) message ID for chatrooms - // setString(hChatContact, FACEBOOK_KEY_MESSAGE_ID, messages[i]->message_id.c_str()); - setDword(FACEBOOK_KEY_LAST_ACTION_TS, messages[i]->time); + // setString(hChatContact, FACEBOOK_KEY_MESSAGE_ID, msg.message_id.c_str()); + setDword(FACEBOOK_KEY_LAST_ACTION_TS, msg.time); // Save TID if not exists already ptrA tid(getStringA(hChatContact, FACEBOOK_KEY_TID)); - if (!tid || mir_strcmp(tid, messages[i]->thread_id.c_str())) - setString(hChatContact, FACEBOOK_KEY_TID, messages[i]->thread_id.c_str()); + if (!tid || mir_strcmp(tid, msg.thread_id.c_str())) + setString(hChatContact, FACEBOOK_KEY_TID, msg.thread_id.c_str()); // Get name of this chat participant - std::string name = messages[i]->user_id; // fallback to numeric id + std::string name = msg.user_id; // fallback to numeric id - auto jt = fbc->participants.find(messages[i]->user_id); + auto jt = fbc->participants.find(msg.user_id); if (jt != fbc->participants.end()) { name = jt->second; } // TODO: support also system messages (rename chat, user quit, etc.)! (here? or it is somewhere else? // ... we must add some new "type" field into facebook_message structure and use it also for Pokes and similar) - UpdateChat(fbc->thread_id.c_str(), messages[i]->user_id.c_str(), name.c_str(), messages[i]->message_text.c_str(), messages[i]->time); + UpdateChat(fbc->thread_id.c_str(), msg.user_id.c_str(), name.c_str(), msg.message_text.c_str(), msg.time); // Automatically mark message as read because chatroom doesn't support onRead event (yet) hChatContacts->insert(hChatContact); // std::set checks duplicates at insert automatically } else { // Single-user message - debugLogA(" < Got message ID: %s", messages[i]->message_id.c_str()); + debugLogA(" < Got message ID: %s", msg.message_id.c_str()); facebook_user fbu; - fbu.user_id = messages[i]->user_id; + fbu.user_id = msg.user_id; MCONTACT hContact = ContactIDToHContact(fbu.user_id); if (hContact == NULL) { // In Naseem's spam mode we ignore outgoing messages sent from other instances - if (naseemsSpamMode && !messages[i]->isIncoming) { - delete messages[i]; + if (naseemsSpamMode && !msg.isIncoming) continue; - } // We don't have this contact, lets load info about him LoadContactInfo(&fbu); @@ -955,52 +947,49 @@ void FacebookProto::ReceiveMessages(std::vector<facebook_message*> messages, boo if (!hContact) { // hopefully shouldn't happen, but who knows? - debugLogA("!!! No hContact for %s", messages[i]->user_id.c_str()); - delete messages[i]; + debugLogA("!!! No hContact for %s", msg.user_id.c_str()); continue; } // Save last (this) message ID - setString(hContact, FACEBOOK_KEY_MESSAGE_ID, messages[i]->message_id.c_str()); + setString(hContact, FACEBOOK_KEY_MESSAGE_ID, msg.message_id.c_str()); // Save TID if not exists already ptrA tid(getStringA(hContact, FACEBOOK_KEY_TID)); - if ((!tid || mir_strcmp(tid, messages[i]->thread_id.c_str())) && !messages[i]->thread_id.empty()) - setString(hContact, FACEBOOK_KEY_TID, messages[i]->thread_id.c_str()); + if ((!tid || mir_strcmp(tid, msg.thread_id.c_str())) && !msg.thread_id.empty()) + setString(hContact, FACEBOOK_KEY_TID, msg.thread_id.c_str()); - if (messages[i]->isIncoming && messages[i]->isUnread && messages[i]->type == MESSAGE) { + if (msg.isIncoming && msg.isUnread && msg.type == MESSAGE) { PROTORECVEVENT recv = { 0 }; - recv.szMessage = const_cast<char*>(messages[i]->message_text.c_str()); - recv.timestamp = messages[i]->time; + recv.szMessage = const_cast<char*>(msg.message_text.c_str()); + recv.timestamp = msg.time; ProtoChainRecvMsg(hContact, &recv); } else { DBEVENTINFO dbei = { 0 }; dbei.cbSize = sizeof(dbei); - if (messages[i]->type == CALL) + if (msg.type == CALL) dbei.eventType = FACEBOOK_EVENTTYPE_CALL; else dbei.eventType = EVENTTYPE_MESSAGE; dbei.flags = DBEF_UTF; - if (!messages[i]->isIncoming) + if (!msg.isIncoming) dbei.flags |= DBEF_SENT; - if (!messages[i]->isUnread) + if (!msg.isUnread) dbei.flags |= DBEF_READ; dbei.szModule = m_szModuleName; - dbei.timestamp = messages[i]->time; - dbei.cbBlob = (DWORD)messages[i]->message_text.length() + 1; - dbei.pBlob = (PBYTE)messages[i]->message_text.c_str(); + dbei.timestamp = msg.time; + dbei.cbBlob = (DWORD)msg.message_text.length() + 1; + dbei.pBlob = (PBYTE)msg.message_text.c_str(); db_event_add(hContact, &dbei); } } - delete messages[i]; } - messages.clear(); if (!hChatContacts->empty()) { ForkThread(&FacebookProto::ReadMessageWorker, (void*)hChatContacts); @@ -1026,7 +1015,7 @@ void FacebookProto::ProcessMessages(void* data) CODE_BLOCK_TRY - std::vector< facebook_message* > messages; + std::vector<facebook_message> messages; facebook_json_parser* p = new facebook_json_parser(this); p->parse_messages(resp, &messages, &facy.notifications); diff --git a/protocols/FacebookRM/src/proto.h b/protocols/FacebookRM/src/proto.h index fa2b1788a5..ad88a916cd 100644 --- a/protocols/FacebookRM/src/proto.h +++ b/protocols/FacebookRM/src/proto.h @@ -214,7 +214,7 @@ public: void AddChatContact(const char *chat_id, const char *id, const char *name); void RemoveChatContact(const char *chat_id, const char *id, const char *name); char *GetChatUsers(const char *chat_id); - void ReceiveMessages(std::vector<facebook_message*> messages, bool check_duplicates = false); + void ReceiveMessages(std::vector<facebook_message> &messages, bool check_duplicates = false); void LoadChatInfo(facebook_chatroom* fbc); void LoadParticipantsNames(facebook_chatroom *fbc); |