[ruamoko] Correct SetIterator memory handling

This makes SetIterator more compatible with autorelease.
This commit is contained in:
Bill Currie 2021-12-23 19:43:27 +09:00
parent 7571231100
commit 2b25748cc4
3 changed files with 21 additions and 5 deletions

View file

@ -52,6 +52,7 @@ typedef struct {
typedef struct { typedef struct {
pr_id_t obj; pr_id_t obj;
pr_int_t set;
pr_int_t iter; pr_int_t iter;
} pr_set_iter_t; } pr_set_iter_t;

View file

@ -1,8 +1,8 @@
#ifndef __ruamoko_Set_h #ifndef __ruamoko_Set_h
#define __ruamoko_Set_h #define __ruamoko_Set_h
typedef struct set_s *set_t; typedef struct set_s set_t;
typedef struct set_iter_s *set_iter_t; typedef struct set_iter_s set_iter_t;
@extern void set_del_iter (set_iter_t *set_iter); @extern void set_del_iter (set_iter_t *set_iter);
@extern unsigned set_iter_element (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 <Object.h> #include <Object.h>
@class Set;
@interface SetIterator: Object @interface SetIterator: Object
{ {
Set *set;
set_iter_t *iter; set_iter_t *iter;
} }
- (SetIterator *) next; - (SetIterator *) next;

View file

@ -27,20 +27,32 @@ string set_as_string (set_t *set) = #0;
@implementation SetIterator: Object @implementation SetIterator: Object
- initWithIterator: (set_iter_t *) iter
- initWithSet: (Set *)set iterator:(set_iter_t *) iter
{ {
if (!(self = [super init])) { if (!(self = [super init])) {
return nil; return nil;
} }
self.set = [set retain];
self.iter = iter; self.iter = iter;
return self; 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 - (SetIterator *) next
{ {
if ((iter = set_next (iter))) if ((iter = set_next (iter)))
return self; return self;
[self dealloc];
return nil; return nil;
} }
@ -93,7 +105,7 @@ string set_as_string (set_t *set) = #0;
if (!iter) if (!iter)
return nil; return nil;
iterator = [[SetIterator alloc] initWithIterator: iter]; iterator = [SetIterator withSet: self iterator: iter];
return iterator; return iterator;
} }