g_autoptr(T) is quite useful when the object's extent matches the
function's.

This isn't the case for an Error object the function propagates to its
caller.  It is the case for an Error object the function reports or
handles itself.  However, the functions to report Error also free it.

Thus, g_autoptr(Error) is rarely applicable.  We have just three
instances out of >1100 local Error variables, all in migration code.

Two want to move the error to the MigrationState for later handling /
reporting.  Since migrate_set_error() doesn't move, but stores a copy,
the original needs to be freed, and g_autoptr() is correct there.  We
have 17 more that instead manually free with error_free() or
error_report_err() right after migrate_set_error().

We recently discussed storing a copy vs. move the original:

    From: Peter Xu <[email protected]>
    Subject: Re: [PATCH 0/3] migration: Error fixes and improvements
    Date: Mon, 17 Nov 2025 11:03:37 -0500
    Message-ID: <[email protected]>

The two g_autoptr() gave me pause when I investigated this topic, simply
because they deviate from the common pattern migrate_set_error(s, err)
followed by error_free() or error_report_err().

The third one became wrong when I cleaned up the reporting (missed in
the cleanup patch, fixed in the patch I'm replying to).  I suspect my
mistake escaped review for the same reason I made it: g_autoptr(Error)
is unusual and not visible in the patch hunk.

Would you like me to replace the two correct uses of g_autoptr(Error) by
more common usage?


Reply via email to