Wierd Issue with NSMutableArray and NSInteger

Discussion in 'iOS Programming' started by AndyCodez, Dec 15, 2009.

  1. AndyCodez macrumors regular

    Joined:
    Aug 6, 2009
    #1
    The issue I'm having is, that the iphone simulator runs the code fine and functions how I designed it. But when I run on the actual device its reading trash values that were stored into the array. I stepped through the code and saw that the proper values were being retrieved from the database. But when i set the nsmutablearray to the contents of the array returned from the sql method its storing crap data (or at least I think so).

    I have a project where I'm reading int values from a database into a NSInteger to be placed in a NSMutableArray. This method is be called from one of my viewcontrollers to fill a NSMutableArray. This array is being used to retrieve the NSInteger into a int.

    Within the SQL Method I'm returning an autoreleased array. I don't understand how this code works fine in the simulator but not on the actual device.

    Any pointers? If you wish to help and need code I can post it later this evening. Otherwise I could just use a pointer in the right direction as where to figure out what the issue is.
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    How do you store an NSInteger in a NSMutableArray?
     
  3. AndyCodez thread starter macrumors regular

    Joined:
    Aug 6, 2009
    #3
    I think I found my issue, as I was digging around more I found this by robbie "NSIntegers are not objects. If you declare them as properties and synthesize the accessors you should declare the property as assign not retain, just like you would with an int property."

    I think that was my issue, I'll have to check tonight. I was trying to set NSInteger *var instead of NSInteger var. I thought NSInteger was a object like NSNumber, but I was wrong.
     
  4. AndyCodez thread starter macrumors regular

    Joined:
    Aug 6, 2009
    #4
    Actually I was wrong i was using a NSNumber not NSInteger.
     
  5. AndyCodez thread starter macrumors regular

    Joined:
    Aug 6, 2009
    #5
    Yeah, I'm an idiot. The issue ended up being that I was releasing a nsinteger when I was retrieving the integer from the database when i should not have been releasing it. I feel like an idiot, I look at it for 10 minutes now and figure it out! lol.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    OK, NSNumber is the right way to store numbers in a mutable array.

    Most likely this is a memory management error. Something isn't being retained that needs to be retained.

    Have you run Clang on this code?

    Otherwise you need to show some of the code or just step through it yourself to see when it's good and when it goes bad.

    BTW, if you're not using FMDB to manage your SQL code I recommend that you do so.
     
  7. AndyCodez thread starter macrumors regular

    Joined:
    Aug 6, 2009
    #7
    Wow I just read what I wrote, and I wrote it wrong. I was in a rush to get to the gym lol. I was using NSNumber the whole time. During the loop of getting values from my query it was creating a NSNumber then releasing it when it was finished (which I shouldn't have been doing based on how I initialized it.)

    What I find weird is the fact that it would work on the simulator but not the iphone. I didn't put 2 + 2 together, I was stepping through the code and it would crash when i tried to [nsnumvar integervalue]; but then I didn't think about it getting trash data because I was storing trash at the time.

    I have not run clang before I'll have to search up more information about it.
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    OK, I think you solved your problem but I guess I'm not certain. Without you showing the code I can only guess.

    There are various differences between the Sim and the device. On the Sim there is a lot more memory available so memory might not be re-used that on the device gets re-used more quickly. It's not worth thinking about very much. Write correct code and it'll work on both.

    On Snow Leopard Clang is integrated into Xcode and called Compile and Analyze, or something like that. It's part of my development process. After I implement any new features I run the leaks tool and I run Clang on my source. Neither tool is perfect but they are both very cheap in terms of time and effort to run and they have both found errors in my code that I didn't know about. I always use them both on new features.
     

Share This Page