From 9c2692f1f18036d506697b37f1a7938632beb62c Mon Sep 17 00:00:00 2001 From: darthgandalf Date: Sun, 10 Oct 2010 09:59:15 +0000 Subject: [PATCH] Remove sockets and timers from perl modules in their destructors. This should decrease memory usage a bit, and decrease chance of crashing znc from bad perl module a bit. git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@2156 726aef4b-f618-498e-8847-2d620e286838 --- modules/modperl.cpp | 20 ++++++++++++++++++++ modules/modperl/module.h | 2 ++ modules/modperl/startup.pl | 26 ++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/modules/modperl.cpp b/modules/modperl.cpp index 336f8bec..c8c53306 100644 --- a/modules/modperl.cpp +++ b/modules/modperl.cpp @@ -249,6 +249,17 @@ void CPerlTimer::RunJob() { } } +CPerlTimer::~CPerlTimer() { + CPerlModule* pMod = AsPerlModule(GetModule()); + if (pMod) { + PSTART; + PUSH_STR(pMod->GetPerlID()); + PUSH_STR(GetPerlID()); + PCALL("ZNC::Core::RemoveTimer"); + PEND; + } +} + #define SOCKSTART PSTART; PUSH_STR(pMod->GetPerlID()); PUSH_STR(GetPerlID()) #define SOCKCBCHECK(OnSuccess) PCALL("ZNC::Core::CallSocket"); if (SvTRUE(ERRSV)) { Close(); DEBUG("Perl socket hook died with: " + PString(ERRSV)); } else { OnSuccess; } PEND #define CBSOCK(Func) void CPerlSocket::Func() {\ @@ -298,4 +309,13 @@ Csock* CPerlSocket::GetSockObj(const CString& sHost, unsigned short uPort) { return result; } +CPerlSocket::~CPerlSocket() { + CPerlModule* pMod = AsPerlModule(GetModule()); + if (pMod) { + SOCKSTART; + PCALL("ZNC::Core::RemoveSocket"); + PEND; + } +} + GLOBALMODULEDEFS(CModPerl, "Loads perl scripts as ZNC modules") diff --git a/modules/modperl/module.h b/modules/modperl/module.h index ae93ad90..4557ac32 100644 --- a/modules/modperl/module.h +++ b/modules/modperl/module.h @@ -107,6 +107,7 @@ public: } virtual void RunJob(); CString GetPerlID() { return m_sPerlID; } + ~CPerlTimer(); }; inline CPerlTimer* CreatePerlTimer(CPerlModule* pModule, unsigned int uInterval, unsigned int uCycles, @@ -119,6 +120,7 @@ class CPerlSocket : public CSocket { public: CPerlSocket(CPerlModule* pModule, const CString& sPerlID) : CSocket(pModule), m_sPerlID(sPerlID) {} CString GetPerlID() { return m_sPerlID; } + ~CPerlSocket(); virtual void Connected(); virtual void Disconnected(); virtual void Timeout(); diff --git a/modules/modperl/startup.pl b/modules/modperl/startup.pl index 70329902..03c514bd 100644 --- a/modules/modperl/startup.pl +++ b/modules/modperl/startup.pl @@ -171,6 +171,18 @@ sub CallSocket { $pmods{$modid}->_CallSocket(@_) } +sub RemoveTimer { + my $modid = shift; + my $timerid = shift; + $pmods{$modid}->_RemoveTimer($timerid) +} + +sub RemoveSocket { + my $modid = shift; + my $sockid = shift; + $pmods{$modid}->_RemoveSocket($sockid) +} + package ZNC::ModuleNV; sub TIEHASH { @@ -377,6 +389,13 @@ sub _CallTimer { &{$self->{_ptimers}{$id}{job}}($self, $self->{_ptimers}{$id}{obj}); } +sub _RemoveTimer { + my $self = shift; + my $id = shift; + say "Removing perl timer $id"; + delete $self->{_ptimers}{$id} +} + sub CreateSocket { my $self = shift; my $class = shift; @@ -398,6 +417,13 @@ sub _CallSocket { $self->{_sockets}{$id}->$func(@_) } +sub _RemoveSocket { + my $self = shift; + my $id = shift; + say "Removing perl socket $id"; + delete $self->{_sockets}{$id} +} + package ZNC::Socket; sub GetModule {