From 2b25748cc40a910c0a8a651f655757e48b3e690a Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Thu, 23 Dec 2021 19:43:27 +0900 Subject: [PATCH] [ruamoko] Correct SetIterator memory handling This makes SetIterator more compatible with autorelease. --- libs/ruamoko/rua_set.c | 1 + ruamoko/include/Set.h | 7 +++++-- ruamoko/lib/Set.r | 18 +++++++++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/libs/ruamoko/rua_set.c b/libs/ruamoko/rua_set.c index 8bc1915a9..4e8b752b5 100644 --- a/libs/ruamoko/rua_set.c +++ b/libs/ruamoko/rua_set.c @@ -52,6 +52,7 @@ typedef struct { typedef struct { pr_id_t obj; + pr_int_t set; pr_int_t iter; } pr_set_iter_t; diff --git a/ruamoko/include/Set.h b/ruamoko/include/Set.h index 1f7c39177..2e020755b 100644 --- a/ruamoko/include/Set.h +++ b/ruamoko/include/Set.h @@ -1,8 +1,8 @@ #ifndef __ruamoko_Set_h #define __ruamoko_Set_h -typedef struct set_s *set_t; -typedef struct set_iter_s *set_iter_t; +typedef struct set_s set_t; +typedef struct set_iter_s set_iter_t; @extern void set_del_iter (set_iter_t *set_iter); @extern unsigned set_iter_element (set_iter_t *set_iter); @@ -32,8 +32,11 @@ typedef struct set_iter_s *set_iter_t; #include +@class Set; + @interface SetIterator: Object { + Set *set; set_iter_t *iter; } - (SetIterator *) next; diff --git a/ruamoko/lib/Set.r b/ruamoko/lib/Set.r index 393bb31ac..7afc13361 100644 --- a/ruamoko/lib/Set.r +++ b/ruamoko/lib/Set.r @@ -27,20 +27,32 @@ string set_as_string (set_t *set) = #0; @implementation SetIterator: Object -- initWithIterator: (set_iter_t *) iter + +- initWithSet: (Set *)set iterator:(set_iter_t *) iter { if (!(self = [super init])) { return nil; } + self.set = [set retain]; self.iter = iter; return self; } ++ (id) withSet: (Set *)set iterator:(set_iter_t *) iter +{ + return [[[self alloc] initWithSet: set iterator: iter] autorelease]; +} + +- (void) dealloc +{ + [set release]; + [super dealloc]; +} + - (SetIterator *) next { if ((iter = set_next (iter))) return self; - [self dealloc]; return nil; } @@ -93,7 +105,7 @@ string set_as_string (set_t *set) = #0; if (!iter) return nil; - iterator = [[SetIterator alloc] initWithIterator: iter]; + iterator = [SetIterator withSet: self iterator: iter]; return iterator; }