Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-29-2012, 05:14 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 70
juanchofern is on a distinguished road
Thumbs up Release an array of uiimageviews

Hello i have an array of uiimage views in a function.

But the problem is when i call the function is sometimes i would like
to change the images, so how could i do it and releasing the memory
to avoid memory leaks.

this is my function

fullarray
{
imageBolaGris = [ImageCache loadImage:[ficherosBolasGrises objectAtIndex:i]];

tBolasGrises[i] = [[UIImageView alloc] initWithImage:imageBolaGris];
}

Thanks a lot for your help
juanchofern is offline   Reply With Quote
Old 01-30-2012, 02:41 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 70
juanchofern is on a distinguished road
Default

FicherosBolasGrises is a NSMutable array of strings where i save the names
Of the images files.

In another function if i need i change the images in FicheroBolasGrises,
The problem is when i call the function to assing the images to the array
Of image views because i have not releases before, how i could release the array of image views to avoid memory leaks?
Any help?

Thanks a lot


Quote:
Originally Posted by juanchofern View Post
Hello i have an array of uiimage views in a function.

But the problem is when i call the function is sometimes i would like
to change the images, so how could i do it and releasing the memory
to avoid memory leaks.

this is my function

fullarray
{
imageBolaGris = [ImageCache loadImage:[ficherosBolasGrises objectAtIndex:i]];

tBolasGrises[i] = [[UIImageView alloc] initWithImage:imageBolaGris];
}

Thanks a lot for your help
juanchofern is offline   Reply With Quote
Old 01-30-2012, 10:03 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by juanchofern View Post
Hello i have an array of uiimage views in a function.

But the problem is when i call the function is sometimes i would like
to change the images, so how could i do it and releasing the memory
to avoid memory leaks.

this is my function

fullarray
{
imageBolaGris = [ImageCache loadImage:[ficherosBolasGrises objectAtIndex:i]];

tBolasGrises[i] = [[UIImageView alloc] initWithImage:imageBolaGris];
}

Thanks a lot for your help

Is tBolasGrises a C style array? The code above implies that it is.

I would advise against using a C style array to manage objects. That requires that you manage object ownership yourself.


if tBolasGrises was an NSMutableArray, your code would look like this:

Code:
UIImageView *anImageView = [[UIImageView alloc] initWithImage:imageBolaGris];
[tBolasGrises addObject: anImageView];
[anImageView release];

NSArrays and mutable arrays retain the objects that they contain. By adding anImageView to the NSMutableArray, the array becomes the owner.

The code above creates the image view using an alloc call, which returns an object you own and must release.

We pass ownership of the image view to the array, and then release the object.

After that code, only the array owns the image view. That's what you want. Then, if you later remove the object from the array, the array will release it and it will be deallocated.

If you release the array, all of the objects in the array will be released and deallocated.

Note that you usually add image views as subviews in your view hierarchy. Adding a view as a subview of another view also retains that view.

If you add an image view to an array AND add it as a subview of your view controller, it will be retained twice. To release it and free it up, you would need to both remove it from the array and remove it from it's parent view (or superview, as Cocoa touch calls it.) That's ok, but you need to make sure your code removes it from it's superview.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-30-2012, 10:26 AM   #4 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 70
juanchofern is on a distinguished road
Default

Ok Duncan, hello and thanks a lot. I am going to try the way you do, i have declared tBolasGrises as C style array in .h

UIImageView *tBolasGrises[91];

Thanks again for the advice.
juanchofern is offline   Reply With Quote
Old 01-30-2012, 11:09 AM   #5 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 70
juanchofern is on a distinguished road
Thumbs up

Quote:
Originally Posted by Duncan C View Post
Is tBolasGrises a C style array? The code above implies that it is.

I would advise against using a C style array to manage objects. That requires that you manage object ownership yourself.


if tBolasGrises was an NSMutableArray, your code would look like this:

Code:
UIImageView *anImageView = [[UIImageView alloc] initWithImage:imageBolaGris];
[tBolasGrises addObject: anImageView];
[anImageView release];

NSArrays and mutable arrays retain the objects that they contain. By adding anImageView to the NSMutableArray, the array becomes the owner.

The code above creates the image view using an alloc call, which returns an object you own and must release.

We pass ownership of the image view to the array, and then release the object.

After that code, only the array owns the image view. That's what you want. Then, if you later remove the object from the array, the array will release it and it will be deallocated.

If you release the array, all of the objects in the array will be released and deallocated.

Note that you usually add image views as subviews in your view hierarchy. Adding a view as a subview of another view also retains that view.

If you add an image view to an array AND add it as a subview of your view controller, it will be retained twice. To release it and free it up, you would need to both remove it from the array and remove it from it's parent view (or superview, as Cocoa touch calls it.) That's ok, but you need to make sure your code removes it from it's superview.
Hello Duncan again,
i have created a NSMutableArray as you said me.

But now how can i acces to the properties of an UIImageView.

Before i did tBolasGrises[i].hidden=Yes.

But now i cannot do [tBolasGrises ObjectAtIndex:i].hidden=Yes
i suppose because is a array of objects.
How can i do it?

Thanks a lot for your help!!!
juanchofern is offline   Reply With Quote
Old 01-30-2012, 01:41 PM   #6 (permalink)
Registered Member
 
apatsufas's Avatar
 
Join Date: Jan 2011
Location: Thessaloniki, Greece
Posts: 121
apatsufas is on a distinguished road
Default

Yes you can't do that. You need to cast the object to an UIImageView.

Code:
(UIImageView*)[yourArray objectAtIndex:i]
__________________
SQLed - Your Database Manager on the go

iAZConverter - Converts everything from A to Z
apatsufas is offline   Reply With Quote
Old 01-30-2012, 02:33 PM   #7 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 70
juanchofern is on a distinguished road
Thumbs up

Quote:
Originally Posted by apatsufas View Post
Yes you can't do that. You need to cast the object to an UIImageView.

Code:
(UIImageView*)[yourArray objectAtIndex:i]
Ok thanks a lot, but i must change a lot of code.
so i would to try with the code i have.

Do you know how can i release the array of uimageviews when i want
to change the images?

The array is Style Old C.

UIImageView tBolasGrandes[5];


this is my code

for (int i =0; i<5;i++)
{
imageBolaGrande = [ImageCache loadImage:[ficherosBolasGrandes objectAtIndex:i]];

tBolasGrandes[i] = [[UIImageView alloc] initWithImage:imageBolaGrande];
}

so in another function i would like to change ficherosbolasGrandes to assign
new images.

ficherosBolasGrandes is a NSMutableArray. This is not a problem, i can
release ficherosBolasGrandes and add new images.

Then after i would like to release tBolasGrandes to avoid memory leaks and assign the new images from ficherosBolasGrandes.

Could you help me please?

Thanks a lot, and regards.
juanchofern is offline   Reply With Quote
Old 01-30-2012, 03:26 PM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by juanchofern View Post
Ok thanks a lot, but i must change a lot of code.
so i would to try with the code i have.

Do you know how can i release the array of uimageviews when i want
to change the images?

The array is Style Old C.

UIImageView tBolasGrandes[5];


this is my code

for (int i =0; i<5;i++)
{
imageBolaGrande = [ImageCache loadImage:[ficherosBolasGrandes objectAtIndex:i]];

tBolasGrandes[i] = [[UIImageView alloc] initWithImage:imageBolaGrande];
}

so in another function i would like to change ficherosbolasGrandes to assign
new images.

ficherosBolasGrandes is a NSMutableArray. This is not a problem, i can
release ficherosBolasGrandes and add new images.

Then after i would like to release tBolasGrandes to avoid memory leaks and assign the new images from ficherosBolasGrandes.

Could you help me please?

Thanks a lot, and regards.

As the other poster said, you have to type cast an item from the array to the desired type before you can change it's properties:

Code:
((UIImageView*)[yourArray objectAtIndex:i]).hidden = TRUE;
Using a C array of objects is a lot more work, and much more prone to errors.

You need to:

Initialize the array elements to nil in your init method.

Before you store a new value into the C array, release the old value and set it to nil.

Add code in your dealloc method that loops through the C array and releases all the objects.

It gets even messier if you're using ARC, since ARC doesn't support object pointers in C arrays or structures.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-30-2012, 04:31 PM   #9 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 70
juanchofern is on a distinguished road
Smile

Quote:
Originally Posted by Duncan C View Post
As the other poster said, you have to type cast an item from the array to the desired type before you can change it's properties:

Code:
((UIImageView*)[yourArray objectAtIndex:i]).hidden = TRUE;
Using a C array of objects is a lot more work, and much more prone to errors.

You need to:

Initialize the array elements to nil in your init method.

Before you store a new value into the C array, release the old value and set it to nil.

Add code in your dealloc method that loops through the C array and releases all the objects.

It gets even messier if you're using ARC, since ARC doesn't support object pointers in C arrays or structures.
Thanks a lot Duncan, you are very nice, ok tomorrow i ll try as you said, i will change a lot of code,

Regards!!!
juanchofern is offline   Reply With Quote
Old 01-30-2012, 07:35 PM   #10 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 70
juanchofern is on a distinguished road
Thumbs up

Quote:
Originally Posted by Duncan C View Post
As the other poster said, you have to type cast an item from the array to the desired type before you can change it's properties:

Code:
((UIImageView*)[yourArray objectAtIndex:i]).hidden = TRUE;
Using a C array of objects is a lot more work, and much more prone to errors.

You need to:

Initialize the array elements to nil in your init method.

Before you store a new value into the C array, release the old value and set it to nil.

Add code in your dealloc method that loops through the C array and releases all the objects.

It gets even messier if you're using ARC, since ARC doesn't support object pointers in C arrays or structures.
Hello Duncan, with this question i think i finish the program

How can i do this sentence if both arrays are NSMutableArray

tUltimasCincoBolas[ContadorUltimasBolas]=tBolasPeques[bola-1];

Thanks a lot!!!
juanchofern is offline   Reply With Quote
Old 01-30-2012, 07:38 PM   #11 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 70
juanchofern is on a distinguished road
Default

Quote:
Originally Posted by juanchofern View Post
Hello Duncan, with this question i think i finish the program

How can i do this sentence if both arrays are NSMutableArray

tUltimasCincoBolas[ContadorUltimasBolas]=tBolasPeques[bola-1];

Thanks a lot!!!
i have done it with this

[tUltimasCincoBolas replaceObjectAtIndex:ContadorUltimasBolas withObject: [tBolasPeques objectAtIndex:bola-1]];

Thanks a lot!!!!
juanchofern is offline   Reply With Quote
Reply

Bookmarks

Tags
array, no leaks, release memory, uimageviews

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
11 members and 391 guests
Atatator, condor304, FrankWeller, imac74, MAMN84, mraalex, n00b, PowerGoofy, QuantumDoja, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:59 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0