[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Question on NSPointerArray +pointerArrayWithWeakObjects
From: |
Philippe Roussel |
Subject: |
Re: Question on NSPointerArray +pointerArrayWithWeakObjects |
Date: |
Fri, 04 Feb 2011 23:11:38 +0100 |
Le vendredi 04 février 2011 à 21:44 +0000, Richard Frith-Macdonald a
écrit :
> On 4 Feb 2011, at 21:23, Philippe Roussel wrote:
>
> > I've been browsing the Apple documentation for a long time but can't
> > find the answer so here's the question : is a NSPointerArray created
> > with pointerArrayWithWeakObjects supposed to retain the objects you add
> > to it ?
>
> No ...'weak', as I understand it, means that the garbage collector is free to
> collect the objects.
Ok, I didn't get the garbage collector stuff. Now I think I do.
> > In GNUstep +pointerArrayWithWeakObjects calls the init function with
> > NSPointerFunctionsZeroingWeakMemory which translate to retain but no
> > release.
>
> A test on OSX would be nice ... but in GNUstep currently, if you
> specify weak memory in a non-gc environment, the object is *not*
> retained.
Are you sure ? With NSPointerFunctionsZeroingWeakMemory the acquire
function is acquireRetainedObject which copies or retains the object.
> > Shouldn't it be NSPointerFunctionsZeroingWeakMemory |
> > NSPointerFunctionsOpaquePersonality ?
>
> No, because NSPointerFunctionsOpaquePersonality means that we
> shouldn't treat the pointers as objects, and the
> +pointerArrayWithWeakObjects: method is supposed to create an array
> for storing objects.
>
> But, everything I say is based on my reading of the documentation! My
> understanding of the documentation may not match what apple
> implemented.
> If you want definitive answers to these questions, please write more
> testcases for the testsuite at
> http://svn.gna.org/viewcvs/gnustep/tests/testsuite/trunk/base/NSPointerArray/
> We can run those on OSX, see how it behaves, and then alter GNUstep to
> match if necessary.
Ok, here are some tests that complete without error on my machine.
Index: base/NSPointerArray/weakobjects.m
===================================================================
--- base/NSPointerArray/weakobjects.m (révision 0)
+++ base/NSPointerArray/weakobjects.m (révision 0)
@@ -0,0 +1,27 @@
+#import "ObjectTesting.h"
+#import <Foundation/NSPointerArray.h>
+#import <Foundation/NSAutoreleasePool.h>
+
+int main()
+{
+ NSAutoreleasePool *arp = [NSAutoreleasePool new];
+ NSString *str;
+ NSPointerArray *obj;
+
+ obj = [NSPointerArray pointerArrayWithWeakObjects];
+ pass(obj != nil
+ && [obj isKindOfClass:[NSPointerArray class]]
+ && [obj count] == 0,
+ "+pointerArrayWithWeakObjects creates an empty pointer array");
+
+ str = [NSString stringWithString:@"Test"];
+ [obj addPointer: str];
+ pass([obj count] == 1, "-addPointer: increments count");
+ pass([str retainCount] == 2, "-addPointer: retains the object");
+ [obj removePointerAtIndex:0];
+ pass([str retainCount] == 2, "-removePointerAtIndex: doesn't release
the object");
+
+ [arp release]; arp = nil;
+ return 0;
+}
+