Bug 1099414: Use RefPtr logic instead of delete when nr_socket_local_create fails. r=ekr
authorByron Campen [:bwc] <docfaraday@gmail.com>
Fri, 14 Nov 2014 15:58:56 -0800
changeset 226079 9028edd5e75235816af44b54673f31eab6120b79
parent 226078 c82fb962499a9828804522b5c3fb7956c6769289
child 226080 d67a6612282cba02ea87e17247176dee412293c3
push id28185
push userkwierso@gmail.com
push dateWed, 28 Jan 2015 00:05:15 +0000
treeherdermozilla-central@b2b10231606b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersekr
bugs1099414
milestone38.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1099414: Use RefPtr logic instead of delete when nr_socket_local_create fails. r=ekr
media/mtransport/nr_socket_prsock.cpp
--- a/media/mtransport/nr_socket_prsock.cpp
+++ b/media/mtransport/nr_socket_prsock.cpp
@@ -1102,17 +1102,17 @@ static nr_socket_vtbl nr_socket_local_vt
   nr_socket_local_getaddr,
   nr_socket_local_connect,
   nr_socket_local_write,
   nr_socket_local_read,
   nr_socket_local_close
 };
 
 int nr_socket_local_create(nr_transport_addr *addr, nr_socket **sockp) {
-  NrSocketBase *sock = nullptr;
+  RefPtr<NrSocketBase> sock;
 
   // create IPC bridge for content process
   if (XRE_GetProcessType() == GeckoProcessType_Default) {
     sock = new NrSocket();
   } else {
     nsCOMPtr<nsIThread> main_thread;
     NS_GetMainThread(getter_AddRefs(main_thread));
     sock = new NrSocketIpc(main_thread.get());
@@ -1124,25 +1124,26 @@ int nr_socket_local_create(nr_transport_
   if (r)
     ABORT(r);
 
   r = nr_socket_create_int(static_cast<void *>(sock),
                            sock->vtbl(), sockp);
   if (r)
     ABORT(r);
 
-  // Add a reference so that we can delete it in destroy()
-  sock->AddRef();
-
   _status = 0;
 
+  {
+    // We will release this reference in destroy(), not exactly the normal
+    // ownership model, but it is what it is.
+    NrSocketBase* dummy = sock.forget().take();
+    (void)dummy;
+  }
+
 abort:
-  if (_status) {
-    delete sock;
-  }
   return _status;
 }
 
 
 static int nr_socket_local_destroy(void **objp) {
   if(!objp || !*objp)
     return 0;