Professional Web Applications Themes

[Q] Why unsolicited dealloc in NSKeyedUnarchiver? - Mac Programming

In trying to add Open Recent support to MyDoent app everything is working fine with the exception of an unsolicited call to dealloc by NSKeyedUnarchiver. Any ideas where this is coming from and moreover why? I'm confused. TIA for your help... Peter From MyDoent - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType { passwordRecords = [NSKeyedUnarchiver unarchiveObjectWithData:data]; return YES; } From PasswordsDataSource designated initializer -(id)initWithCoder:(NSCoder *)coder { self = [super init]; passwordRecs = [[NSMutableArray alloc] init]; [self addPasswordRecord]; if ([coder allowsKeyedCoding]) { passwordRecs = [coder decodeObjectForKey:"passwordRecs"]; } return self; } From PasswordRecord designated initializer -(id)initWithCoder:(NSCoder *)coder { if (self = [super init]) { ...

  1. #1

    Default [Q] Why unsolicited dealloc in NSKeyedUnarchiver?

    In trying to add Open Recent support to MyDoent app everything is
    working fine with the exception of an unsolicited call to dealloc by
    NSKeyedUnarchiver. Any ideas where this is coming from and moreover why?
    I'm confused. TIA for your help... Peter

    From MyDoent
    - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType {
    passwordRecords = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return YES;
    }
    From PasswordsDataSource designated initializer
    -(id)initWithCoder:(NSCoder *)coder {
    self = [super init];
    passwordRecs = [[NSMutableArray alloc] init];
    [self addPasswordRecord];
    if ([coder allowsKeyedCoding]) {
    passwordRecs = [coder decodeObjectForKey:"passwordRecs"];
    }
    return self;
    }
    From PasswordRecord designated initializer
    -(id)initWithCoder:(NSCoder *)coder {
    if (self = [super init]) {
    [self setClientID:"Replace me"];
    [self setCreatedTS:[NSCalendarDate calendarDate]]; // Now
    [self setMACAddress:"01-23-45-67-89-AB"];
    [self setPassword:"replace it"];
    }
    if ([coder allowsKeyedCoding]) {
    [self setClientID:[coder decodeObjectForKey:"clientID"]];
    [self setCreatedTS:[coder decodeObjectForKey:"createdTS"]];
    [self setMACAddress:[coder decodeObjectForKey:"macAddress"]];
    [self setPassword:[coder decodeObjectForKey:"password"]];
    }
    return self;
    }

    From debugger
    #0 0x020e8450 in -[PasswordRecord dealloc] at PasswordRecordsModel.m:88
    #1 0x90197fe8 in CFDictionaryRemoveAllValues
    #2 0x901a6088 in __CFDictionaryDeallocate
    #3 0x90190db0 in CFRelease
    #4 0x90a5545c in -[NSKeyedUnarchiver dealloc] <<<<<<<<< unsolicited call
    #5 0x90aacf3c in +[NSKeyedUnarchiver unarchiveObjectWithData:]
    #6 0x02009348 in -[MyDoent loadDataRepresentation:ofType:] at
    MyDoent.m:52
    #7 0x930522f8 in -[NSDoent loadFileWrapperRepresentation:ofType:]
    #8 0x9305261c in -[NSDoent readFromFile:ofType:]
    #9 0x92f69fb0 in -[NSDoent initWithContentsOfFile:ofType:]
    #10 0x92f69e24 in -[NSDoentController
    makeDoentWithContentsOfFile:ofType:]
    #11 0x92f69c64 in -[NSDoentController _openDoentFileAt:display:]
    #12 0x92fb3b4c in -[NSDoentController _openFile:]
    #13 0x92fa5740 in -[NSApplication _doOpenFile:ok:tryTemp:]
    #14 0x92fec4ec in -[NSApplication openFile:ok:]
    #15 0x93054188 in -[NSDoentController _openRecentDoent:]
    #16 0x92e77ac0 in -[NSApplication sendAction:to:from:]
    #17 0x92ead30c in -[NSMenu performActionForItemAtIndex:]
    #18 0x92ef1c2c in -[NSCarbonMenuImpl
    performActionWithHighlightingForItemAtIndex:]
    #19 0x92e2ee08 in _NSHandleCarbonMenuEvent
    #20 0x92dd2d84 in _DPSNextEvent
    #21 0x92de93b0 in -[NSApplication
    nextEventMatchingMask:untilDate:inMode:dequeue:]
    #22 0x92dfd718 in -[NSApplication run]
    #23 0x92eb9b80 in NSApplicationMain
    #24 0x011bff74 in main at main.m:13
    Peter Guest

  2. #2

    Default Re: [Q] Why unsolicited dealloc in NSKeyedUnarchiver?

    On Tue, 20 Apr 2004, Peter Teeson wrote:
     

    You forgot to retain the object here. Remember that if you don't alloc or
    copy an object, and you want it to stick around, you must retain it.
    Michael Guest

  3. #3

    Default Re: [Q] Why unsolicited dealloc in NSKeyedUnarchiver?

    In article <twistedsys.net>,
    Michael Ash <com> wrote:
     
    >
    > You forgot to retain the object here. Remember that if you don't alloc or
    > copy an object, and you want it to stick around, you must retain it.[/ref]

    Forgive my ignorance and thanks for trying to help me.
    But dosn't passwordRecs = [[NSMutableArray alloc] init]; retain it in
    the autorelease pool?

    Do you mean that I have to do something in addition?
    Specifically what did you have in mind please?...

    and thanks...

    respect....

    Peter
    Peter Guest

  4. #4

    Default Re: [Q] Why unsolicited dealloc in NSKeyedUnarchiver?

    On Tue, 20 Apr 2004, Peter Teeson wrote:
     
    > >
    > > You forgot to retain the object here. Remember that if you don't alloc or
    > > copy an object, and you want it to stick around, you must retain it.[/ref]
    >
    > Forgive my ignorance and thanks for trying to help me.
    > But dosn't passwordRecs = [[NSMutableArray alloc] init]; retain it in
    > the autorelease pool?
    >
    > Do you mean that I have to do something in addition?
    > Specifically what did you have in mind please?...[/ref]

    Remember that your variables are not objects, they are *pointers* to
    objects. Let's examine the following code:

    obj = [[NSArray alloc] init];
    // obj now points to a fresh NSArray instance
    obj = [NSArray array];
    // obj now points to an autoreleased NSArray. The original array is
    // *lost*, and it's been leaked because it wasn't released.
    // Since the new array is autoreleased, it will go away if you
    // don't retain it.

    You're doing the equivalent of what this code does. In order to stop the
    crash, retain obj when you assign it in the statement quoted above. In
    order to stop the leak, release the old array before you do the
    assignment.

    Michael Guest

  5. #5

    Default Re: [Q] Why unsolicited dealloc in NSKeyedUnarchiver?

    In article <twistedsys.net>,
    Michael Ash <com> wrote:
     
    > >
    > > Forgive my ignorance and thanks for trying to help me.
    > > But dosn't passwordRecs = [[NSMutableArray alloc] init]; retain it in
    > > the autorelease pool?
    > >
    > > Do you mean that I have to do something in addition?
    > > Specifically what did you have in mind please?...[/ref]
    >
    > Remember that your variables are not objects, they are *pointers* to
    > objects. Let's examine the following code:
    >
    > obj = [[NSArray alloc] init];
    > // obj now points to a fresh NSArray instance
    > obj = [NSArray array];
    > // obj now points to an autoreleased NSArray. The original array is
    > // *lost*, and it's been leaked because it wasn't released.
    > // Since the new array is autoreleased, it will go away if you
    > // don't retain it.
    >
    > You're doing the equivalent of what this code does. In order to stop the
    > crash, retain obj when you assign it in the statement quoted above. In
    > order to stop the leak, release the old array before you do the
    > assignment.
    >[/ref]

    Thank you very much for your helpful explanation and patience.
    I have changed the method as follows and it now works:

    -(id)initWithCoder:(NSCoder *)coder {
    self = [super init];
    passwordRecs = [[NSMutableArray alloc] init];
    [self addPasswordRecord];
    if ([coder allowsKeyedCoding]) {
    [passwordRecs release];
    passwordRecs = [coder decodeObjectForKey:"passwordRecs"];
    [passwordRecs retain];
    }
    return self;
    }
    Peter Guest

  6. #6

    Default Re: [Q] Why unsolicited dealloc in NSKeyedUnarchiver?

    Peter Teeson wrote: [/ref][/ref]

    Let me point out that nothing is ever "retained in the autorelease
    pool". Autorelease is nothing more than a delayed messaging facility.
    When you -autorelease an object, the current pool will take note, and
    send it a -release whenever the pool itself is released.

    -jcr
    John Guest

  7. #7

    Default Re: [Q] Why unsolicited dealloc in NSKeyedUnarchiver?

    Peter Teeson wrote: 
     

    At this point, you've just created a new dictionary.
     

    With this assignment, you've leaked the dictionary you created, and
    assigned "passwordRecs" to point to an autoreleased dictionary returned
    by -decodeObjectForKey:.
     

    What you probably want is:

    -(id)initWithCoder:(NSCoder *)coder {
    self = [super init];
    [self addPasswordRecord];
    if ([coder allowsKeyedCoding]) {
    passwordRecs = [[coder decodeObjectForKey:"passwordRecs"] retain];
    }
    return self;
    }

    Better still would be to have an accessor method for passwordRecs, such as:

    - setPasswordRecs:(id) newRecs
    {
    if (passwordRecs != newRecs)
    {
    [newRecs retain];
    [passwordRecs release];
    passwordRecs = newRecs;
    }
    }

    Which would make your -initWithCoder: method look like this:

    -(id)initWithCoder:(NSCoder *)coder {
    self = [super init];
    [self addPasswordRecord];
    if ([coder allowsKeyedCoding]) {
    [self setPasswordRecs:[coder decodeObjectForKey:"passwordRecs"]];
    }
    return self;
    }

    HTH,

    -jcr
    John Guest

Similar Threads

  1. [Cocoa] NSKeyedUnarchiver and "root" object
    By Manfred in forum Mac Programming
    Replies: 7
    Last Post: January 7th, 07:59 PM
  2. Unsolicited fax strategy
    By Corey in forum Mac Networking
    Replies: 18
    Last Post: December 19th, 03:59 AM
  3. OT: Unsolicited testimonial
    By Tony in forum SCO
    Replies: 5
    Last Post: September 14th, 02:40 AM
  4. Unsolicited Disconnects while on line
    By Rajiv in forum Windows Networking
    Replies: 0
    Last Post: July 18th, 12:52 AM
  5. unsolicited pop up windows!
    By Steen Jensen in forum Windows Setup, Administration & Security
    Replies: 1
    Last Post: July 10th, 09:53 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