1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsProxyRelease.h"
8 #include "nsThreadUtils.h"
11 class nsProxyReleaseEvent
: public nsRunnable
14 explicit nsProxyReleaseEvent(nsISupports
* aDoomed
) : mDoomed(aDoomed
) {}
23 nsISupports
* MOZ_OWNING_REF mDoomed
;
27 NS_ProxyRelease(nsIEventTarget
* aTarget
, nsISupports
* aDoomed
,
43 bool onCurrentThread
= false;
44 rv
= aTarget
->IsOnCurrentThread(&onCurrentThread
);
45 if (NS_SUCCEEDED(rv
) && onCurrentThread
) {
51 nsRefPtr
<nsIRunnable
> ev
= new nsProxyReleaseEvent(aDoomed
);
53 // we do not release aDoomed here since it may cause a delete on the
54 // wrong thread. better to leak than crash.
55 return NS_ERROR_OUT_OF_MEMORY
;
58 rv
= aTarget
->Dispatch(ev
, NS_DISPATCH_NORMAL
);
60 NS_WARNING("failed to post proxy release event");
61 // again, it is better to leak the aDoomed object than risk crashing as
62 // a result of deleting it on the wrong thread.