1

I have the following question.

Lets say i have a UIViewController Class named "ModuleViewController" which contains multiple dynamically created UIButtons and multiple dynamically created Mpmovieplayercontrollers. Each of these objects have their Callback functions, contained in the "ModuleViewController" Class. One Call Back for all the UIButtons and one for all The Mpmovieplayercontrollers.

Now i want to add multiple "ModuleViewController" Class instances inside a UIScrollview. I do not use ARC. If i release these objects after i allocate them and initialize them inside my "ModuleViewController" Class, the Buttons and the Videos do not play or rhe Application Crashs.

Currently my solution is to have an NSMutableArray to store their pointers when they are created, and then release them when i release the "ModuleViewController" Class at a later state. (e.x. Release the "ModuleViewController" Class when it is off screen from the viewport of the UIScrollview)

For example. If my array which holds the pointers is "objectsRetained"

// Creating the Array of fPointers on ViewDidLoad

NSMutableArray *objectsRetained = [[NSMutableArray alloc] init];

. . // Add Object Pointer inside the array to release it at later state

[objectsRetained addObject:[NSValue valueWithPointer: myObject]];

This solutions works but when i analyze my application, it shows that there is a posible Memory Leak in this area.

Is there another way to solve this?

karthika
  • 4,085
  • 3
  • 21
  • 23
hyphestos
  • 61
  • 1
  • 5

1 Answers1

0

First, use ARC.

But in this case, the problem seems to be in your extra NSValue wrapper. Just put the buttons in an NS(Mutable)Array directly. The array will retain the buttons for you. In -dealloc, call [buttons release]. That will release the buttons. There is no need to bundle these things into value objects.

And then switch to ARC. (But the approach is completely the same; you just don't need dealloc then.)

Community
  • 1
  • 1
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • The problem is that i need to keep an array with ViewControllers, in order to access some of their functions in a later state or remove them completely along with their views. So if i use ARC i suppose i have to declare them as weak objects. But then how should i store them inside an array? I tried to use the [NSValue valueWithNonretainedObject:object] method, but the application keeps crashing when i try to reference to them. – hyphestos Sep 30 '13 at 07:48
  • That strongly suggests you are not following an MVC pattern. There should be very little in a VCs you need to access from the outside. Move your data (and these functions) into a model layer and out of the VCs. – Rob Napier Sep 30 '13 at 12:41