@@ -61,13 +61,15 @@ CAbstractUploadEngine* UploadEngineManager::getUploadEngine(ServerProfile &serve
6161 LOG (ERROR) << " No such server " << serverProfile.serverName ();
6262 return nullptr ;
6363 }
64- CAbstractUploadEngine* result = nullptr ;
64+ std::shared_ptr< CAbstractUploadEngine> result = nullptr ;
6565 std::string serverName = serverProfile.serverName ();
6666 std::thread::id threadId = std::this_thread::get_id ();
6767
6868 BasicSettings* Settings = ServiceLocator::instance ()->basicSettings ();
6969 ServerSettingsStruct* serverSettings = Settings->getServerSettings (serverProfile, true );
7070 std::string authDataLogin = serverSettings ? serverSettings->authData .Login : std::string ();
71+ auto key = std::make_pair (serverName, serverProfile.profileName ());
72+
7173 if (ue->UsingPlugin ) {
7274 // Try to load Squirrel (.nut) script
7375 result = getPlugin (serverProfile, ue->PluginName );
@@ -80,9 +82,9 @@ CAbstractUploadEngine* UploadEngineManager::getUploadEngine(ServerProfile &serve
8082 CAbstractUploadEngine* plugin = nullptr ;
8183 auto it = m_plugins.find (threadId);
8284 if (it != m_plugins.end ()) {
83- auto it2 = it->second .find (serverName );
85+ auto it2 = it->second .find (key );
8486 if (it2 != it->second .end ()) {
85- plugin = it2->second ;
87+ plugin = it2->second . get () ;
8688 }
8789 }
8890
@@ -96,48 +98,48 @@ CAbstractUploadEngine* UploadEngineManager::getUploadEngine(ServerProfile &serve
9698 if (!ue->Engine .empty ()) {
9799#ifdef IU_ENABLE_MEGANZ
98100 if (ue->Engine == " MegaNz" ) {
99- result = new CMegaNzUploadEngine (serverSync, serverSettings, errorCallback);
101+ result = std::make_shared< CMegaNzUploadEngine> (serverSync, serverSettings, errorCallback);
100102 }
101103#endif
102104 if (!result) {
103105 LOG (ERROR) << " There is no built-in upload engine named '" << ue->Engine << " '." ;
104106 return nullptr ;
105107 }
106108 } else {
107- result = new CDefaultUploadEngine (serverSync, errorCallback);
109+ result = std::make_shared< CDefaultUploadEngine> (serverSync, errorCallback);
108110 }
109111 result->setServerSettings (serverSettings);
110112 result->setUploadData (ue);
111113
112- m_plugins[threadId][serverName ] = result;
114+ m_plugins[threadId][key ] = result;
113115 }
114116
115117 result->setServerSettings (serverSettings);
116118 result->setUploadData (ue);
117119 result->setOnErrorMessageCallback (std::bind (&IUploadErrorHandler::ErrorMessage,uploadErrorHandler_.get (),std::placeholders::_1));
118- return result;
120+ return result. get () ;
119121}
120122
121123CScriptUploadEngine* UploadEngineManager::getScriptUploadEngine (ServerProfile& serverProfile)
122124{
123125 return dynamic_cast <CScriptUploadEngine*>(getUploadEngine (serverProfile));
124126}
125127
126- CScriptUploadEngine* UploadEngineManager::getPlugin (ServerProfile& serverProfile, const std::string& pluginName, bool UseExisting) {
128+ std::shared_ptr< CScriptUploadEngine> UploadEngineManager::getPlugin (ServerProfile& serverProfile, const std::string& pluginName, bool UseExisting) {
127129 std::lock_guard<std::mutex> lock (pluginsMutex_);
128130 std::string serverName = serverProfile.serverName ();
129131
130132 BasicSettings* basicSettings = ServiceLocator::instance ()->basicSettings ();
131133 ServerSettingsStruct* params = basicSettings->getServerSettings (serverProfile, true );
132134
133135 std::thread::id threadId = std::this_thread::get_id ();
134- CScriptUploadEngine* plugin = nullptr ;
135-
136+ std::shared_ptr< CScriptUploadEngine> plugin;
137+ auto key = std::make_pair (serverName, serverProfile. profileName ());
136138 auto it = m_plugins.find (threadId);
137139 if (it != m_plugins.end ()) {
138- auto it2 = it->second .find (serverName );
140+ auto it2 = it->second .find (key );
139141 if (it2 != it->second .end ()) {
140- plugin = dynamic_cast <CScriptUploadEngine* >(it2->second ); ;
142+ plugin = std::dynamic_pointer_cast <CScriptUploadEngine>(it2->second );
141143 }
142144 }
143145
@@ -155,40 +157,31 @@ CScriptUploadEngine* UploadEngineManager::getPlugin(ServerProfile& serverProfile
155157 }
156158
157159 if (plugin) {
158- delete plugin;
159- plugin = 0 ;
160- m_plugins[threadId][serverName] = nullptr ;
160+ m_plugins[threadId][key] = nullptr ;
161161 }
162162 ServerSync* serverSync = getServerSync (serverProfile);
163163 std::string fileName = scriptsDirectory_ + pluginName + " .nut" ;
164- CScriptUploadEngine* newPlugin = new CScriptUploadEngine (fileName, serverSync, params, networkClientFactory_,
164+ auto newPlugin = std::make_shared< CScriptUploadEngine> (fileName, serverSync, params, networkClientFactory_,
165165 std::bind (&IUploadErrorHandler::ErrorMessage, uploadErrorHandler_.get (), std::placeholders::_1));
166166
167167 if (newPlugin->isLoaded ()) {
168- m_plugins[threadId][serverName ] = newPlugin;
168+ m_plugins[threadId][key ] = newPlugin;
169169 return newPlugin;
170170 }
171- else {
172- delete newPlugin;
173- }
171+
174172 return nullptr ;
175173}
176174
177175void UploadEngineManager::unloadUploadEngines () {
178176 std::lock_guard<std::mutex> lock (pluginsMutex_);
179- for (auto it = m_plugins.begin (); it != m_plugins.end (); ++it) {
180- for (auto it2 = it->second .begin (); it2 != it->second .end (); ++it2) {
181- delete it2->second ;
182- }
183- }
184177 m_plugins.clear ();
185178}
186179
187180
188181void UploadEngineManager::unloadUploadEngines (const std::string& serverName, const std::string& profileName) {
189182 std::lock_guard<std::mutex> lock (pluginsMutex_);
190183 for (auto &pr: m_plugins) {
191- pr.second .erase (serverName);
184+ pr.second .erase ({ serverName, profileName } );
192185 }
193186}
194187
@@ -202,9 +195,6 @@ void UploadEngineManager::clearThreadData()
202195 std::thread::id threadId = std::this_thread::get_id ();
203196 auto it = m_plugins.find (threadId);
204197 if (it != m_plugins.end ()) {
205- for (auto it2 = it->second .begin (); it2 != it->second .end (); ++it2) {
206- delete it2->second ;
207- }
208198 m_plugins.erase (it);
209199 }
210200}
0 commit comments