cashew: Prevent double deletion of ProcfsByteCounter instance.

BUG=chromium-os:29181
TEST=Build cashew and run unit tests.

Change-Id: I4d0226882b5e4a33c0b899c589199a438a2ee844
Reviewed-on: https://gerrit.chromium.org/gerrit/20545
Reviewed-by: Paul Stewart <pstew@chromium.org>
Commit-Ready: Ben Chan <benchan@chromium.org>
Tested-by: Ben Chan <benchan@chromium.org>
diff --git a/src/data_plan.cc b/src/data_plan.cc
index d8801fa..48f5c97 100644
--- a/src/data_plan.cc
+++ b/src/data_plan.cc
@@ -376,7 +376,6 @@
   DCHECK(service != NULL);
   DCHECK(service_manager != NULL);
   DCHECK(device != NULL);
-  DCHECK(device->ByteCounterRunning());
 
   LOG(INFO) << "OnByteCounterUpdate: delta_rx_bytes = " << delta_rx_bytes
       << ", delta_tx_bytes = " << delta_tx_bytes;
diff --git a/src/device_impl.cc b/src/device_impl.cc
index aaa10d5..e90cfd9 100644
--- a/src/device_impl.cc
+++ b/src/device_impl.cc
@@ -149,16 +149,21 @@
 
 void DeviceImpl::StopByteCounter() {
   if (byte_counter_running_) {
-    DeleteByteCounter();
+    // NOTE: |byte_counter_| is an instance of ProcfsByteCounter.
+    // The implementation of ProcfsByteCounter is subtle in a way that
+    // the destructor of ProcfsByteCounter may again trigger the invocation
+    // of this method. To prevent |byte_counter_| from being deleted again
+    // during its destruction, we must set |byte_counter_running_| to false
+    // before calling DeleteByteCounter to delete |byte_counter_|.
     byte_counter_running_ = false;
+    DeleteByteCounter();
     // notify |parent_|
     parent_->OnByteCounterStopped();
   }
-  DCHECK(byte_counter_ == NULL);
 }
 
 bool DeviceImpl::ReadStats() {
-  return byte_counter_->ReadStats();
+  return byte_counter_ ? byte_counter_->ReadStats() : false;
 }
 
 bool DeviceImpl::GetByteCounterStats(uint64 *rx_bytes, uint64 *tx_bytes) const {