fix message Searialize memory leak
This commit is contained in:
parent
e5bed79e64
commit
0618406a7d
@ -139,6 +139,8 @@ std::string
|
|||||||
CMessagePlugin::GenerateFunctionDeclaration(Message::Ptr message)
|
CMessagePlugin::GenerateFunctionDeclaration(Message::Ptr message)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
ss << message->id() << "* " << message->id() << "_New();" << std::endl;
|
||||||
|
ss << "void " << message->id() << "_Free(" << message->id() << "** message_ptr);" << std::endl;
|
||||||
ss << "void " << message->id() << "_Init(" << message->id() << "* message);" << std::endl;
|
ss << "void " << message->id() << "_Init(" << message->id() << "* message);" << std::endl;
|
||||||
ss << "void " << message->id() << "_Destroy(" << message->id() << "* message);" << std::endl;
|
ss << "void " << message->id() << "_Destroy(" << message->id() << "* message);" << std::endl;
|
||||||
ss << std::endl;
|
ss << std::endl;
|
||||||
@ -164,10 +166,10 @@ std::string
|
|||||||
CMessagePlugin::GenerateFunctionDefinition(Message::Ptr message)
|
CMessagePlugin::GenerateFunctionDefinition(Message::Ptr message)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
ss << GenerateFunctionDefinitionNew(message);
|
||||||
|
ss << GenerateFunctionDefinitionFree(message);
|
||||||
ss << GenerateFunctionDefinitionInit(message);
|
ss << GenerateFunctionDefinitionInit(message);
|
||||||
ss << GenerateFunctionDefinitionDestroy(message);
|
ss << GenerateFunctionDefinitionDestroy(message);
|
||||||
ss << std::endl;
|
|
||||||
|
|
||||||
ss << GenerateFunctionDefinitionSerialize(message);
|
ss << GenerateFunctionDefinitionSerialize(message);
|
||||||
ss << GenerateFunctionDefinitionDeserialize(message);
|
ss << GenerateFunctionDefinitionDeserialize(message);
|
||||||
ss << std::endl;
|
ss << std::endl;
|
||||||
@ -314,12 +316,12 @@ CMessagePlugin::GenerateFunctionDefinitionSerialize(Message::Ptr message)
|
|||||||
ss << " return *buffer_size; " << std::endl;
|
ss << " return *buffer_size; " << std::endl;
|
||||||
ss << " } while (0);" << std::endl;
|
ss << " } while (0);" << std::endl;
|
||||||
|
|
||||||
// for (int idx = 0; idx < message->items().size(); idx++) {
|
for (int idx = 0; idx < message->items().size(); idx++) {
|
||||||
// const auto &item = items[idx];
|
const auto &item = items[idx];
|
||||||
// if (item->type() == Item::Type::kString) {
|
if (item->type() == Item::Type::kString) {
|
||||||
// ss << " if (item_" << idx << ") { free(item_" << idx << "); }" << std::endl;
|
ss << " if (item_" << idx << ") { free(item_" << idx << "); }" << std::endl;
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
ss << " return 0; " << std::endl;
|
ss << " return 0; " << std::endl;
|
||||||
ss << "}" << std::endl;
|
ss << "}" << std::endl;
|
||||||
@ -337,22 +339,24 @@ CMessagePlugin::GenerateFunctionDefinitionDeserialize(Message::Ptr message)
|
|||||||
ss << " size_t dump_sz = buffer_size;" << std::endl;
|
ss << " size_t dump_sz = buffer_size;" << std::endl;
|
||||||
// if (!message->items().empty()) {
|
// if (!message->items().empty()) {
|
||||||
if (true) {
|
if (true) {
|
||||||
ss << " do {" << std::endl;
|
|
||||||
ss << " " << message->id() << "_Destroy(message);" << std::endl;
|
|
||||||
ss << " " << message->id() << "_Init(message);" << std::endl;
|
|
||||||
auto items = message->items();
|
auto items = message->items();
|
||||||
|
|
||||||
for (int idx = 0; idx < message->items().size(); idx++) {
|
for (int idx = 0; idx < message->items().size(); idx++) {
|
||||||
const auto &item = items[idx];
|
const auto &item = items[idx];
|
||||||
std::string type_name;
|
std::string type_name;
|
||||||
if (item->type() == Item::Type::kInt32) {
|
if (item->type() == Item::Type::kInt32) {
|
||||||
ss << " int32_t item_" << idx << ";" << std::endl;
|
ss << " int32_t item_" << idx << ";" << std::endl;
|
||||||
} else if (item->type() == Item::Type::kString) {
|
} else if (item->type() == Item::Type::kString) {
|
||||||
ss << " char* item_" << idx << " = NULL;" << std::endl;
|
ss << " char* item_" << idx << " = NULL;" << std::endl;
|
||||||
} else if (item->type() == Item::Type::kMessage) {
|
} else if (item->type() == Item::Type::kMessage) {
|
||||||
// type_name = "char*";
|
// type_name = "char*";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ss << " do {" << std::endl;
|
||||||
|
ss << " " << message->id() << "_Destroy(message);" << std::endl;
|
||||||
|
ss << " " << message->id() << "_Init(message);" << std::endl;
|
||||||
|
|
||||||
ss << std::endl;
|
ss << std::endl;
|
||||||
|
|
||||||
std::string tpl_format = GetTPLMap(message->items());
|
std::string tpl_format = GetTPLMap(message->items());
|
||||||
@ -393,9 +397,14 @@ CMessagePlugin::GenerateFunctionDefinitionDeserialize(Message::Ptr message)
|
|||||||
// type_name = "char*";
|
// type_name = "char*";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ss << " } while (0);" << std::endl;
|
ss << " } while (0);" << std::endl;
|
||||||
|
|
||||||
|
for (int idx = 0; idx < message->items().size(); idx++) {
|
||||||
|
const auto &item = items[idx];
|
||||||
|
if (item->type() == Item::Type::kString) {
|
||||||
|
ss << " if (item_" << idx << ") { free(item_" << idx << "); }" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ss << " return 0; " << std::endl;
|
ss << " return 0; " << std::endl;
|
||||||
ss << "}" << std::endl;
|
ss << "}" << std::endl;
|
||||||
@ -422,6 +431,7 @@ CMessagePlugin::GenerateFunctionDefinitionDestroy(Message::Ptr message)
|
|||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "void " << message->id() << "_Destroy(" << message->id() << "* message) {" << std::endl;
|
ss << "void " << message->id() << "_Destroy(" << message->id() << "* message) {" << std::endl;
|
||||||
|
ss << " if (message == NULL) { return; }" << std::endl;
|
||||||
for (auto &item : message->items()) {
|
for (auto &item : message->items()) {
|
||||||
auto c_item = CItem::Create(item);
|
auto c_item = CItem::Create(item);
|
||||||
ss << c_item->GenerateDestroyCodeBlock(message->id()) << std::endl;
|
ss << c_item->GenerateDestroyCodeBlock(message->id()) << std::endl;
|
||||||
@ -430,4 +440,30 @@ CMessagePlugin::GenerateFunctionDefinitionDestroy(Message::Ptr message)
|
|||||||
ss << std::endl;
|
ss << std::endl;
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
CMessagePlugin::GenerateFunctionDefinitionNew(Message::Ptr message)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << message->id() << "* " << message->id() << "_New() {" << std::endl;
|
||||||
|
ss << " " << message->id() << "* message = (" << message->id() << "*)malloc(sizeof(" << message->id() << "));" << std::endl;
|
||||||
|
ss << " if (message == NULL) { return NULL; }" << std::endl;
|
||||||
|
ss << " " << message->id() << "_Init(message);" << std::endl;
|
||||||
|
ss << " return message;" << std::endl;
|
||||||
|
ss << "}" << std::endl;
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
CMessagePlugin::GenerateFunctionDefinitionFree(Message::Ptr message)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "void " << message->id() << "_Free(" << message->id() << "** message_ptr) {" << std::endl;
|
||||||
|
ss << " if (!message_ptr) { return; }" << std::endl;
|
||||||
|
ss << " " << message->id() << "_Destroy(*message_ptr);" << std::endl;
|
||||||
|
ss << " free(*message_ptr);" << std::endl;
|
||||||
|
ss << " *message_ptr = NULL; " << std::endl;
|
||||||
|
ss << "}" << std::endl;
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
}// namespace tqcq
|
}// namespace tqcq
|
||||||
|
@ -29,6 +29,8 @@ private:
|
|||||||
std::string GenerateFunctionDeclaration(Message::Ptr message);
|
std::string GenerateFunctionDeclaration(Message::Ptr message);
|
||||||
std::string GenerateFunctionDefinition(Message::Ptr message);
|
std::string GenerateFunctionDefinition(Message::Ptr message);
|
||||||
|
|
||||||
|
std::string GenerateFunctionDefinitionNew(Message::Ptr message);
|
||||||
|
std::string GenerateFunctionDefinitionFree(Message::Ptr message);
|
||||||
std::string GenerateFunctionDefinitionSerialize(Message::Ptr message);
|
std::string GenerateFunctionDefinitionSerialize(Message::Ptr message);
|
||||||
std::string GenerateFunctionDefinitionDeserialize(Message::Ptr message);
|
std::string GenerateFunctionDefinitionDeserialize(Message::Ptr message);
|
||||||
std::string GenerateFunctionDefinitionInit(Message::Ptr message);
|
std::string GenerateFunctionDefinitionInit(Message::Ptr message);
|
||||||
|
@ -103,13 +103,12 @@ CRPCClient::GenerateStructAPIWrapper(std::string service_id) const
|
|||||||
|
|
||||||
|
|
||||||
ss << " do {" << std::endl;
|
ss << " do {" << std::endl;
|
||||||
ss << " response = (" << response_id() << " *)malloc(sizeof(" << response_id() << "));" << std::endl;
|
ss << " response = " << response_id() << "_New(); " << std::endl;
|
||||||
ss << " if (response == NULL) {" << std::endl;
|
ss << " if (response == NULL) {" << std::endl;
|
||||||
ss << " status = -1;" << std::endl;
|
ss << " status = -1;" << std::endl;
|
||||||
ss << " break;" << std::endl;
|
ss << " break;" << std::endl;
|
||||||
ss << " }" << std::endl;
|
ss << " }" << std::endl;
|
||||||
|
|
||||||
ss << " " << response_id() << "_Init(response);" << std::endl;
|
|
||||||
ss << " status = " << response_id() << "_Deserialize(response, msg, msg_len);" << std::endl;
|
ss << " status = " << response_id() << "_Deserialize(response, msg, msg_len);" << std::endl;
|
||||||
ss << " if (status != 0) {" << std::endl;
|
ss << " if (status != 0) {" << std::endl;
|
||||||
ss << " " << response_id() << "_Destroy(response);" << std::endl;
|
ss << " " << response_id() << "_Destroy(response);" << std::endl;
|
||||||
@ -194,14 +193,15 @@ CRPCClient::GenerateCallFunctionDefinition(std::string service_id) const
|
|||||||
ss << " break;" << std::endl;
|
ss << " break;" << std::endl;
|
||||||
ss << " }" << std::endl;
|
ss << " }" << std::endl;
|
||||||
ss << " Base64encode(base64_buffer, request_buffer, request_buffer_size);" << std::endl;
|
ss << " Base64encode(base64_buffer, request_buffer, request_buffer_size);" << std::endl;
|
||||||
ss << " const char* service_name = (const char*)strdup(\"" << service_id << "::" << id() << "\");"<< std::endl;
|
ss << " const char* service_name = \"" << service_id << "::" << id() << "\";"<< std::endl;
|
||||||
ss << " /* tpl pack A(ss)*/ " << std::endl;
|
ss << " /* tpl pack A(ss)*/ " << std::endl;
|
||||||
ss << " {" << std::endl;
|
ss << " {" << std::endl;
|
||||||
// ss << " const char* base64_buffer_ptr = strdup(base64_buffer); " << std::endl;
|
// ss << " const char* base64_buffer_ptr = strdup(base64_buffer); " << std::endl;
|
||||||
ss << " tpl_node* tn = tpl_map(\"A(ss)\", &service_name, &base64_buffer);" << std::endl;
|
ss << " tpl_node* tn = tpl_map(\"A(ss)\", &service_name, &base64_buffer);" << std::endl;
|
||||||
ss << " status = tpl_pack(tn, 1);" << std::endl;
|
ss << " status = tpl_pack(tn, 1);" << std::endl;
|
||||||
ss << " if (status == -1) { break; }" << std::endl;
|
ss << " if (status == -1) { break; tpl_free(tn); }" << std::endl;
|
||||||
ss << " tpl_dump(tn, TPL_MEM, &dump_buffer, &dump_sz);" << std::endl;
|
ss << " tpl_dump(tn, TPL_MEM, &dump_buffer, &dump_sz);" << std::endl;
|
||||||
|
ss << " tpl_free(tn);" << std::endl;
|
||||||
ss << " if (dump_sz < 0) { break; }" << std::endl;
|
ss << " if (dump_sz < 0) { break; }" << std::endl;
|
||||||
ss << " }" << std::endl;
|
ss << " }" << std::endl;
|
||||||
ss << " status = "<< service_id << "_Client_Send(client, dump_buffer, dump_sz);" << std::endl;
|
ss << " status = "<< service_id << "_Client_Send(client, dump_buffer, dump_sz);" << std::endl;
|
||||||
@ -223,7 +223,7 @@ CRPCClient::GenerateCallFunctionDefinition(std::string service_id) const
|
|||||||
ss << GenerateCallUnlock(service_id) << std::endl;
|
ss << GenerateCallUnlock(service_id) << std::endl;
|
||||||
ss << " free(request_buffer);" << std::endl;
|
ss << " free(request_buffer);" << std::endl;
|
||||||
ss << " free(dump_buffer);" << std::endl;
|
ss << " free(dump_buffer);" << std::endl;
|
||||||
// ss << " free(base64_buffer);" << std::endl;
|
ss << " free(base64_buffer);" << std::endl;
|
||||||
ss << " return priv->ok ? 0 : -1;" << std::endl;
|
ss << " return priv->ok ? 0 : -1;" << std::endl;
|
||||||
ss << "}" << std::endl;
|
ss << "}" << std::endl;
|
||||||
return ss.str();
|
return ss.str();
|
||||||
|
@ -82,12 +82,12 @@ CRPCServer::GenerateStructAPIWrapper(std::string service_id) const
|
|||||||
ss << " printf(\"" << id() << " failed: %d\\n\", status);" << std::endl;
|
ss << " printf(\"" << id() << " failed: %d\\n\", status);" << std::endl;
|
||||||
ss << " break;" << std::endl;
|
ss << " break;" << std::endl;
|
||||||
ss << " }" << std::endl;
|
ss << " }" << std::endl;
|
||||||
ss << " Base64encode(base64_buffer, response_msg, response_msg_len);" << std::endl;
|
ss << " base64_buffer_size = Base64encode(base64_buffer, response_msg, response_msg_len);" << std::endl;
|
||||||
ss << " // pack" << std::endl;
|
ss << " // pack" << std::endl;
|
||||||
ss << " {" << std::endl;
|
ss << " {" << std::endl;
|
||||||
ss << " const char* service_name = (const char*)strdup(\"" << service_id << "::" << id() << "\");" << std::endl;
|
ss << " const char* service_name = \"" << service_id << "::" << id() << "\";" << std::endl;
|
||||||
// ss << " const char* base64_buffer_ptr = strdup(base64_buffer);" << std::endl;
|
ss << " const char* base64_buffer_ptr = base64_buffer;" << std::endl;
|
||||||
ss << " tpl_node* tn = tpl_map(\"A(ss)\", &service_name, &base64_buffer);" << std::endl;
|
ss << " tpl_node* tn = tpl_map(\"A(ss)\", &service_name, &base64_buffer_ptr);" << std::endl;
|
||||||
ss << " tpl_pack(tn, 1);" << std::endl;
|
ss << " tpl_pack(tn, 1);" << std::endl;
|
||||||
ss << " tpl_dump(tn, TPL_MEM, &buffer, &buffer_size);" << std::endl;
|
ss << " tpl_dump(tn, TPL_MEM, &buffer, &buffer_size);" << std::endl;
|
||||||
ss << " tpl_free(tn);" << std::endl;
|
ss << " tpl_free(tn);" << std::endl;
|
||||||
@ -101,7 +101,7 @@ CRPCServer::GenerateStructAPIWrapper(std::string service_id) const
|
|||||||
ss << " status = 0;" << std::endl;
|
ss << " status = 0;" << std::endl;
|
||||||
ss << " } while (0);" << std::endl;
|
ss << " } while (0);" << std::endl;
|
||||||
ss << " free(buffer);" << std::endl;
|
ss << " free(buffer);" << std::endl;
|
||||||
// ss << " free(base64_buffer);" << std::endl;
|
// ss << " free(base64_buffer);" << std::endl;
|
||||||
ss << " free(response_msg);" << std::endl;
|
ss << " free(response_msg);" << std::endl;
|
||||||
ss << " " << response_id() << "_Destroy(&response);" << std::endl;
|
ss << " " << response_id() << "_Destroy(&response);" << std::endl;
|
||||||
ss << " " << request_id() << "_Destroy(&request);" << std::endl;
|
ss << " " << request_id() << "_Destroy(&request);" << std::endl;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user