@@ -291,6 +291,25 @@ struct PreparedChildGraphUpdateState {
291291 }
292292};
293293
294+ // Deleter of the shared hierarchy: it owns the root CUgraph once the root box
295+ // exists.
296+ static void
297+ release_graph_hierarchy (GraphHierarchy *hierarchy)
298+ {
299+ for (const GraphBox &box : hierarchy->graphs ) {
300+ if (box.resource ) {
301+ graph_registry.unregister_handle (box.resource );
302+ }
303+ }
304+ GraphBox *root = hierarchy->root ();
305+ if (root && root->resource ) {
306+ GILReleaseGuard gil;
307+ pw_cuGraphDestroy (root->resource );
308+ }
309+ retry_deferred_cleanup ();
310+ delete hierarchy;
311+ }
312+
294313GraphHandle
295314create_graph_handle (CUgraph graph)
296315{
@@ -299,28 +318,21 @@ create_graph_handle(CUgraph graph)
299318 return {};
300319 }
301320
302- // clang-format off
303- auto hierarchy = std::shared_ptr<GraphHierarchy>(
304- new GraphHierarchy{},
305- [](GraphHierarchy *hierarchy) {
306- for (const GraphBox& box : hierarchy->graphs ) {
307- if (box.resource ) {
308- graph_registry.unregister_handle (box.resource );
309- }
310- }
311- GraphBox *root = hierarchy->root ();
312- if (root && root->resource ) {
313- GILReleaseGuard gil;
314- pw_cuGraphDestroy (root->resource );
315- }
316- retry_deferred_cleanup ();
317- delete hierarchy;
318- }
319- );
320- // clang-format on
321- GraphBox &root = hierarchy->graphs .emplace_back (graph, hierarchy.get ());
321+ // Until the root box exists the hierarchy deleter has nothing to destroy,
322+ // so an allocation failure before then must release the graph here.
323+ std::shared_ptr<GraphHierarchy> hierarchy;
324+ GraphBox *root = nullptr ;
325+ try {
326+ hierarchy = std::shared_ptr<GraphHierarchy>(new GraphHierarchy{}, release_graph_hierarchy);
327+ root = &hierarchy->graphs .emplace_back (graph, hierarchy.get ());
328+ }
329+ catch (...) {
330+ GILReleaseGuard gil;
331+ pw_cuGraphDestroy (graph);
332+ throw ;
333+ }
322334
323- GraphHandle h_graph (hierarchy, &root. resource );
335+ GraphHandle h_graph (hierarchy, &root-> resource );
324336 graph_registry.register_handle (graph, h_graph);
325337 return h_graph;
326338}
0 commit comments