Sherer 487 Posted February 24, 2019 Share Posted February 24, 2019 Hello, There is a little memory leak associated with ITEM_BLEND item you would probably like to sort out. Find: LPITEM CHARACTER::AutoGiveItem(DWORD dwItemVnum, WORD bCount, int iRarePct, bool bMsg) Scroll down until you find this kind of code piece: if (item->GetType() == ITEM_BLEND) { for (int i=0; i < INVENTORY_MAX_NUM; i++) { LPITEM inv_item = GetInventoryItem(i); if (inv_item == NULL) continue; if (inv_item->GetType() == ITEM_BLEND) { if (inv_item->GetVnum() == item->GetVnum()) { if (inv_item->GetSocket(0) == item->GetSocket(0) && inv_item->GetSocket(1) == item->GetSocket(1) && inv_item->GetSocket(2) == item->GetSocket(2) && inv_item->GetCount() < ITEM_MAX_COUNT) { inv_item->SetCount(inv_item->GetCount() + item->GetCount()); return inv_item; } } } } } And add there destroy item's function before return statement. The result should look like this: if (item->GetType() == ITEM_BLEND) { for (int i=0; i < INVENTORY_MAX_NUM; i++) { LPITEM inv_item = GetInventoryItem(i); if (inv_item == NULL) continue; if (inv_item->GetType() == ITEM_BLEND) { if (inv_item->GetVnum() == item->GetVnum()) { if (inv_item->GetSocket(0) == item->GetSocket(0) && inv_item->GetSocket(1) == item->GetSocket(1) && inv_item->GetSocket(2) == item->GetSocket(2) && inv_item->GetCount() < ITEM_MAX_COUNT) { inv_item->SetCount(inv_item->GetCount() + item->GetCount()); // Memory Leak Fix M2_DESTROY_ITEM(item); return inv_item; } } } } } Item created above using CreateItem function would never be destroyed at all (it's usually returned from function for further use). Thus you need to wipe it out manually. Regards 20 Link to comment Share on other sites More sharing options...
attila1995 3 Posted February 24, 2019 Share Posted February 24, 2019 Thank you for public it. Link to comment Share on other sites More sharing options...
Active Member Helia01 2159 Posted February 25, 2019 Active Member Share Posted February 25, 2019 SEXY 1 Link to comment Share on other sites More sharing options...
Honorable Member Mali 42512 Posted February 28, 2020 Honorable Member Share Posted February 28, 2020 (edited) ++Blend_Item_load Spoiler bool Blend_Item_load(char *file) { char one_line[256]; const char* delim = " \t\r\n"; char* v; if (!file || !file[0]) return false; std::unique_ptr<FILE, decltype(&std::fclose)> fp(std::fopen(file, "r"), &std::fclose); if (!fp) return false; std::unique_ptr< BLEND_ITEM_INFO > blend_item_info = nullptr; while (fgets(one_line, 256, fp.get())) { if (one_line[0]=='#') continue; const char* token_string = strtok(one_line, delim); if (NULL==token_string) continue; TOKEN("section") { blend_item_info.reset(new BLEND_ITEM_INFO{}); } else TOKEN("item_vnum") { v = strtok(NULL, delim); if (NULL==v || !blend_item_info) return false; str_to_number(blend_item_info->item_vnum, v); } else TOKEN("apply_type") { v = strtok(NULL, delim); if (NULL==v || !blend_item_info) return false; if (0 == (blend_item_info->apply_type = FN_get_apply_type(v))) { sys_err ("Invalid apply_type(%s)", v); return false; } } else TOKEN("apply_value") { for (int i=0; i<MAX_BLEND_ITEM_VALUE; ++i) { v = strtok(NULL, delim); if (NULL==v || !blend_item_info) return false; str_to_number(blend_item_info->apply_value[i], v); } } else TOKEN("apply_duration") { for (int i=0; i<MAX_BLEND_ITEM_VALUE; ++i) { v = strtok(NULL, delim); if (NULL == v || !blend_item_info) return false; str_to_number(blend_item_info->apply_duration[i], v); } } else TOKEN("end") { if (blend_item_info) s_blend_info.push_back(blend_item_info.release()); } } return true; } Edited February 6, 2022 by Mali str 2 Link to comment Share on other sites More sharing options...
Debloat 88 Posted February 6, 2022 Share Posted February 6, 2022 On 2/28/2020 at 5:15 PM, Mali said: ++Blend_Item_load Hide contents bool Blend_Item_load(char* file) { char one_line[256]; const char* delim = " \t\r\n"; char* v; if (!file || !file[0]) return false; std::unique_ptr<FILE, decltype(&std::fclose)> fp(std::fopen(file, "r"), &std::fclose); if (!fp) return false; std::unique_ptr< BLEND_ITEM_INFO > blend_item_info; while (fgets(one_line, 256, fp.get())) { if (one_line[0] == '#') continue; const char* token_string = strtok(one_line, delim); if (!token_string) continue; if (!stricmp(token_string, "section")) blend_item_info.reset(new BLEND_ITEM_INFO()); else if (!stricmp(token_string, "item_vnum")) { v = strtok(nullptr, delim); if (v && blend_item_info) str_to_number(blend_item_info->item_vnum, v); } else if (!stricmp(token_string, "apply_type")) { v = strtok(nullptr, delim); if (!v || !blend_item_info || !(blend_item_info->apply_type = FN_get_apply_type(v))) { sys_err("Invalid apply_type(%s)", v); return false; } } else if (!stricmp(token_string, "apply_value")) { for (int i = 0; i < MAX_BLEND_ITEM_VALUE; ++i) { v = strtok(nullptr, delim); if (v && blend_item_info) str_to_number(blend_item_info->apply_value[i], v); } } else if (!stricmp(token_string, "apply_duration")) { for (int i = 0; i < MAX_BLEND_ITEM_VALUE; ++i) { v = strtok(nullptr, delim); if (v && blend_item_info) str_to_number(blend_item_info->apply_duration[i], v); } } else if (!stricmp(token_string, "end") && blend_item_info) s_blend_info.emplace_back(new BLEND_ITEM_INFO(*blend_item_info)); } return true; } Hi Mali, I'm getting this error with your code while building game. Could you please help? blend_item.cpp:83:22: error: 'stricmp' was not declared in this scope; did you mean 'strncmp'? 83 | if (!stricmp(token_string, "section")) Link to comment Share on other sites More sharing options...
Premium xTryhard 12 Posted February 6, 2022 Premium Share Posted February 6, 2022 this is not a leak the variable "inv_item" was created on the stack and not on the heap it is just a local variable inside a function Link to comment Share on other sites More sharing options...
Debloat 88 Posted February 6, 2022 Share Posted February 6, 2022 23 minutes ago, xTryhard said: this is not a leak the variable "inv_item" was created on the stack and not on the heap it is just a local variable inside a function So you mean this fix is unnecessary? Link to comment Share on other sites More sharing options...
Premium xTryhard 12 Posted February 6, 2022 Premium Share Posted February 6, 2022 1 minute ago, Debloat said: So you mean this fix is unnecessary? i thought he was talking about "inv_item" now i have seen he is destroying "item" sorry my fault. 1 Link to comment Share on other sites More sharing options...
Debloat 88 Posted February 6, 2022 Share Posted February 6, 2022 4 hours ago, Debloat said: Hi Mali, I'm getting this error with your code while building game. Could you please help? blend_item.cpp:83:22: error: 'stricmp' was not declared in this scope; did you mean 'strncmp'? 83 | if (!stricmp(token_string, "section")) Don't know if this is smart way to do it or not since I don't know how to code, but I read on google that "stricmp" is windows spesific and "strcasecmp" is its equivalent. So I changed "stricmp"s with "strcasecmp" and the error is gone. Can someone confirm? Link to comment Share on other sites More sharing options...
Recommended Posts