Passing Pointer to Objects In and Out.

boyfarrell

Registered
Hi folks,

I'm trying to pass out a C array of objects from a particular method but I am having memory problems. Strangely, this works when I autorelease the objects but not when I pass them out and release them from the main.m? So my question is, is the autorelease actually working here? As I can't do it manually I'm a bit concerned.

Code:
-(Vector**) doSomething
{
	Vector *result[5];
	unsigned int i,f;
	for(i=0; i < 5; i++)
	{
		result[i] = [[Vector alloc] initWithElements:5];
		
			for(f=0; f < 5; f++)
			{	
				[result[i] setObjectAtIndex:f To:f];
				
			}
	}
		
	return result;
}

A Vector object is a C array of doubles of number elements as defined during initalisation, above the Vectors are all 5 elements long, i.e. I have malloc enough space for each to hold 5 doubles.

The -setObjectAtIndex:To: method then takes one of these Vector objects and fills the 5 elements with values.

I then return a pointer pointing to the array of Vector objects.

Inside the main I pickup the what I have just returned:
Code:
	main.m---->

     Vector *A = [[Vector alloc] initWithElements:5]; //dummy object
	
	Vector **array;      //A pointer to a pointer of Vector objects
	
	array = [A doSomething];    //the dummy instance calls the above method

    	unsigned int i;    
	for(i=0; i < 5; i++)
		printf("retainCount (outside) = %d\n",[array[i] retainCount]);

The first like a make a dummy Vector object, this is just so I can call the method. As the method doesn't actually do anything with the callers instance variables this is okay.

I then make my pointer to pointer of Vector objects.

Call the above method.

I then loop through the array of Vector objects asking each one for the current retainCount. it gives this output, it can only ask the first two before crashing this seem random!
Code:
retainCount (outside) = 1
retainCount (outside) = 1

Dynamic has exited due to signal 10 (SIGBUS).

Should I just go back to alloc and init-ing with autorelease? Can I trust it?
 
Back
Top