Resolved Move to ARC, won't keep windows.

Discussion in 'Mac Programming' started by Madd the Sane, Mar 26, 2012.

  1. Madd the Sane, Mar 26, 2012
    Last edited: Mar 26, 2012

    macrumors 6502

    Madd the Sane

    Joined:
    Nov 8, 2010
    Location:
    Utah
    #1
    When I moved to ARC, the window pops up for a short time then disappears. The code to make the window is coded thus:
    Code:
    [color=#aa0d91]@implementation[/color] Document
    
    + ([color=#aa0d91]void[/color])openFile:([color=#5c2699]NSString[/color] *)apath
    {
        [[[color=#aa0d91]self[/color] [color=#2e0d6e]alloc[/color]] [color=#26474b]initWithFile[/color]: apath];
    }
    
    
    - ([color=#aa0d91]IBAction[/color])reload:([color=#aa0d91]id[/color])sender
    {
        {
            [color=#3f6e74]svg_t[/color] *svg;
            [color=#5c2699]clock_t[/color] t;
            [color=#3f6e74]SVGRenderContext[/color] *svg_render_context = [[[color=#3f6e74]SVGRenderContext[/color] [color=#2e0d6e]alloc[/color]] [color=#2e0d6e]init[/color]];
    
            [color=#26474b]svg_create[/color](&svg);
    [color=#007400]//        printf("parsing...\n");[/color]
            [color=#26474b]svg_parse[/color](svg, [[color=#3f6e74]path[/color] [color=#2e0d6e]fileSystemRepresentation[/color]]);
    
    [color=#007400]//        printf("rendering...\n");[/color]
            t=[color=#2e0d6e]clock[/color]();
            [svg_render_context [color=#26474b]prepareRender[/color]: [color=#3f6e74]scale[/color]];
            [color=#26474b]svg_render[/color](svg, &[color=#3f6e74]cocoa_svg_engine[/color], ([color=#aa0d91]__bridge[/color] [color=#aa0d91]void[/color]*)svg_render_context);
            [svg_render_context [color=#26474b]finishRender[/color]];
            t=[color=#2e0d6e]clock[/color]()-t;
    [color=#007400]//        printf("done: %15.8f seconds\n",t/(double)CLOCKS_PER_SEC);[/color]
    
            [color=#5c2699]NSSize[/color] contextSize = [svg_render_context [color=#26474b]size[/color]];
            [[color=#3f6e74]svg_view[/color] [color=#2e0d6e]setFrame[/color]: [color=#2e0d6e]NSMakeRect[/color]([color=#1c00cf]0[/color], [color=#1c00cf]0[/color], contextSize.[color=#5c2699]width[/color], contextSize.[color=#5c2699]height[/color])];
            [[color=#3f6e74]svg_view[/color] [color=#26474b]setSVGRenderContext[/color]: svg_render_context];
    
            [color=#26474b]svg_destroy[/color](svg);
        }
    }
    
    
    - ([color=#aa0d91]id[/color])initWithFile:([color=#5c2699]NSString[/color] *)apath
    {
        [color=#aa0d91]if[/color] (!([color=#aa0d91]self[/color]=[[color=#aa0d91]super[/color] [color=#2e0d6e]initWithWindowNibName[/color]: [color=#c41a16]@"Document"[/color]])) [color=#aa0d91]return[/color] [color=#aa0d91]nil[/color];
        [color=#3f6e74]path[/color] = [apath [color=#2e0d6e]copy[/color]];
        [[[color=#aa0d91]self[/color] [color=#2e0d6e]window[/color]] [color=#2e0d6e]setTitleWithRepresentedFilename[/color]: [color=#3f6e74]path[/color]];
    
        [color=#3f6e74]scale[/color] = [color=#1c00cf]1.0[/color];
        [[color=#aa0d91]self[/color] [color=#26474b]reload[/color]:[color=#aa0d91]nil[/color]];
        [[color=#aa0d91]self[/color] [color=#2e0d6e]showWindow[/color]:[color=#aa0d91]nil[/color]];
    
        [color=#aa0d91]return[/color] [color=#aa0d91]self[/color];
    }
    
    
    - ([color=#aa0d91]void[/color])windowWillClose:([color=#5c2699]NSNotification[/color] *)n
    {
        [color=#007400]//[self autorelease];[/color]
    }
    
    As you can see, the + (void)openFile: (NSString *)apath gets called, but the Document isn't stored anywhere, so ARC automatically releases it. Is there a way to set it so that the window and/or Document class isn't autoreleased?
     
  2. thread starter macrumors 6502

    Madd the Sane

    Joined:
    Nov 8, 2010
    Location:
    Utah
    #2
    Nevermind: I moved from an NSWindowController subclass to NSDocument. That fixed it.
     

Share This Page