MATLAB Answers

onCleanup order of execution

5 views (last 30 days)
When onCleanup objects are created in a function, they execute their cleanup routines when destroyed. If several onCleanup objects are created in a function, they will all execute their cleanup routines. I have 2 questions about the order of execution:
  1. Say 2 onCleanup objects are created, in this order: objCleanup1 and objCleanup2. The function they are created in then completes, and the objects are destroyed. I assume that their corresponding cleanup routines will execute in arbitrary order (or at least not necessarily in the order of creation). Is this correct?
  2. Say both cleanup routines are very long (i.e., several seconds). Can we safely assume that as long as a single cleanup routine is running, the next one will not start (even if their order is, in fact, arbitrary)?
Thank you in advance.

Accepted Answer

Bryan White
Bryan White on 11 May 2011
1. Yes, you are correct: execution order is not guaranteed. I have seen people "force" the ordering of onCleanup object destruction a couple different ways, none of them being particularly pretty.
Example 1: Extra onCleanup object that references the other onCleanup objects.
oC1 = onCleanup(@doTask1);
oC2 = onCleanup(@doTask2);
...
oCN = onCleanup(@doTaskN);
orderedCleanupObj = onCleanup(@()cellfun(@delete, {oC1, oC2, ..., oCN}));
Example 2: One onCleanup object.
task1 = {@doTask1, task1Arg1, task1Arg2, ..., task1ArgN};
task2 = {@doTask2, task1Arg2, task2Arg2, ..., task2ArgN};
...
taskN = {@doTaskN, taskNArg1, taskNArg2, ..., taskNArgN};
oC = onCleanup(@()cellfun(@(c)feval(c{:}), {task1, task2, ..., taskN}));
Example 3: Incremental Addition via Backreferencing.
oC1 = onCleanup(@doTask1);
s1.cleanup1 = oC1;
...
oC2 = onCleanup(@doTask2);
s2 = struct('cleanup1', s1, 'cleanup2', oC2);
...
oCN = onCleanup(@doTaskN);
sN = struct('cleanupN-1', sN-1, 'cleanupN', oCN);
2. Yes, the cleanup routines are synchronous: one must finish for the next to start. The only caveat, then, would be a cleanup task that spawns something asynchronous (e.g., starting a TIMER). But generally speaking, yes.
  2 Comments
Bryan White
Bryan White on 12 May 2011
Hi Eugeny,
Besides MATLAB documentation (namely the <http://www.mathworks.com/help/techdoc/ref/oncleanup.html onCleanup> and <http://www.mathworks.com/help/techdoc/ref/handle.delete.html delete> reference pages and the pages they link) the best web resource I can recommend off-hand would be <http://blogs.mathworks.com/loren/2008/03/10/keeping-things-tidy/ Loren's blog on the onCleanup object>, where some discussion in the comments (circa comment numbers 9-14) goes into some detail.

Sign in to comment.

More Answers (1)

Jacob Lynch August
Jacob Lynch August on 27 Sep 2018
Edited: Jacob Lynch August on 27 Sep 2018
onCleanup objects can be stored in a cellular vector, and will be performed first to last. You should know the number of total tasks, and the order they should be run. Usually my code requires a first-in-last-out ordering. I would setup the onCleanup tasks like this:
ON_CLEANUP_TASKS = cell(N,1);
ON_CLEANUP_TASKS{N} = onCleanup(@()TaskN);
% ... some operations and additional tasks
ON_CLEANUP_TASKS{1} = onCleanup(@()Task1);
Even though TaskN was created first, it will be done last.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!