GNOME Bugzilla – Bug 640555
Can't run foreach{} on a null iterable variable
Last modified: 2011-01-26 22:26:26 UTC
If I have a GeeCollection which is null and try to iterate over it using foreach{}, libgee crashes. It would be nice if it gracefully treated null collections as empty. i.e. The following Vala: Gee.Collection<Individual> foo = null; foreach (var i in foo) { // Stuff } Gives (approximately) the following C: _tmp1_ = gee_iterable_iterator ((GeeIterable*) foo); and the gee_iterable_iterator() call crashes: Program received signal SIGSEGV, Segmentation fault. 0x00007ffff6271a4a in gee_iterable_iterator (self=0x0) at iterable.c:77 77 return GEE_ITERABLE_GET_INTERFACE (self)->iterator (self);
(In reply to comment #0) > If I have a GeeCollection which is null and try to iterate over it using > foreach{}, libgee crashes. It would be nice if it gracefully treated null > collections as empty. > > i.e. The following Vala: > > Gee.Collection<Individual> foo = null; > Gee.Collection<Individual> foo = Collection.empty (); > foreach (var i in foo) { > // Stuff > } > > Gives (approximately) the following C: > > _tmp1_ = gee_iterable_iterator ((GeeIterable*) foo); > > and the gee_iterable_iterator() call crashes: > > Program received signal SIGSEGV, Segmentation fault. > 0x00007ffff6271a4a in gee_iterable_iterator (self=0x0) at iterable.c:77 > 77 return GEE_ITERABLE_GET_INTERFACE (self)->iterator (self); It is not libgee problem - it is vala as foreach is syntax sugar. Internally it tries to call ((Gee.Collection)null).iterator () which results in segfault. Vala does not support calling any methods on nulls not mentioning the interfaces and I don't think it will ever will (and hardly any other language do). In any case there is workaround with the .empty() which you may find useful depending on problem. If you still think that is a bug please repopen it againt vala but I don't think it will be considered.
(In reply to comment #1) > (In reply to comment #0) > > If I have a GeeCollection which is null and try to iterate over it using > > foreach{}, libgee crashes. It would be nice if it gracefully treated null > > collections as empty. > > > > i.e. The following Vala: > > > > Gee.Collection<Individual> foo = null; > > > > Gee.Collection<Individual> foo = Collection.empty (); > > > foreach (var i in foo) { > > // Stuff > > } > > > > Gives (approximately) the following C: > > > > _tmp1_ = gee_iterable_iterator ((GeeIterable*) foo); > > > > and the gee_iterable_iterator() call crashes: > > > > Program received signal SIGSEGV, Segmentation fault. > > 0x00007ffff6271a4a in gee_iterable_iterator (self=0x0) at iterable.c:77 > > 77 return GEE_ITERABLE_GET_INTERFACE (self)->iterator (self); > > It is not libgee problem - it is vala as foreach is syntax sugar. Internally it > tries to call ((Gee.Collection)null).iterator () which results in segfault. > > Vala does not support calling any methods on nulls not mentioning the > interfaces and I don't think it will ever will (and hardly any other language > do). In any case there is workaround with the .empty() which you may find > useful depending on problem. > > If you still think that is a bug please repopen it againt vala but I don't > think it will be considered. Fair enough, that makes sense. Unfortunately, .empty() won't work for us, so we'll just continue to wrap our foreach blocks in if statements. Thanks.