Skip to content

Commit 62bad63

Browse files
committed
#1546 Fix crash when using iter::get_var on chained iterators
1 parent 41a14e2 commit 62bad63

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

distr/flecs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33207,8 +33207,8 @@ inline flecs::entity iter::get_var(int var_id) const {
3320733207
* Get value of a query variable for current result.
3320833208
*/
3320933209
inline flecs::entity iter::get_var(const char *name) const {
33210-
ecs_query_iter_t *qit = &iter_->priv_.iter.query;
33211-
const flecs::query_t *q = qit->query;
33210+
const flecs::query_t *q = iter_->query;
33211+
3321233212
int var_id = ecs_query_find_var(q, name);
3321333213
ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, name);
3321433214
return flecs::entity(iter_->world, ecs_iter_get_var(iter_, var_id));

include/flecs/addons/cpp/impl/iter.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ inline flecs::entity iter::get_var(int var_id) const {
9595
* Get value of a query variable for current result.
9696
*/
9797
inline flecs::entity iter::get_var(const char *name) const {
98-
ecs_query_iter_t *qit = &iter_->priv_.iter.query;
99-
const flecs::query_t *q = qit->query;
98+
const flecs::query_t *q = iter_->query;
99+
100100
int var_id = ecs_query_find_var(q, name);
101101
ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, name);
102102
return flecs::entity(iter_->world, ecs_iter_get_var(iter_, var_id));

test/cpp/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@
526526
"multithread_system_w_query_iter",
527527
"multithread_system_w_query_iter_w_iter",
528528
"multithread_system_w_query_iter_w_world",
529+
"multithread_system_w_get_var",
529530
"run_callback",
530531
"startup_system",
531532
"interval_tick_source",

test/cpp/src/System.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,38 @@ void System_multithread_system_w_query_iter_w_world(void) {
18561856
test_int(p->y, 22);
18571857
}
18581858

1859+
struct Rel { };
1860+
1861+
void System_multithread_system_w_get_var(void) {
1862+
flecs::world world;
1863+
world.set_threads(4);
1864+
1865+
flecs::entity bob = world.entity("bob").add<Position>();
1866+
flecs::entity alice = world.entity("alice").add<Position>();
1867+
1868+
bob.add<Rel>(alice);
1869+
1870+
int count = 0;
1871+
1872+
world.system<const Position>()
1873+
.with<Rel>("$other")
1874+
.term_at(0).src("$other")
1875+
.multi_threaded()
1876+
.each([&](flecs::iter &it, size_t row, const Position &pos) {
1877+
flecs::entity e = it.entity(row);
1878+
flecs::entity other = it.get_var("other");
1879+
1880+
test_assert(e == bob);
1881+
test_assert(other == alice);
1882+
1883+
count ++;
1884+
});
1885+
1886+
world.progress();
1887+
1888+
test_int(count, 1);
1889+
}
1890+
18591891
void System_run_callback(void) {
18601892
flecs::world world;
18611893

test/cpp/src/main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ void System_multithread_system_w_query_each_w_world(void);
508508
void System_multithread_system_w_query_iter(void);
509509
void System_multithread_system_w_query_iter_w_iter(void);
510510
void System_multithread_system_w_query_iter_w_world(void);
511+
void System_multithread_system_w_get_var(void);
511512
void System_run_callback(void);
512513
void System_startup_system(void);
513514
void System_interval_tick_source(void);
@@ -3405,6 +3406,10 @@ bake_test_case System_testcases[] = {
34053406
"multithread_system_w_query_iter_w_world",
34063407
System_multithread_system_w_query_iter_w_world
34073408
},
3409+
{
3410+
"multithread_system_w_get_var",
3411+
System_multithread_system_w_get_var
3412+
},
34083413
{
34093414
"run_callback",
34103415
System_run_callback
@@ -7082,7 +7087,7 @@ static bake_test_suite suites[] = {
70827087
"System",
70837088
NULL,
70847089
NULL,
7085-
73,
7090+
74,
70867091
System_testcases
70877092
},
70887093
{

0 commit comments

Comments
 (0)