Browse code

Use std::unique_ptr in in_xsf.cpp.

Also move the code of the NCSF Player's destructor to Terminate() and call Terminate() in the destructor.

Naram Qashat authored on 2021/04/06 20:45:29
Showing 2 changed files
... ...
@@ -130,19 +130,7 @@ static DWORD WINAPI soundViewThread(void *b)
130 130
 
131 131
 XSFPlayer_NCSF::~XSFPlayer_NCSF()
132 132
 {
133
-#ifndef NDEBUG
134
-	if (soundViewThreadHandle != INVALID_HANDLE_VALUE)
135
-	{
136
-		killSoundViewThread = true;
137
-		if (WaitForSingleObject(soundViewThreadHandle, 2000) == WAIT_TIMEOUT)
138
-		{
139
-			TerminateThread(soundViewThreadHandle, 0);
140
-			static_cast<XSFConfig_NCSF *>(xSFConfig)->CloseSoundView();
141
-		}
142
-		CloseHandle(soundViewThreadHandle);
143
-		soundViewThreadHandle = INVALID_HANDLE_VALUE;
144
-	}
145
-#endif
133
+	this->Terminate();
146 134
 }
147 135
 
148 136
 bool XSFPlayer_NCSF::Load()
... ...
@@ -232,6 +220,20 @@ void XSFPlayer_NCSF::GenerateSamples(std::vector<std::uint8_t> &buf, unsigned of
232 220
 void XSFPlayer_NCSF::Terminate()
233 221
 {
234 222
 	this->player.Stop(true);
223
+
224
+#ifndef NDEBUG
225
+	if (soundViewThreadHandle != INVALID_HANDLE_VALUE)
226
+	{
227
+		killSoundViewThread = true;
228
+		if (WaitForSingleObject(soundViewThreadHandle, 2000) == WAIT_TIMEOUT)
229
+		{
230
+			TerminateThread(soundViewThreadHandle, 0);
231
+			static_cast<XSFConfig_NCSF *>(xSFConfig)->CloseSoundView();
232
+		}
233
+		CloseHandle(soundViewThreadHandle);
234
+		soundViewThreadHandle = INVALID_HANDLE_VALUE;
235
+	}
236
+#endif
235 237
 }
236 238
 
237 239
 #ifndef NDEBUG
... ...
@@ -25,8 +25,8 @@
25 25
 extern In_Module inMod;
26 26
 static const XSFFile *xSFFile = nullptr;
27 27
 XSFFile *xSFFileInInfo = nullptr;
28
-static XSFPlayer *xSFPlayer = nullptr;
29
-XSFConfig *xSFConfig = nullptr;
28
+static std::unique_ptr<XSFPlayer> xSFPlayer;
29
+std::unique_ptr<XSFConfig> xSFConfig;
30 30
 static bool paused;
31 31
 static int seek_needed;
32 32
 static double decode_pos_ms;
... ...
@@ -84,7 +84,7 @@ void config(HWND hwndParent)
84 84
 {
85 85
 	xSFConfig->CallConfigDialog(inMod.hDllInstance, hwndParent);
86 86
 	if (xSFPlayer)
87
-		xSFConfig->CopyConfigToMemory(xSFPlayer, false);
87
+		xSFConfig->CopyConfigToMemory(xSFPlayer.get(), false);
88 88
 }
89 89
 
90 90
 void about(HWND hwndParent)
... ...
@@ -94,7 +94,7 @@ void about(HWND hwndParent)
94 94
 
95 95
 void init()
96 96
 {
97
-	xSFConfig = XSFConfig::Create();
97
+	xSFConfig.reset(XSFConfig::Create());
98 98
 	xSFConfig->LoadConfig();
99 99
 	xSFConfig->GenerateDialogs();
100 100
 	xSFConfig->SetHInstance(inMod.hDllInstance);
... ...
@@ -102,8 +102,8 @@ void init()
102 102
 
103 103
 void quit()
104 104
 {
105
-	delete xSFPlayer;
106
-	delete xSFConfig;
105
+	xSFPlayer.reset();
106
+	xSFConfig.reset();
107 107
 }
108 108
 
109 109
 void getFileInfo(const in_char *file, in_char *title, int *length_in_ms)
... ...
@@ -196,7 +196,7 @@ int play(const in_char *fn)
196 196
 		inMod.VSASetInfo(tmpxSFPlayer->GetSampleRate(), NumChannels);
197 197
 		inMod.outMod->SetVolume(-666);
198 198
 
199
-		xSFPlayer = tmpxSFPlayer.release();
199
+		xSFPlayer = std::move(tmpxSFPlayer);
200 200
 		killThread = false;
201 201
 		thread_handle = CreateThread(nullptr, 0, playThread, &killThread, 0, nullptr);
202 202
 		return 0;
... ...
@@ -239,8 +239,7 @@ void stop()
239 239
 	}
240 240
 	inMod.outMod->Close();
241 241
 	inMod.SAVSADeInit();
242
-	delete xSFPlayer;
243
-	xSFPlayer = nullptr;
242
+	xSFPlayer.reset();
244 243
 	xSFFile = nullptr;
245 244
 }
246 245