Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gnc_commodity_table_get_namespaces returns a vector #1910

Draft
wants to merge 6 commits into
base: stable
Choose a base branch
from

Conversation

christopherlam
Copy link
Contributor

@christopherlam christopherlam commented Apr 9, 2024

gnc_commodity_table_get_namespaces currently returns a GList* which isn't always released. There's more leaks similar to 4f5ee5d. Instead of hunting down leaks, forward-proof it to return a std::vector. Should it return a std::vector<std::string>?

Ditto gnc_commodity_table_get_commodities isn't always being released properly. postponed to later branch

Copy link
Member

@jralls jralls left a comment

Choose a reason for hiding this comment

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

Maybe we should split base-typemaps.i into base-typemaps-guile.i and base-typemaps-python.i.

gnucash/gnome-utils/dialog-commodity.cpp Outdated Show resolved Hide resolved
libgnucash/engine/gnc-commodity.cpp Show resolved Hide resolved
g_hash_table_destroy(ns->cm_table);
std::for_each (ns->cm_table.begin(), ns->cm_table.end(),
[](auto it){ gnc_commodity_destroy (it.second); });
StrCommodityMap().swap(ns->cm_table);
Copy link
Member

@jralls jralls May 1, 2024

Choose a reason for hiding this comment

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

Sigh. Not your fault but this function should call only

{
    gnc_commodity_namespace *ns = gnc_commodity_table_find_namespace(table, name_space);
    g_object_unref(ns);
}

The cleanup stuff--removing ns from table->ns_table and table->ns_list and clearing the StrCommodityMap should be in gnc_commodity_namespace_dispose_real(). Call ns->cm_table.~StrCommodityMap() in gnc_commodity_namespace_finalize_real(). If you use placement new for constructing the StrCommodityMap then you can call delete ns->cm_table instead, which is all-around cleaner. Edit No, I remember now that you still have to use ~StrCommodityMap() with placement new.

There's no point to having GObject memory management if we don't bleeping use it.

Better still of course would be to rewrite gnc_commodity_namespace as a proper GncCommodityNamespace C++ class, but that's probably more work than you want to do at this point.

@@ -1592,7 +1593,7 @@ gnc_commodity_table *
gnc_commodity_table_new(void)
{
gnc_commodity_table * retval = g_new0(gnc_commodity_table, 1);
retval->ns_table = g_hash_table_new(&g_str_hash, &g_str_equal);
StrCommodityNSMap().swap (retval->ns_table);
Copy link
Member

Choose a reason for hiding this comment

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

As for StrCommodityMap, better to use placement new.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As for StrCommodityMap, better to use placement new.

or auto retval = new gnc_commodity_table; which will properly initialize the map.

Copy link
Member

Choose a reason for hiding this comment

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

It will, but you'll need an explicit ~gnc_commodity_table() to clean up the GList. Note that mixing new with malloc like that is considered risky. The compiler will probably do what you want but it doesn't have to.

libgnucash/engine/gnc-commodity.cpp Outdated Show resolved Hide resolved
libgnucash/engine/gnc-commodity.cpp Outdated Show resolved Hide resolved
@christopherlam
Copy link
Contributor Author

This branch getting rather crowded. I wonder if it would be safer to push well-tested commits one by one. The aim is still to use c++ classes.

@christopherlam christopherlam force-pushed the progress branch 3 times, most recently from b74d21e to 74b1ea8 Compare May 5, 2024 08:53
@christopherlam
Copy link
Contributor Author

last commit is fake... don't know how to fix python bindings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants