Professional Web Applications Themes

NSArray error - Mac Programming

In a controller class, I have an instance variable NSMutableArray* TagVektorH. It is set in awakeFormNib with TagVektorH=[NSMutableArray arrayWithCapacity:0]; Later ist is filled with for (z=0;z<max;z++) { NSNumber* tempTag =[NSNumber numberWithInt:(10*z)]; [TagVektorH addObject:tempTag]; } Everything works fine, and I can see the array in the debugger with the right number of objects. Accesing the array just now works fine and it returns his objects correctly. But going to the array later within an action method of th controller class ends up in a BAD_ACCESS after a call like [TagVektorH count]; Looking at the array in this moment shows in the ...

  1. #1

    Default NSArray error

    In a controller class, I have an instance variable NSMutableArray*
    TagVektorH.
    It is set in awakeFormNib with
    TagVektorH=[NSMutableArray arrayWithCapacity:0];
    Later ist is filled with
    for (z=0;z<max;z++)
    {
    NSNumber* tempTag =[NSNumber numberWithInt:(10*z)];
    [TagVektorH addObject:tempTag];
    }
    Everything works fine, and I can see the array in the debugger with
    the right number of objects.
    Accesing the array just now works fine and it returns his objects
    correctly.
    But going to the array later within an action method of th controller
    class ends up in a BAD_ACCESS after a call like
    [TagVektorH count];
    Looking at the array in this moment shows in the summary column:
    {int)[$VAR count]} objects
    instead of the number of objects as before.
    What is going wrong?
    Thanks for help
    Ruedi Heimlicher
    Ruedi Guest

  2. #2

    Default Re: NSArray error

    In article <google.com>,
    ch (Ruedi Heimlicher) wrote:
     

    This creates an autoreleased instance of an NSNumatbleArray, which will
    receive a "release" message once your application returns to the event
    loop or the current autorelease pool is released some other way.
     

    You probably forgot to correctly retain your array. If you store an
    autoreleased object in a member variable (instance variable, whatever
    you like to call it) of your object, the moment it is autoreleased you
    will have a stale pointer in there. Retain the object when you put it in
    the member variable, and release it in your object's dealloc method.

    But you probably already knew that from reading the docs, and just
    forgot the call to retain.

    Cheers,
    -- Uli
    http://www.zathras.de
    Uli Guest

  3. #3

    Default Re: NSArray error

    Uli Kusterer <de> wrote in message news:<t-online.com>... 
    >
    > This creates an autoreleased instance of an NSNumatbleArray, which will
    > receive a "release" message once your application returns to the event
    > loop or the current autorelease pool is released some other way.

    >
    > You probably forgot to correctly retain your array. If you store an
    > autoreleased object in a member variable (instance variable, whatever
    > you like to call it) of your object, the moment it is autoreleased you
    > will have a stale pointer in there. Retain the object when you put it in
    > the member variable, and release it in your object's dealloc method.
    >
    > But you probably already knew that from reading the docs, and just
    > forgot the call to retain.
    >
    > Cheers,
    > -- Uli
    > http://www.zathras.de[/ref]
    Thank you very much, that solved the problem. I read the docs
    repeatly, but didn't understand the mechanism the right way, since I
    am new to cocoa (C++, Pascal)
    Ruedi
    Ruedi Guest

  4. #4

    Default Re: NSArray error

    In article <google.com>,
    ch (Ruedi Heimlicher) wrote:
     

    Ruedi,

    general rule of thumb: If the name of the method that returns an object
    contains a "retain", "init/alloc", copy or "load", then you get a
    retained object, and you are responsible for releasing it.

    Otherwise, the object is either autoreleased, or owned by whatever
    object you got it from. In that case, you need to retain it if you want
    to keep it around for a while, and of course if you retain it, you'll
    also have to release it later so you're not leaking the object.

    (NB - I hope I didn't leave out one of the words that can be in methods
    that return retained objects ... I recognize them, but don't really have
    the list memorized)

    Cheers,
    -- Uli
    http://www.zathras.de
    Uli Guest

  5. #5

    Default Re: NSArray error

    On 16/07/2004, Ruedi Heimlicher wrote in message
    <google.com>:
     

    Should you be using

    TagVektorH = [[NSMutableArray alloc] initWithCapacity:0];

    instead, and releasing the array when you're finished with it ?
    You're running into a problem I had when I started programming
    in Objective-C: I didn't understand when variables were released.
    A couple of people on this group kindly explained the problem
    to me.

    Of course, I may be entirely wrong about this.

    Simon.
    --
    Using pre-release version of newsreader.
    Please tell me if it does weird things.
    Simon Guest

Similar Threads

  1. Replies: 0
    Last Post: March 29th, 09:49 PM
  2. Replies: 0
    Last Post: November 10th, 05:24 PM
  3. bulk insert error: 'Operating system error code 5(Access is denied.)'
    By Trint in forum Microsoft SQL / MS SQL Server
    Replies: 4
    Last Post: August 20th, 04:21 PM
  4. Replies: 1
    Last Post: August 7th, 07:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139