22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5+ #include < memory>
6+ #include < utility>
7+
58#include " include/dart_native_api.h"
69#include " platform/assert.h"
710#include " platform/unicode.h"
@@ -113,8 +116,9 @@ static void ThrowIsolateSpawnException(const String& message) {
113116
114117class SpawnIsolateTask : public ThreadPool ::Task {
115118 public:
116- SpawnIsolateTask (Isolate* parent_isolate, IsolateSpawnState* state)
117- : parent_isolate_(parent_isolate), state_(state) {
119+ SpawnIsolateTask (Isolate* parent_isolate,
120+ std::unique_ptr<IsolateSpawnState> state)
121+ : parent_isolate_(parent_isolate), state_(std::move(state)) {
118122 parent_isolate->IncrementSpawnCount ();
119123 }
120124
@@ -131,8 +135,6 @@ class SpawnIsolateTask : public ThreadPool::Task {
131135 if (callback == NULL ) {
132136 ReportError (
133137 " Isolate spawn is not supported by this Dart implementation\n " );
134- delete state_;
135- state_ = NULL ;
136138 return ;
137139 }
138140
@@ -149,8 +151,6 @@ class SpawnIsolateTask : public ThreadPool::Task {
149151 parent_isolate_ = nullptr ;
150152 if (isolate == NULL ) {
151153 ReportError (error);
152- delete state_;
153- state_ = NULL ;
154154 free (error);
155155 return ;
156156 }
@@ -162,8 +162,7 @@ class SpawnIsolateTask : public ThreadPool::Task {
162162 }
163163 MutexLocker ml (isolate->mutex ());
164164 state_->set_isolate (isolate);
165- isolate->set_spawn_state (state_);
166- state_ = NULL ;
165+ isolate->set_spawn_state (std::move (state_));
167166 if (isolate->is_runnable ()) {
168167 isolate->Run ();
169168 }
@@ -181,7 +180,7 @@ class SpawnIsolateTask : public ThreadPool::Task {
181180 }
182181
183182 Isolate* parent_isolate_;
184- IsolateSpawnState* state_;
183+ std::unique_ptr< IsolateSpawnState> state_;
185184
186185 DISALLOW_COPY_AND_ASSIGN (SpawnIsolateTask);
187186};
@@ -238,20 +237,19 @@ DEFINE_NATIVE_ENTRY(Isolate_spawnFunction, 0, 11) {
238237 const char * utf8_debug_name =
239238 debugName.IsNull () ? NULL : String2UTF8 (debugName);
240239
241- IsolateSpawnState* state = new IsolateSpawnState (
240+ std::unique_ptr< IsolateSpawnState> state ( new IsolateSpawnState (
242241 port.Id (), isolate->origin_id (), String2UTF8 (script_uri), func,
243242 &message_buffer, utf8_package_config, paused.value (), fatal_errors,
244- on_exit_port, on_error_port, utf8_debug_name);
243+ on_exit_port, on_error_port, utf8_debug_name)) ;
245244
246245 // Since this is a call to Isolate.spawn, copy the parent isolate's code.
247246 state->isolate_flags ()->copy_parent_code = true ;
248247
249- ThreadPool::Task* spawn_task = new SpawnIsolateTask (isolate, state);
248+ ThreadPool::Task* spawn_task =
249+ new SpawnIsolateTask (isolate, std::move (state));
250250
251251 if (!Dart::thread_pool ()->Run (spawn_task)) {
252252 // Running on the thread pool failed. Clean up everything.
253- delete state;
254- state = NULL ;
255253 delete spawn_task;
256254 spawn_task = NULL ;
257255 }
@@ -360,10 +358,10 @@ DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 0, 13) {
360358 const char * utf8_debug_name =
361359 debugName.IsNull () ? NULL : String2UTF8 (debugName);
362360
363- IsolateSpawnState* state = new IsolateSpawnState (
361+ std::unique_ptr< IsolateSpawnState> state ( new IsolateSpawnState (
364362 port.Id (), canonical_uri, utf8_package_config, &arguments_buffer,
365363 &message_buffer, paused.value (), fatal_errors, on_exit_port,
366- on_error_port, utf8_debug_name);
364+ on_error_port, utf8_debug_name)) ;
367365
368366 // If we were passed a value then override the default flags state for
369367 // checked mode.
@@ -375,12 +373,11 @@ DEFINE_NATIVE_ENTRY(Isolate_spawnUri, 0, 13) {
375373 // Since this is a call to Isolate.spawnUri, don't copy the parent's code.
376374 state->isolate_flags ()->copy_parent_code = false ;
377375
378- ThreadPool::Task* spawn_task = new SpawnIsolateTask (isolate, state);
376+ ThreadPool::Task* spawn_task =
377+ new SpawnIsolateTask (isolate, std::move (state));
379378
380379 if (!Dart::thread_pool ()->Run (spawn_task)) {
381380 // Running on the thread pool failed. Clean up everything.
382- delete state;
383- state = NULL ;
384381 delete spawn_task;
385382 spawn_task = NULL ;
386383 }
0 commit comments