Raised This Month: $121 Target: $400
 30% 

Solved Possible, memory leak in DataPack


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dragokas
Senior Member
Join Date: Nov 2017
Location: Ukraine
Old 12-07-2018 , 06:28   Possible, memory leak in DataPack
Reply With Quote #1

Hi,
it was my old plugin:
Code:
#pragma semicolon 1

#include <sourcemod>
#include <sdktools>

#define PLUGIN_VERSION "1.0"

const int Slot_Primary        = 0;    // Primary weapon slot (weapon_smg, weapon_pumpshotgun, weapon_autoshotgun, weapon_rifle, weapon_hunting_rifle)
const int Slot_Secondary      = 1;    // Secondary weapon slot (weapon_pistol)
const int Slot_Melee          = 2;    // Melee (knife) weapon slot (weapon_first_aid_kit)
const int Slot_Projectile     = 3;    // Projectile weapon slot (weapon_molotov, weapon_pipe_bomb)
const int Slot_Explosive      = 4;    // Explosive (c4) weapon slot (weapon_pain_pills)

bool g_MapEnd = false;
Handle hWeaponTimer = INVALID_HANDLE;

public Plugin myinfo = 
{
	name = "WeaponOnSpawn",
	author = "Dragokas",
	description = "Give pistol, pain pills and basic weapon to player on spawn",
	version = PLUGIN_VERSION,
	url = "https://github.com/dragokas"
}

public OnPluginStart()
{
	HookEvent("survivor_rescued", Event_Survivor_Rescued, EventHookMode_Post);
	HookEvent("player_spawn", Event_PlayerSpawn, EventHookMode_Post);
}

public void OnPluginEnd()
{
	UnhookEvent("survivor_rescued", Event_Survivor_Rescued);
	UnhookEvent("player_spawn", Event_PlayerSpawn);
	hWeaponTimer = INVALID_HANDLE;
}
public void OnMapEnd()
{
	g_MapEnd = true;
	hWeaponTimer = INVALID_HANDLE;
}
public void OnMapStart()
{
	g_MapEnd = false;
}

void GiveItem(int Client, char Item[22])
{
	int flags = GetCommandFlags("give");
	SetCommandFlags("give", flags & ~FCVAR_CHEAT);
	FakeClientCommand(Client, "give %s", Item);
	SetCommandFlags("give", flags|FCVAR_CHEAT);
}

public Action Event_Survivor_Rescued( Event event, const char[] name, bool dontBroadcast )
{
	int client = GetClientOfUserId( GetEventInt(event, "victim") );

	if ( (0 < client <= MaxClients) && IsClientInGame(client))
	{
		WeaponSetTimer(client, true);
	}
	return Plugin_Continue;
}

public Action Event_PlayerSpawn(Event event, const char[] name, bool dontBroadcast)
{
	int clientid = GetEventInt(event,"userid");
	int client = GetClientOfUserId(clientid);
	
	if ((0 < client <= MaxClients) && IsClientInGame(client) && (GetClientTeam(client) == 2) )
	{
		WeaponSetTimer(client, false);
	}
	return Plugin_Continue;
}

void WeaponSetTimer(int client, bool DoGivePills)
{
	DataPack pack = new DataPack();
	hWeaponTimer = CreateDataTimer(2.0, Timer_GiveWeapon, pack, TIMER_FLAG_NO_MAPCHANGE);
	pack.WriteCell(client);
	pack.WriteCell(view_as<int>(DoGivePills));
}

public Action Timer_GiveWeapon(Handle timer, Handle pack)
{
	int client;
	int DoGivePills;

	ResetPack(pack);
	client = ReadPackCell(pack);
	DoGivePills = ReadPackCell(pack);
	
	if (IsClientInGame(client) && (GetClientTeam(client) == 2) && IsPlayerAlive(client) )
	{
		GivePrimaryWeapon(client, view_as<bool>(DoGivePills));
	}
}

void GivePrimaryWeapon(int client, bool DoGivePills)
{
	if (g_MapEnd)
		return;

	int EntWeapon;

	if (DoGivePills)
	{
		EntWeapon = GetPlayerWeaponSlot(client, Slot_Explosive);
		if (EntWeapon == -1)
		{
			GiveItem(client, "pain_pills");
		}
	}

	EntWeapon = GetPlayerWeaponSlot(client, Slot_Secondary);
	if (EntWeapon == -1)
	{
		GiveItem(client, "pistol");
	}

	EntWeapon = GetPlayerWeaponSlot(client, Slot_Primary);
	if (EntWeapon == -1)
	{
		if (GetRandomInt(0, 1) == 0) {
			GiveItem(client, "pumpshotgun");
		} else {
			GiveItem(client, "smg");
		}
	}
}
and easily can be rewritten without DataPack.

Anyway, I noticed that sm_dump_handles show me ~ 50 lines of:
Quote:
0x2a9c0583 WeaponOnSpawn.smx DataPack 16
Why?
AFAIK, timer is responsible for erasing passed DataPack.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D1 Bloody Witch //^.^\\ server

Last edited by Dragokas; 12-07-2018 at 06:50.
Dragokas is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Old 12-07-2018 , 06:44   Re: Possible, memory leak in DataPack
Reply With Quote #2

CreateDataTimer initializes the DataPack for you. So this:

PHP Code:
DataPack pack = new DataPack(); 
should be:

PHP Code:
DataPack pack
__________________

Last edited by DJ Tsunami; 12-07-2018 at 08:36.
DJ Tsunami is offline
Dragokas
Senior Member
Join Date: Nov 2017
Location: Ukraine
Old 12-07-2018 , 06:49   Re: Possible, memory leak in DataPack
Reply With Quote #3

hmm, I see.

Thank you.
No questions.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D1 Bloody Witch //^.^\\ server
Dragokas is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:32.


Powered by vBulletin®
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Theme made by Freecode