diff --git a/dom/bluetooth/BluetoothService.cpp b/dom/bluetooth/BluetoothService.cpp index 6155de6d7f99..4b95ffd965a1 100644 --- a/dom/bluetooth/BluetoothService.cpp +++ b/dom/bluetooth/BluetoothService.cpp @@ -20,6 +20,7 @@ #include "BluetoothUtils.h" #include "jsapi.h" +#include "mozilla/ClearOnShutdown.h" #include "mozilla/Services.h" #include "mozilla/StaticPtr.h" #include "mozilla/unused.h" @@ -732,15 +733,15 @@ BluetoothService::Get() } // Create new instance, register, return - nsRefPtr service = BluetoothService::Create(); - NS_ENSURE_TRUE(service, nullptr); + sBluetoothService = BluetoothService::Create(); + NS_ENSURE_TRUE(sBluetoothService, nullptr); - if (!service->Init()) { - service->Cleanup(); + if (!sBluetoothService->Init()) { + sBluetoothService->Cleanup(); return nullptr; } - sBluetoothService = service; + ClearOnShutdown(&sBluetoothService); return sBluetoothService; } diff --git a/dom/bluetooth2/BluetoothService.cpp b/dom/bluetooth2/BluetoothService.cpp index 0a409ae292b4..5cafeb0a430e 100644 --- a/dom/bluetooth2/BluetoothService.cpp +++ b/dom/bluetooth2/BluetoothService.cpp @@ -20,6 +20,7 @@ #include "BluetoothUtils.h" #include "jsapi.h" +#include "mozilla/ClearOnShutdown.h" #include "mozilla/Services.h" #include "mozilla/StaticPtr.h" #include "mozilla/unused.h" @@ -663,15 +664,15 @@ BluetoothService::Get() } // Create new instance, register, return - nsRefPtr service = BluetoothService::Create(); - NS_ENSURE_TRUE(service, nullptr); + sBluetoothService = BluetoothService::Create(); + NS_ENSURE_TRUE(sBluetoothService, nullptr); - if (!service->Init()) { - service->Cleanup(); + if (!sBluetoothService->Init()) { + sBluetoothService->Cleanup(); return nullptr; } - sBluetoothService = service; + ClearOnShutdown(&sBluetoothService); return sBluetoothService; }