Jump to content

Fix ITEM_BLEND Little Memory Leak


Sherer

Recommended Posts

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

  • Love 20
Link to comment
Share on other sites

  • 1 year later...
  • Honorable Member

++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 by Mali
str
  • Love 2

 

Link to comment
Share on other sites

  • 1 year later...
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

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

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.