If you create your dictionary with [[NSDictionary alloc] init], it will be
retained by default. If you use one of the class methods though, it gets
autoreleased. That means it'll work first time, then when your code finishes
running it gets released and the memory is freed. The next time your code
runs, it crashes if it tries to use the dictionary which no longer exists.

So yeah, retaining it prevents it from being autoreleased, that's all. The
golden rule is always: class methods = autorelease, alloc = retained.

Chris


On 28 September 2010 14:24, Stefan Kainbacher, NEON GOLDEN <
[email protected]> wrote:

> hi adrian,
>
> thanks for your advice. i also read quite a bit on it, but still i'm not
> looking completely through ...
>
> ad 1) i want to keep the values of the dictionary over the whole process
> ... now there is just random dummy data but i want to keep the state of the
> entries ...
>
> ad 2) alright, thats what i thought and expected, but why doesnt it work
> then without the retain? thats actually my problem with understanding ...
>
> ad 3) thats also clear so far, but if i retain it and release it afterwards
> it should be even and ok ... i thought ...
>
>
> so my problem is still. why doesnt it work without the retain? it should as
> far as i understand the memory management concept ...
>
> thx a lot, stefan
>
>
>
> On Sep 28, 2010, at 2:56 PM, Adrian Ward wrote:
>
> >
> > This is more of an Cocoa programming discussion than QC, but read up on
> memory management to get a handle on why this is, and what the different
> techniques mean. You will suffer endlessly until you do!
> >
> >
> > The quick answers, if you're feeling lazy, are:
> >
> > 1. Don't create your dictionary in startExecution unless you really have
> to, you're complicating matters by creating an object and keeping it around.
> Creating a new one on each execute: is fairly lightweight, I doubt that's
> going to contribute much to your CPU load if that's what you're worried
> about.
> >
> > 2. [NSMutableDictionary new] is a shorthand for [[NSMutableDictionary
> alloc] init] which also retains the object in a non-GC environment. So no
> additional retain is needed (only a release, later).
> >
> > Using a convenience constructor like +[NSMutableDictionary dictionary]
> appears to be more complex because it'll give you an autoreleased object
> which could go on to cause a crash, but it makes your code easier to read
> because it's easy to balance a single retain with a single release. Also, if
> you set a property to an autoreleased object, the synthesized setter should
> retain and release it for you, automatically - so you get even simpler code
> and there's no need to retain or release yourself.
> >
> > 3. Whatever you do, don't continually retain an object on each execute:!
> You're just incrementing the retain counter madly. If your composition was
> to continually stop and start, you'd be leaking memory.
> >
> >
> > A.
> >
> >
> > On 28 Sep 2010, at 12:56, Stefan Kainbacher, NEON GOLDEN wrote:
> >
> >> cheers adrian, that was it. i changed it to the following ...
> >>
> >>
> >> - (BOOL) startExecution:(id<QCPlugInContext>)context
> >> {
> >>
> >>      particles = [NSMutableDictionary new];
> >>
> >>      return YES;
> >> }
> >>
> >>
> >> - (BOOL) execute:(id<QCPlugInContext>)context
> atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
> >> {
> >>
> >>      NSLog(@"Amount: %i", self.inputAmount);
> >>
> >>      //[particles removeAllObjects];
> >>
> >>      for(int i=0;i<self.inputAmount;i++){
> >>
> >>              NSDictionary *dictionary = [NSDictionary
> dictionaryWithObjectsAndKeys:
> >>
>  [NSString stringWithFormat:@"%i", rand()], @"x",
> >>
>  [NSString stringWithFormat:@"%i", rand()], @"y",
> >>
>  [NSString stringWithFormat:@"%i", rand()], @"z",
> >>
>  [NSString stringWithFormat:@"%i", rand()], @"random value",
> >>
>  nil];
> >>              [particles setObject:dictionary forKey:[NSString
> stringWithFormat:@"%i",i]];
> >>      }
> >>
> >>      //[self.outputParticles setDictionary:particles];
> >>
> >>      [particles retain];
> >>      self.outputParticles = particles;
> >>
> >>      return YES;
> >> }
> >>
> >>
> >> - (void) stopExecution:(id<QCPlugInContext>)context
> >> {
> >>
> >>      [particles release];
> >>
> >> }
> >>
> >>
> >> why do i have to retain it? is it because of the "particles =
> [NSMutableDictionary new];"? do i have to release it one more time, or ist
> correct now?
> >>
> >>
> >>
> >> On Sep 28, 2010, at 12:25 PM, Adrian Ward wrote:
> >>
> >>>
> >>> Are you sure you're retaining your particles dictionary so its not
> being autoreleased? I can't see where you've created it so cannot be
> certain. This sort of mistake often causes these sorts of crashes (works
> initially, doesn't shortly after).
> >>>
> >>> An easier and probably safer approach would be to just set your
> outputParticles property directly to a new dictionary that you create each
> time, rather than trying to maintain one mutable object over time, and then
> having to retain it, etc.
> >>>
> >>> And finally, to answer your source comment, no they're not the same -
> the first one changes the contents of your self.outputParticles dictionary
> to match those of particles. The second one assigns (depending on your
> @property declaration) the object directly to the property, which is what
> you're wanting.
> >>>
> >>> Try this:
> >>>
> >>> NSMutableDictionary* particles = [NSMutableDictionary dictionary];
> >>>
> >>> for(int i=0;i<self.inputAmount;i++){
> >>>     NSDictionary *dictionary = [NSDictionary
> dictionaryWithObjectsAndKeys:
> >>>
> [NSString stringWithFormat:@"%i", rand()], @"x",
> >>>
> [NSString stringWithFormat:@"%i", rand()], @"y",
> >>>
> [NSString stringWithFormat:@"%i", rand()], @"z",
> >>>
> [NSString stringWithFormat:@"%i", rand()], @"random value",
> >>>
> nil];
> >>>     [particles setObject:dictionary forKey:[NSString stringWithFormat:@
> "%i",i]];
> >>> }
> >>> self.outputParticles = particles;
> >>>
> >>>
> >>>
> >>>
> >>> Best,
> >>>
> >>> A.
> >>>
> >>>
> >>> On 28 Sep 2010, at 10:57, Stefan Kainbacher, NEON GOLDEN wrote:
> >>>
> >>>>
> >>>> hello together,
> >>>>
> >>>> i would like to build a plugin that has one input (number) and an
> output (structure).
> >>>>
> >>>> if i change the the number on the input it should give me a a
> dictionary or array with n=inputNumber elements. it works the first time and
> hands out the structure in qc, but when i change the input qc crashes. whats
> wrong? what do i miss?
> >>>>
> >>>>
> >>>> - (BOOL) execute:(id<QCPlugInContext>)context
> atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
> >>>> {
> >>>>
> >>>>    NSLog(@"Amount: %i", self.inputAmount);
> >>>>    //[particles removeAllObjects];
> >>>>
> >>>>    for(int i=0;i<self.inputAmount;i++){
> >>>>
> >>>>            NSDictionary *dictionary = [NSDictionary
> dictionaryWithObjectsAndKeys:
> >>>>
>  [NSString stringWithFormat:@"%i", rand()], @"x",
> >>>>
>  [NSString stringWithFormat:@"%i", rand()], @"y",
> >>>>
>  [NSString stringWithFormat:@"%i", rand()], @"z",
> >>>>
>  [NSString stringWithFormat:@"%i", rand()], @"random value",
> >>>>
>  nil];
> >>>>            [particles setObject:dictionary forKey:[NSString
> stringWithFormat:@"%i",i]];
> >>>>    }
> >>>>
> >>>>    [self.outputParticles setDictionary:particles];  // BTW: IS THIS
> THE SAME? self.outputParticles = particles;
> >>>>
> >>>>    return YES;
> >>>> }
> >>>>
> >>>>
> >>>> full source download (2.5 mb) available here:
> >>>>
> >>>>
> http://dl.dropbox.com/u/950822/Quartz%20Composer/NG%20ParticleSystem%2001.zip
> >>>>
> >>>>
> >>>> cheers, stefan
> >>>
> >>> _______________________________________________
> >>> Do not post admin requests to the list. They will be ignored.
> >>> Quartzcomposer-dev mailing list      (
> [email protected])
> >>> Help/Unsubscribe/Update your Subscription:
> >>>
> http://lists.apple.com/mailman/options/quartzcomposer-dev/stefan%40neongolden.net
> >>>
> >>> This email sent to [email protected]
> >>
> >> --
> >> NEW phone number: 0676 60 33 989
> >>
> >> --
> >> NEON GOLDEN
> >> VISUAL EXPERIMENT
> >>
> >> lab.neongolden.net
> >>
> >> --
> >> www.respectyourvj.net
> >>
> >>
> >>
> >>
> >>
> >
>
> --
> NEW phone number: 0676 60 33 989
>
> --
> NEON GOLDEN
> VISUAL EXPERIMENT
>
> lab.neongolden.net
>
> --
> www.respectyourvj.net
>
>
>
>
>
>  _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Quartzcomposer-dev mailing list      ([email protected])
> Help/Unsubscribe/Update your Subscription:
>
> http://lists.apple.com/mailman/options/quartzcomposer-dev/psonice%40gmail.com
>
> This email sent to [email protected]
>
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartzcomposer-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quartzcomposer-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to