1

How would one pass a NSMutableArray via method return.

I have it passing the array "spaces" so an array of 10 objects passes the 10 blocks but none of the information contained in those objects.

Thanks in advance

Edit: Basically I created another class that contains path information because my controller was getting a bit cluttered. So this new class I want call the "create" method which returns an NSMutableArray. The array is created fine in the path class but when the return statement fires it only passes the spaces and not the values or even a pointer.

currently it's

return path;

I've tried

return &path; 

and that fails epically.

Edit2: Here is the issue I'm having unfortunately.

enter image description here

enter image description here

Still crashing

calling

newNode = [newNode copy]; 

causes a crash

8
  • Can you provide some code? Or describe the problem in detail? Commented Feb 28, 2011 at 18:46
  • You cannot add a block to an NSMutableArray as a block is not an object, or am I missing something? Commented Feb 28, 2011 at 18:48
  • Updated above on what I'm trying to do with this class, if you need code I can provide that as well but it would make this grow quite a bit... Commented Feb 28, 2011 at 18:50
  • @Dave DeLong might be, but at least I have never seen a block conforming to the NSObject protocol, and they are not a root class either (all root classes in Foundation conform to NSObject). Commented Feb 28, 2011 at 19:04
  • 1
    Your code indicates a lack of understanding about pointer-based memory management. Please read boredzo.org/pointers on what a pointer is and what it means for your code. Commented Feb 28, 2011 at 20:15

2 Answers 2

6
- (NSMutableArray *) mutableFloobizwits {
  NSMutableArray *array = [NSMutableArray array];
  for (NSInteger i = 0; i < TheAnswerToTheUltimateQuestion; ++i) {
    void(^MyBlock)(void) = ^{
      NSLog(@"captured i: %ld", i);
    };
    MyBlock = [MyBlock copy];  //move the block from off the stack and onto the heap
    [array addObject:[Floobizwit floobizwithWithBlock:MyBlock]];
    [MyBlock release]; //the Floobizwit should've -retained the block, so we release it
  }
  return array;
}
Sign up to request clarification or add additional context in comments.

5 Comments

This is almost working but the objects inside of the Array are out of scope still.
@Mytheral When you say out of scope, do you mean they have been released? The memory for the path objects should be held by the array you create and return.
@Mytheral if you add a block to an array, you have to make sure it's been -copy'd first. I've updated the answer to show how that's done.
Not sure it makes a difference but the block is actually an instance of a custom class with other custom classes and an array in it. It also crashes when I do the newNode = [newNode copy];
This gave me a good start down the path I needed to go :) This step by step took me the last step I needed: techotopia.com/index.php/Copying_Objects_in_Objective-C
1

I would set up your other class that returns the array of path objects as follows:

@implementation PathFactory

- (NSMutableArray*) create
{
    // In your PathFactory object you create an array and make it autorelease so 
    // it becomes the callers responsibility to free the memory 
    NSMutableArray * pathArray = [[[NSMutableArray alloc] init] autorelease];

    // Create a bunch of PathObject objects and add them to the mutable array
    // also set these to autorelease because the NSMutableArray will retain objects
    // added to the collection (ie It is the NSMutableArray's responsibility to ensure
    // the objects remain allocated).
    for (int i = 0; i < numberOfPaths; i++)
        [pathArray addObject:[[[PathObject alloc] init] autorelease]];

    // Just return the pointer to the NSMutableArray. The caller will need to 
    // call the retain message on the pointer it gets back (see next)
    return pathArray;
}

@end

So in your caller code:

// create a tempory PathFactory (autorelease will make sure it is cleaned up when we
// are finished here)
PathFactory * newPathFactory = [[[PathFactory alloc] init] autorelease];
// grab the new array of Path objects and retain the memory. _newPathArray
// is a member of this class that you will need to release later.
_newPathArray = [[newPathFactory create] retain];

1 Comment

no joy here either :( Included a screen shot of the problem up above

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.