Skip to content

Commit

Permalink
[Account.cpp] explicitly destroy priv->splits
Browse files Browse the repository at this point in the history
better than 35b6fb7
  • Loading branch information
christopherlam committed Apr 20, 2024
1 parent 53c6bc6 commit 871f669
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions libgnucash/engine/Account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1436,11 +1436,10 @@ xaccFreeAccount (Account *acc)
priv->type = ACCT_TYPE_NONE;
gnc_commodity_decrement_usage_count(priv->commodity);
priv->commodity = nullptr;
priv->splits.clear();
priv->splits.shrink_to_fit();

priv->balance_dirty = FALSE;
priv->sort_dirty = FALSE;
priv->splits.~SplitsVec();

/* qof_instance_release (&acc->inst); */
g_object_unref(acc);
Expand Down

4 comments on commit 871f669

@spueblas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this better than shrink_to_fit? Also, I haven't found convincing evidence that calling this destructor explicitly is safe.

Just a gnucash user, who likes coding, and doesn't want his gnucash to crash one day :-)

@jralls
Copy link
Member

@jralls jralls commented on 871f669 Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better because it runs the destructor which doesn't otherwise run instead of just freeing the space allocated for the vector's contents. Although we're not explicitly using placement new we're doing essentially the same thing. You'll see in the example that explicitly calling the object's destructor is required.

@spueblas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! Just for my education, when you have a minute, can you show me how you're doing essentially the same thing as placement new? Thanks!

@jralls
Copy link
Member

@jralls jralls commented on 871f669 Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AccountPrivate allocation and memset to 0 is buried in the GObject macros, which also arrange for calling gnc_account_init wherein priv->splits.clear() is the first place where that offset is treated as a std::vector.

If you want deeper understanding a good place to start is the GObject tutorial.

Please sign in to comment.