Removing text from ComboBoxText

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Removing text from ComboBoxText

Gtkmm mailing list
Hello list,

So i am trying to remove text from a comboboxtext when a user clicks a button. However, i get "basic_string::_M_construct null not valid" and in some cases an std::bad_alloc. I can't understand what i am doing wrong.
Sometimes it works just fine, but when i restart my application and try again it fails !
The following code is in a callback function linked to a button :
// Code of callback

string nom = posteCfgcombo->get_active_text();

int index = posteCfgcombo->get_active_row_number();

posteCfgcombo->remove_text(index);

posteCfgcombo->unset_active();

// end

Can anyone point out what am i doing wrong here ?

Thanks.

_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Removing text from ComboBoxText

Gtkmm mailing list
> Sometimes it works just fine, but when i restart my application and try again it fails !

so check the value of `index` in all cases, and tell us what those values are. Maybe it's -1 when this fails and remove_text() might not handle that (I didn't check the GTK code). Or just use a debugger and see for yourself.



_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Removing text from ComboBoxText

Carlos Gomez
In reply to this post by Gtkmm mailing list
Hi,

First thing is index always valid? I suggest you check it is different
from -1.

On 29/05/2019 09:45, Mohamed Khalil BOUJDARIA via gtkmm-list wrote:
> basic_string::_M_construct null not valid
_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Removing text from ComboBoxText

Gtkmm mailing list
In reply to this post by Gtkmm mailing list
I checked it with gdb, everytime index and nom have a valid value. It seems the problem is in remove_text

Le mer. 29 mai 2019 à 10:12, Daniel Boles via gtkmm-list <[hidden email]> a écrit :
> Sometimes it works just fine, but when i restart my application and try again it fails !

so check the value of `index` in all cases, and tell us what those values are. Maybe it's -1 when this fails and remove_text() might not handle that (I didn't check the GTK code). Or just use a debugger and see for yourself.


_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list


--
Mohamed Khalil BOUJDARIA
Élève ingénieur en mécatronique
École nationale d'ingénieurs de Carthage
Tél : +21653283365/+21652054010

_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Removing text from ComboBoxText

Gtkmm mailing list
In reply to this post by Carlos Gomez
Hi,

Yes, it fails even when index is valid..

Le mer. 29 mai 2019 à 10:16, Carlos Gomez <[hidden email]> a écrit :
Hi,

First thing is index always valid? I suggest you check it is different
from -1.

On 29/05/2019 09:45, Mohamed Khalil BOUJDARIA via gtkmm-list wrote:
> basic_string::_M_construct null not valid
_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list


--
Mohamed Khalil BOUJDARIA
Élève ingénieur en mécatronique
École nationale d'ingénieurs de Carthage
Tél : +21653283365/+21652054010

_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Removing text from ComboBoxText

Gtkmm mailing list
This is the error i get :
glibmm-ERROR **:
unhandled exception (type std::exception) in signal handler:
what: basic_string::_M_construct null not valid

Trace/breakpoint trap (core dumped)

Le mer. 29 mai 2019 à 10:20, Mohamed Khalil BOUJDARIA <[hidden email]> a écrit :
Hi,

Yes, it fails even when index is valid..

Le mer. 29 mai 2019 à 10:16, Carlos Gomez <[hidden email]> a écrit :
Hi,

First thing is index always valid? I suggest you check it is different
from -1.

On 29/05/2019 09:45, Mohamed Khalil BOUJDARIA via gtkmm-list wrote:
> basic_string::_M_construct null not valid
_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list


--
Mohamed Khalil BOUJDARIA
Élève ingénieur en mécatronique
École nationale d'ingénieurs de Carthage
Tél : +21653283365/+21652054010


--
Mohamed Khalil BOUJDARIA
Élève ingénieur en mécatronique
École nationale d'ingénieurs de Carthage
Tél : +21653283365/+21652054010

_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Removing text from ComboBoxText

Carlos Gomez

I have written this code

Glib::ustring us = nullptr;

Glib::ustring us1( nullptr );

And I am getting the same error. Reading the doc about this function

Glib::ustring Gtk::ComboBoxText::get_active_text() const

Returns the currently active string in combo_box, or nullptr if none is selected.

If combo_box contains an entry, this function will return its contents (which will not necessarily be an item from the list).

But constructing a Glib::ustring from a nullptr produces the error, so it could be a bug in the function.


I suggest to wrap the call with a try-catch and if this works fine, but I think it is a bug. I am compiling the version 3.18 of gtkmm to dig a little bit more.


On 29/05/2019 10:21, Mohamed Khalil BOUJDARIA wrote:
This is the error i get :
glibmm-ERROR **:
unhandled exception (type std::exception) in signal handler:
what: basic_string::_M_construct null not valid

Trace/breakpoint trap (core dumped)

Le mer. 29 mai 2019 à 10:20, Mohamed Khalil BOUJDARIA <[hidden email]> a écrit :
Hi,

Yes, it fails even when index is valid..

Le mer. 29 mai 2019 à 10:16, Carlos Gomez <[hidden email]> a écrit :
Hi,

First thing is index always valid? I suggest you check it is different
from -1.

On 29/05/2019 09:45, Mohamed Khalil BOUJDARIA via gtkmm-list wrote:
> basic_string::_M_construct null not valid
_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list


--
Mohamed Khalil BOUJDARIA
Élève ingénieur en mécatronique
École nationale d'ingénieurs de Carthage
Tél : +21653283365/+21652054010


--
Mohamed Khalil BOUJDARIA
Élève ingénieur en mécatronique
École nationale d'ingénieurs de Carthage
Tél : +21653283365/+21652054010

_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Removing text from ComboBoxText

Gtkmm mailing list
In reply to this post by Gtkmm mailing list
so do a backtrace and see where that null string is coming from.


_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Removing text from ComboBoxText

Gtkmm mailing list
In reply to this post by Carlos Gomez
Yes, it simply not valid to construct an std::string, and hence nor a Glib::ustring, from a nullptr. So if any code in gtkmm is not converting a C nullptr string to a C++ "", that's a bug.

> Returns the currently active string in combo_box, or nullptr if none is selected.

Saying nullptr in documentation like this is simply wrong, because it gets auto-translated from C but we don't change that to "" as we should (and I opened a bug for). That might be easier said than done though.

Basically, any C nullptr should be mapped to a C++ empty string, and if it's not somewhere, there's probably a bug in the wrapping.



_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Removing text from ComboBoxText

Carlos Gomez

I have checked that and get_active_text is using

inline Glib::ustring
convert_return_gchar_ptr_to_ustring(char* str)
{
  return (str) ? Glib::ustring(Glib::make_unique_ptr_gfree(str).get()) : Glib::ustring();
}

So there is no problem with that

And remove_text is

void ComboBoxText::remove_text(int position)
{
  gtk_combo_box_text_remove(gobj(), position);
}

So, I do not understand what is going on.

On 29/05/2019 11:15, Daniel Boles via gtkmm-list wrote:

Yes, it simply not valid to construct an std::string, and hence nor a Glib::ustring, from a nullptr. So if any code in gtkmm is not converting a C nullptr string to a C++ "", that's a bug.

> Returns the currently active string in combo_box, or nullptr if none is selected.

Saying nullptr in documentation like this is simply wrong, because it gets auto-translated from C but we don't change that to "" as we should (and I opened a bug for). That might be easier said than done though.

Basically, any C nullptr should be mapped to a C++ empty string, and if it's not somewhere, there's probably a bug in the wrapping.



_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list

_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Removing text from ComboBoxText

Gtkmm mailing list
The sure fire way to figure out what is going on is always to make complete use of the debugger, i.e. in this case to get and analyse a backtrace.


_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
Reply | Threaded
Open this post in threaded view
|

Re: Removing text from ComboBoxText

Gtkmm mailing list
...and if you aren't confident enough to debug, the way to get help is to provide a minimal test case that reproduces the problem, so that others can try to do that for you. Anyway, it looks like gtkmm is behaving just fine here, so I suspect something is going on between your callback and the rest of the code, e.g. if the captured widget pointer becomes invalidated or etc. (It's not at all clear why it gets the string locally and never uses it, but that shouldn't be a problem.) There's no point trying to guess when we can't see the rest of the code.


On Wed, 29 May 2019 at 11:45, Daniel Boles <[hidden email]> wrote:
The sure fire way to figure out what is going on is always to make complete use of the debugger, i.e. in this case to get and analyse a backtrace.


_______________________________________________
gtkmm-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtkmm-list