开发者

MacRuby: EXC_BAD_ACCESS on file dialog

I have been using MacRuby and running through the book MacRuby:The Definitive Guide by Matt Aimonetti.

On the Movies CoreData app example, I've got this code:

def add_image(sender)
    movie = movies.selectedObjects.lastObject
    return unless movie
    image_panel = NSOpenPanel.openPanel
    image_panel.canChooseDirectories = false
    image_panel.canCreateDirectories = false
    image_panel.allowsMultipleSelection = false

    image_panel.beginSheetModalForWindow(sender.window, completionHandler: Proc.new{|result|
        return if (result == NSCancelButton)
        path = image_panel.filename
        # use a GUID to avoid conflicts
        guid = NSProcessInfo.processInfo.globallyUniqueString
        # set the destination path in the support folder
        dest_path = applicationFilesDirectory.URLByAppendingPathComponent(guid)
        dest_path = dest_path.relativePath
        error = Pointer.new(:id)
        NSFileManager.defaultManager.copyItemAtPath(path, toPath:dest_path, error:error)
        NSApplication.sharedApplication.presentError(error[0]) if error[0]
        movie.setValue(dest_path, forKey:"imagePath")
    })
end

The app loads fine and runs without issue - I can create new movies in CoreData and delete them, etc. However, when I click the button which calls this function it opens the dialog window fine, but either the "cancel" or "open file" buttons cause a crash here:

#import <Cocoa/Cocoa.h>

#import <MacRuby/MacRuby.h>

int main(int argc, char *argv[])
{
    return macruby_main("rb_main.rb", argc, argv); &开发者_JS百科lt;< Thread 1: Program received signal EXC_BAD_ACCESS
}

Any help is appreciated. I thought it had something to do with BridgeSupport but either embedding isn't working or my attempts to do so aren't working. Either way, something else seems borked as the example provided with the book also crashes.

Thanks!

ADDED NOTE:

I went and tested out this code from macruby.org and it worked fine:

def browse(sender)
  # Create the File Open Dialog class.
  dialog = NSOpenPanel.openPanel
  # Disable the selection of files in the dialog.
  dialog.canChooseFiles = false
  # Enable the selection of directories in the dialog.
  dialog.canChooseDirectories = true
  # Disable the selection of multiple items in the dialog.
  dialog.allowsMultipleSelection = false

  # Display the dialog and process the selected folder
  if dialog.runModalForDirectory(nil, file:nil) == NSOKButton
  # if we had a allowed for the selection of multiple items
  # we would have want to loop through the selection
    destination_path.stringValue = dialog.filenames.first
  end
end

Seems either something is borked in the beginSheetModalForWindow call or I'm missing something, trying to track down what. I can make my modal dialog for file selection work with the above code, but it isn't a sheet attached to the window.


NSOpenPanel is a subclass of NSSavePanel so you can use NSSavePanel's beginSheetModalForWindow:completionHandler:. It is runModalForDirectory that is depreciated. However, "depreciated" doesn't mean, "doesn't work" it means "will stop working in the future."

The error you are getting is pointing to the C code that loads MacRuby itself. This indicates a severe crash that MacRuby could not trap. It just shows in Main.m because that is the only place the debugger manages to trap the stack in which the error occurred. Unfortunately, locating the error at the very top/bottom of the stack like this makes it useless for debugging.

I don't see any obvious problem with Matt Aimonetti's code so I'm going to guess it's a problem with MacRuby handling the block that is passed for the completion handler. That would also explain why the error is not trapped because the block will be in a different address space than the object that defines it.

I would suggest contacting Matt Aimonetti directly via either the book site or the MacRuby mailing list (he's very active there.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜