for (int i=0;i<10;i++)
[myMutableArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"img000%d.png",(start+i+1)] ofType:nil]]];
self.myImageView.animationImages=myMutableArray;
[myMutableArray removeAllObjects];
//[myMutableArray release];
I have around 50 images and I load only 10 at a time for a purpose, I am controlling this my "start" variable which contains the initial point. And I want to free previous loaded images while loading new ones.
But when I uncomment the release method My program crashes..
And if I do not release My memory is not being freed, as I checked in Instruments, although there is not leak in Leaks but still object allocation goes on and on with overall system memory of 100 MB. And because of this I get Low Memory Warning in program when I test in the device. I don't know what is the problem here. Please let me know if anyone has resolution.
ok... i really dont know if this is right, but it may be worth checking...
you have to set the array to nil before you try and release it dont you? is removeAllObjects the same as setting to nil?
------------------------
You mean to say instead of
[myMutableArray release];
I should do
myMutableArray=nil;
But this will just put the nil value at myMutableArray pointer without freeing the memory, I guess..
The removeAllObjects method removes all the objects present in the array as per the Apple documentation. It doesn't release the array, I guess.
actually "animationImages" is the copy property of UIImageView and myImageView is the reference of UIImageView, so all the date in myMutableArray will get copied to it when i assign. Hence I want to free the data of myMutableArray because there will be 2 copies of same data in the memory... right?
Quote:
Originally Posted by FlyingDiver
The program is probably crashing because your myImageView class is trying to use the image array you just released. Let's see the code for it.
I assume animationImages is a property with a "copy" attribute? Otherwise, why would you be setting it and then immediately emptying the array?
If it's a retain, then your imageView now has an empty array in "animationImages", which is most likely not what you wanted.
Also, where exactly is the program crashing? What exception are you getting?
yess the "animationImages" is the copy property of UIImageView of which the reference is "myImageView". Program crashes when I uncomment release method without giving any exception, but when I trace in debug mode I got to know that NSAutoReleasePool fails...
but when I comment the release method then program works fine but i get low memory warning call back..
yess the "animationImages" is the copy property of UIImageView of which the reference is "myImageView". Program crashes when I uncomment release method without giving any exception, but when I trace in debug mode I got to know that NSAutoReleasePool fails...
but when I comment the release method then program works fine but i get low memory warning call back..
why copy attribute?
I bet if you comment out
self.myImageView.animationImages=myMutableArray;
Your "copy" property of NSMutableArray won't add reference count to the objects in the array.
Those UIImage are created with autorelease. Their reference count +1 when you add them to the array, but when you removeAllObjects (or the array release), their reference count -1. And then, at the end of autorelease pool, they are dealloc'd.
Which means your animationImages array contains pointers which are invalid.
Your "copy" property of NSMutableArray won't add reference count to the objects in the array.
Actually, it does add to the reference count. It's a shallow copy, but retains are sent to the objects.
Quote:
Their reference count +1 when you add them to the array, but when you removeAllObjects (or the array release), their reference count -1. And then, at the end of autorelease pool, they are dealloc'd.
But then he should get the same behavior regardless of whether he removes all objects or releases the array. But he says he is not getting the same behavior.
Sanniv, can you show us the header file for the class of myImageView? (and please use code tags)
Actually, it does add to the reference count. It's a shallow copy, but retains are sent to the objects.
But then he should get the same behavior regardless of whether he removes all objects or releases the array. But he says he is not getting the same behavior.
Sanniv, can you show us the header file for the class of myImageView? (and please use code tags)
If myImageView is just a pointer to an UIImageView, where is animationImages defined? I think everyone is assuming is an instance variable of your custom class, but that might not be the case. If myImageView is not a custom class, then what does this mean?
Quote:
yess the "animationImages" is the copy property of UIImageView of which the reference is "myImageView".
Ah, OK, I was thinking that was your own class. So you're just using a regular UIImageView.
Ok, so then I don't see the problem. Memory management bugs are tricky, because ultimately you need to have a balance between retains and releases, right. So if you have too many releases (which it sounds like is the issue here), then removing any one of the releases will mask the problem, but not solve it.
So I think the issue is somewhere else in your code. My guess is the images themselves are being released somewhere else where they shouldn't be.
If myImageView is just a pointer to an UIImageView, where is animationImages defined?
It's a property of UIImageView. I thought he had a custom class and that he was somehow inadvertently masking that property, which is why I asked for the header file. But that's not the issue, he's just using a regular old UIImageView.
It's a property of UIImageView. I thought he had a custom class and that he was somehow inadvertently masking that property, which is why I asked for the header file. But that's not the issue, he's just using a regular old UIImageView.
(1) If animationImages makes a copy of the array, it doesn't matter what you do to the old array. If animationImages just retains the old array, then you shouldn't remove all of the images.
Either way, don't do
[myMutableArray removeAllObjects]
just do
[myMutableArray release]
If this is truly the LAST RELEASE, then the dealloc method will call removeAllObjects anyway.
(2) Are you sure the images are really getting loaded? You may want to add a breakpoint after the array gets filled up, and see if it really has items in it. There are lots of ways that this could go wrong:
stringWithFormat:@"img000%d.png",(start+i+1)
For example, you're taking "i" from zero to nine, but then you're adding one. And is "start" zero, or something else? If start is "200" (for example) then you're asking for images:
i'm also having difficulty grasping how mutable arrays should be released.
what's the proper method to release your mutable arrays??
assume the array is created as follows (just rough code here).
Code:
-(void) viewDidLoad
{
NSMutableArrray* mutable_array=[NSMutableArrray alloc] initWithCapacity:0];
for (loop)
{
blah* object=[blah alloc] init];
... set some stuff in object ...
[mutable_array addObject: object];
[object release];
}
// are the next two lines ok to do ? wrt reference counts, etc.
self.myMutable= mutable_array;
[mutable_array release];
}
-(void) dealloc
{
[self.myMutable release];
}
is that right? or should i loop through self.myMutable and remove the objects first? like
Code:
-(void) dealloc
{
for (object in self.myMutable)
{
[self.myMutable removeObject: object];
}
[self.myMutable release];
}
// are the next two lines ok to do ? wrt reference counts, etc.
self.myMutable= mutable_array;
[mutable_array release];
}
Yes, assuming myMutable is a property with "retain" semantics. If it is "copy", then it's still OK from a memory management perspective, but just keep in mind that "copy" will give you an immutable copy, unless you provide your own setMyMutable method. The one you get for free with @synthesize will create an immutable copy.
Quote:
Code:
-(void) dealloc
{
for (object in self.myMutable)
{
[self.myMutable removeObject: object];
}
[self.myMutable release];
}
It's not necessary to do that. NSArray's own dealloc method will remove all objects (sending each of them releases)