开发者

Crashing the OS X Pasteboard

开发者 https://www.devze.com 2022-12-29 11:07 出处:网络
I have an application that reads in text by emulating CMD-C copy commands and reading the pasteboard - unfortunately this the only way to achieve what I need.Occasionally, as this is under development

I have an application that reads in text by emulating CMD-C copy commands and reading the pasteboard - unfortunately this the only way to achieve what I need. Occasionally, as this is under development, once in a while I do something wrong (I don't think it's related to the copy command) and the app crashes. Once in a while, this has a knock on effect on the system-wide pasteboard - any other application that is running will crash if I attempt a copy, cut, or paste.

Is there a robust way to handle this - something I should be doing with the NSPasteboard before exiting? Any information on开发者_运维百科 what might be happening is appreciated.

For completeness, here are the only snippets of code that access the pasteboard:

Reading from the pasteboard:

NSString *pBoardText = [[NSPasteboard generalPasteboard]stringForType:NSStringPboardType];

Initially clearing the pasteboard (I run this only once, at launch):

[[NSPasteboard generalPasteboard] declareTypes: [NSArray arrayWithObject:NSStringPboardType] owner: self];
    [[NSPasteboard generalPasteboard] setString: @"" forType: NSStringPboardType];

PS I forgot to mention that this copy command runs on a loop, in a different thread - could be important. Although I've been careful not to access the pasteboard on the main thread without checking first that the loop is stopped.

Update - a few questions about what I am doing...

  • Can you post a crash report

Working on it right now - unfortunately the crashes are irregular. Let me be clear though - this is an application I am still developing, and sometimes I introduce a bug. When this results in a crash, the system-wide pasteboard gets messed up SOMETIMES. It doesn't look like the pasteboard access in my app is CAUSING the crash though, instead that it exits while the background loop is at a delicate stage of interacting with the PB. Update - Re the crash report - how important is this to you guys? I'm still developing, but can try to run it a few times not in the debugger until something breaks. Unfortunately I fixed all outstanding bugs for now and am not getting any crashes. This strongly suggests to me that the problem is not with the PBoard code itself - I'm more looking for some safeguards, so that if there IS a crash, it doesn't bring down my whole system. All these restarts are getting annoying.

  • Can you elaborate on why you need to emulate Cmd-C to do what you need?

I am scraping text from a chat box on an external application. The chat box is built to prohibit me from using the Accessibility interface or any other means.

  • Why are you clearing the contents of the clipboard on launch?

I examine the pasteboard text for new text. This was is a quick way to make sure that I don't process text copied from some other application.

  • Why are you running the code on a thread at all?

The loop continually posts events to simulate user input, including switching to the chat window, and copying the selected text. If this is done on the main thread, my app UI will just hang. I use the UI to display an overview of what's going on.

  • please show the code that runs on the main thread and checks the loop and accesses the pasteboard

the background thread passes data to the main thread using NSNotifications:

[self performSelectorOnMainThread:@selector(postNote:) withObject:d waitUntilDone:NO];


Some thoughts:

  • It is highly unlikely that any threading issues within one app will bring down the other applications. As this appears to be your main problem, it seems more likely that the problem is with the data you put on the pasteboard or the description of the data. The apps crash for some reason when they try to use the data on the pasteboard.
  • The code you're using is only for 10.5 or earlier. There were several major changes in the way the pasteboard is used in 10.6. If you're running under 10.6, using the 10.5 methods might might be your problem.
  • One major change in 10.6 is the reliance on UTIs to accurately describe the data on the pasteboard. If the UTI is incorrect or garbled, any app trying to make use of the data based on the assumption it is some other from of data could crash.
  • If you're using 10.6 make sure to use the Pasteboard Programming Guide as your reference and not the older Pasteboard Programming Topics for Cocoa.
0

精彩评论

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