#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    IPhone Flashlight application: Issue in memory release??


    Hi, I have written my first iPhone application FlashLight which turn on/off camera flash light. Following is the code for the same. It works fine except one issue. When I have flash on and it I press iPhone home button, it exit the application, turned off the flash light but when I start again it starts behaving weird. Sometimes I get black screen and at times it hangs up and respond very late. The other strange thing is if I wait few minutes and then restart the application works fine. I am sure there must be something to do memory release but I already been releasing session memory and also releasing captured AVSession in dealloc routine..! Not sure what could be wrong here. Could anyone please help me to find it out and let me know how could I debug the issue?

    Code:
    -(IBAction)submit:(id) sender {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
        if ([device hasTorch] && [device hasFlash]){
    
            if (device.torchMode == AVCaptureTorchModeOff) {
                AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
    
                AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
    
    
                AVCaptureSession *session = [[AVCaptureSession alloc] init];
    
                [session beginConfiguration];
    
                [device lockForConfiguration:nil];
    
                [device setTorchMode:AVCaptureTorchModeOn];
    
                [device setFlashMode:AVCaptureFlashModeOn];
    
                [session addInput:flashInput];
    
                [session addOutput:output];
    
                [device unlockForConfiguration];
    
                [output release];
    
                [session commitConfiguration];
    
                [session startRunning];
    
                [self setAVSession:session];
                [session release];
            }
            else {
                [AVSession stopRunning];
           }
       }
    }
    Thanks.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0
    It sounds like your actually having a problem with background processes, not dealloc. I beleive your only going to get there at app close. Which does happen when waiting for a few minutes, as iOS will close the app and your cold starting again.

    Use the Find leaks tool in xcode (I assume developing on Apples hardware) to eliminate the memory leaks possability.

    And review your backgorund processes/code that is running in the background to find how it handles awaking from being in the back.

    Jason
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0
    Originally Posted by jaxjason
    It sounds like your actually having a problem with background processes, not dealloc. I beleive your only going to get there at app close. Which does happen when waiting for a few minutes, as iOS will close the app and your cold starting again.

    Use the Find leaks tool in xcode (I assume developing on Apples hardware) to eliminate the memory leaks possability.

    And review your backgorund processes/code that is running in the background to find how it handles awaking from being in the back.

    Jason
    I think you are correct. I didn't know about background process and doesn't have any code included for that. I just read about it and I think little bit rework will resolve my issue.

    Thanks.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0
    Look for either "applicationDidEnterBackground:" or "applicationWillEnterForeground:" and write what you need to to check for system events that might have happened (use of the camera possibly in your case) and what to do upon awakening.

    also, if you do not require background operation follow apples suggestion... "If you do not want your application to remain in the background when it is quit, you can explicitly opt out of the background execution model by adding the UIApplicationExitsOnSuspend key to your applicationís Info.plist file and setting its value to YES."

    This is the simplest solution of course, adn what I will most likely do with what i am working on. I will save state variables to preference file and re-read them back in for resumption of my app.

    Jason
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0
    Originally Posted by jaxjason
    Look for either "applicationDidEnterBackground:" or "applicationWillEnterForeground:" and write what you need to to check for system events that might have happened (use of the camera possibly in your case) and what to do upon awakening.

    also, if you do not require background operation follow apples suggestion... "If you do not want your application to remain in the background when it is quit, you can explicitly opt out of the background execution model by adding the UIApplicationExitsOnSuspend key to your applicationís Info.plist file and setting its value to YES."

    This is the simplest solution of course, adn what I will most likely do with what i am working on. I will save state variables to preference file and re-read them back in for resumption of my app.

    Jason
    Hmm...that was useful. I think setting flag in plist file will do in my case.

    BTW, what do you mean by saving variables in file? We can just save variables using setVarName like I am doing in my app. Isn't that sufficient? I think those variables will be still persisted after resuming the application. No??

    Thanks.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0
    I can't find anything for setvarname in the dev site. I found prevoously about iOS v2, and have been doing since, that for a variable to be persistant when you close and reopen the app without multitasking, and if you only have a few variables, you write them to the prefs file with a nsstring key, then read those in upon initializing the app on starting it up again.

    Can you send me the link that documents setvarname please, I would love it if it did this for me.

    Jason
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0
    I mean see below code:

    I have following variable defined in controller header file:

    AVCaptureSession *AVSession

    Code in controller.m (given in main question)

    [self setAVSession:session]; ==> I don't know in details but this is how it set my AVSession variable. Also not sure whether it will be persisted when app moved to background.

    Regards.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0
    I beleive your just setting an Instance Variable that is good as long as the app is in memory and not Dealloc'd. It should be persistant through background procvessing and even memory freeze. But if it is forced to close, it will be lost.

    Mine I wrote way, way before they were even talking about multitasking, but wanted to give the impression that my Tip and Sale calculator was, and since it took no time to load, I just remembered all their numbers to a preference file on exiting, then read them back in upon app loading again. Giving the impression it was still running, or at least remembering their last settings.

    If you have a few settings, switches, etc... that you would like to reset again when the app is run again, thats how it is done as far as I could find. Maybe a "turn on flash on app start", so that if you move it to yes, just starting the app will turn on the flashlight function, no need to press a button for "on" on the screen?

    Jason
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0
    Originally Posted by jaxjason
    I beleive your just setting an Instance Variable that is good as long as the app is in memory and not Dealloc'd. It should be persistant through background procvessing and even memory freeze. But if it is forced to close, it will be lost.

    Mine I wrote way, way before they were even talking about multitasking, but wanted to give the impression that my Tip and Sale calculator was, and since it took no time to load, I just remembered all their numbers to a preference file on exiting, then read them back in upon app loading again. Giving the impression it was still running, or at least remembering their last settings.

    If you have a few settings, switches, etc... that you would like to reset again when the app is run again, thats how it is done as far as I could find. Maybe a "turn on flash on app start", so that if you move it to yes, just starting the app will turn on the flashlight function, no need to press a button for "on" on the screen?

    Jason
    Exactly...your points are completely valid and I understood. Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo