Changeset 8500


Ignore:
Timestamp:
Feb 16, 2008, 1:37:02 AM (12 years ago)
Author:
mikel
Message:

added more on the nib-loading function

Location:
trunk/source/examples/cocoa/nib-loading
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/examples/cocoa/nib-loading/HOWTO.html

    r8499 r8500  
    282282        needed.</p>
    283283
     284      <p>The nib-loading function can simply parameterize the file to
     285      be loaded, and then perform the sequence of steps covered in the
     286      previous section. If we just literally do that, the result will
     287      look something like this:</p>
     288
     289      <pre>
     290(defun load-nibfile (nib-path)
     291  (let* ((app-class-name (%make-nsstring "NSApplication"))
     292         (app-class (#_NSClassFromString class-name))
     293         (app (#/sharedApplication appclass))
     294         (app-zone (#/zone app))
     295         (nib-name (%make-nsstring (namestring nib-path)))
     296         (dict (#/dictionaryWithObject:forKey:
     297                (@class ns-mutable-dictionary) app #@"NSNibOwner")))
     298    (#/loadNibFile:externalNameTable:withZone: (@class ns-bundle)
     299                                               nib-name
     300                                               dict
     301                                               app-zone)))
     302      </pre>
     303
     304      <p>The trouble with this function is that it leaks two strings
     305      and a dictionary every time we call it. We need to release the
     306      variables <code>app-class-name</code>, <code>nib-name</code>,
     307      and <code>dict</code> before returning. So how about this
     308      version instead?</p>
     309
     310      <pre>
     311(defun load-nibfile (nib-path)
     312  (let* ((app-class-name (%make-nsstring "NSApplication"))
     313         (app-class (#_NSClassFromString class-name))
     314         (app (#/sharedApplication appclass))
     315         (app-zone (#/zone app))
     316         (nib-name (%make-nsstring (namestring nib-path)))
     317         (dict (#/dictionaryWithObject:forKey:
     318                (@class ns-mutable-dictionary) app #@"NSNibOwner"))
     319         (result (#/loadNibFile:externalNameTable:withZone: (@class ns-bundle)
     320                                                            nib-name
     321                                                            dict
     322                                                            app-zone)))
     323    (#/release app-class-name)
     324    (#/release nib-name)
     325    (#/release dict)
     326    result))
     327      </pre>
     328
     329      <p>This version solves the leaking problem by binding the result
     330      of the load call to <code>result</code>, then releasing the
     331      variables in question before returning the result of the
     332      load.</p>
     333
     334      <p>There's just one more problem: what if we want to use the
     335      dictionary to collect the nibfile's toplevel objects, so that we
     336      can get access to them from our code? We'll need another version
     337      of our function.</p>
    284338
    285339      <div class="section-head">
     
    293347      the <code>"NSNibTopLevelObjects"</code> key with the dictionary
    294348      object that you pass
    295       to <code>loadNibFile:externalNameTable:withZone:</code>7mdash;to
     349      to <code>loadNibFile:externalNameTable:withZone:</code>&mdash;to
    296350      obtain a collection of toplevel objects that you release when
    297351      the nibfile is no longer needed.</p>
     
    299353      <p>In document-based Cocoa applications, the main nibfile is
    300354      usually owned by the application object, and is never unloaded
    301       while the appliation runs. Auxliliary nibfiles are normally
     355      while the application runs. Auxliliary nibfiles are normally
    302356      owned by controller objects, usually instances of
    303357      <code>NSWindowController</code> subclasses. When you
    304358      use <code>NSWindowController</code> objects to load nibfiles,
    305       they take responsbility for loading and unloading nibfile
     359      they take responsibility for loading and unloading nibfile
    306360      objects.</p>
    307361
     
    312366      management yourself. On the one hand, loading nibfiles by hand
    313367      is not likely to be the source of major application problems. On
    314       the other hand, if you experiement with nib-loading for a long
     368      the other hand, if you experiment with nib-loading for a long
    315369      time in an interactive session, it's possible that you'll end up
    316370      with numerous discarded objects cluttering memory, along with
  • trunk/source/examples/cocoa/nib-loading/nib-loading.lisp

    r8479 r8500  
    1313
    1414(defun load-nibfile (nib-path)
    15   (let* ((appclass (#_NSClassFromString (%make-nsstring "NSApplication")))
     15  (let* ((app-class-name (%make-nsstring "NSApplication"))
     16         (app-class (#_NSClassFromString class-name))
    1617         (app (#/sharedApplication appclass))
    1718         (app-zone (#/zone app))
    1819         (nib-name (%make-nsstring (namestring nib-path)))
    19          (toplevel-objects-array (#/arrayWithCapacity: (@class ns-mutable-array) 8))
    20          (context (#/dictionaryWithObjectsAndKeys: (@class ns-mutable-dictionary)
    21                                                    app #@"NSNibOwner"
    22                                                    toplevel-objects-array #@"NSNibTopLevelObjects"))
    23          (load-succeeded-p (#/loadNibFile:externalNameTable:withZone: (@class ns-bundle)
    24                                                                       nib-name context app-zone)))
    25     (values load-succeeded-p context)))
    26 
    27 (setf  *my-app*
    28        (let* ((class-name (%make-nsstring "NSApplication"))
    29               (appclass (#_NSClassFromString class-name)))
    30          (#/release class-name)
    31          (#/sharedApplication appclass)))
    32 
    33 
     20         (dict (#/dictionaryWithObject:forKey:
     21                (@class ns-mutable-dictionary) app #@"NSNibOwner"))
     22         (result (#/loadNibFile:externalNameTable:withZone: (@class ns-bundle)
     23                                                            nib-name
     24                                                            dict
     25                                                            app-zone)))
     26    (#/release app-class-name)
     27    (#/release nib-name)
     28    (#/release dict)
     29    result))
    3430#|
    3531(ccl::load-nibfile "/usr/local/openmcl/trunk/source/examples/cocoa/nib-loading/hello.nib")
Note: See TracChangeset for help on using the changeset viewer.