From 4a8df7a33c298d22bf78b947d0e861fc03ec70e1 Mon Sep 17 00:00:00 2001
From: Michael Natterer <mitch@lanedo.com>
Date: Fri, 07 Dec 2012 11:19:52 +0000
Subject: quartz: fix crash in the recent clipboard "fix", and really fix it
We must not release the GtkClipboardOwner in pasteboardChangedOwner
becaue we don't own a reference to ourselves (NSPasteboard does).
Instead, release the owner right after setting it, transferring
ownership to NSPasteboard
Also, fix repeated setting of the same owner by keeping the
owner around in GtkCLipboard, and re-use it if "user_data"
doesn't change. To avoid clipboard_unset()ting our own contents
in the process, add an ugly "setting_same_owner" boolean to
GtkClipboardOwner, set it during re-setting the same owner,
and avoid calling clipboard_unset() from pasteboardChangedOwner
if it's TRUE.
---
(limited to 'gtk/gtkclipboard-quartz.c')
diff --git gtk/gtkclipboard-quartz.c gtk/gtkclipboard-quartz.c
index 743f037..14b0974 100644
|
|
enum { |
39 | 39 | LAST_SIGNAL |
40 | 40 | }; |
41 | 41 | |
| 42 | @interface GtkClipboardOwner : NSObject { |
| 43 | GtkClipboard *clipboard; |
| 44 | @public |
| 45 | gboolean setting_same_owner; |
| 46 | } |
| 47 | |
| 48 | @end |
| 49 | |
42 | 50 | typedef struct _GtkClipboardClass GtkClipboardClass; |
43 | 51 | |
44 | 52 | struct _GtkClipboard |
… |
… |
struct _GtkClipboard |
46 | 54 | GObject parent_instance; |
47 | 55 | |
48 | 56 | NSPasteboard *pasteboard; |
| 57 | GtkClipboardOwner *owner; |
49 | 58 | NSInteger change_count; |
50 | 59 | |
51 | 60 | GdkAtom selection; |
… |
… |
static GtkClipboard *clipboard_peek (GdkDisplay *display, |
88 | 96 | GdkAtom selection, |
89 | 97 | gboolean only_if_exists); |
90 | 98 | |
91 | | @interface GtkClipboardOwner : NSObject { |
92 | | GtkClipboard *clipboard; |
93 | | } |
94 | | |
95 | | @end |
96 | | |
97 | 99 | @implementation GtkClipboardOwner |
98 | 100 | -(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type |
99 | 101 | { |
… |
… |
static GtkClipboard *clipboard_peek (GdkDisplay *display, |
132 | 135 | */ |
133 | 136 | - (void)pasteboardChangedOwner:(NSPasteboard *)sender |
134 | 137 | { |
135 | | clipboard_unset (clipboard); |
136 | | |
137 | | [self release]; |
| 138 | if (! setting_same_owner) |
| 139 | clipboard_unset (clipboard); |
138 | 140 | } |
139 | 141 | |
140 | 142 | - (id)initWithClipboard:(GtkClipboard *)aClipboard |
… |
… |
static GtkClipboard *clipboard_peek (GdkDisplay *display, |
144 | 146 | if (self) |
145 | 147 | { |
146 | 148 | clipboard = aClipboard; |
| 149 | setting_same_owner = FALSE; |
147 | 150 | } |
148 | 151 | |
149 | 152 | return self; |
… |
… |
gtk_clipboard_set_contents (GtkClipboard *clipboard, |
334 | 337 | NSSet *types; |
335 | 338 | NSAutoreleasePool *pool; |
336 | 339 | |
337 | | pool = [[NSAutoreleasePool alloc] init]; |
338 | | |
339 | | owner = [[GtkClipboardOwner alloc] initWithClipboard:clipboard]; |
340 | | |
341 | 340 | if (!(clipboard->have_owner && have_owner) || |
342 | 341 | clipboard->user_data != user_data) |
343 | 342 | { |
… |
… |
gtk_clipboard_set_contents (GtkClipboard *clipboard, |
352 | 351 | clipboard->user_data != user_data) |
353 | 352 | { |
354 | 353 | (*clear_func) (clipboard, user_data); |
355 | | [pool release]; |
356 | 354 | return FALSE; |
357 | 355 | } |
358 | 356 | else |
359 | 357 | { |
360 | | [pool release]; |
361 | 358 | return TRUE; |
362 | 359 | } |
363 | 360 | } |
364 | 361 | } |
365 | 362 | |
| 363 | pool = [[NSAutoreleasePool alloc] init]; |
| 364 | |
| 365 | types = _gtk_quartz_target_entries_to_pasteboard_types (targets, n_targets); |
| 366 | |
366 | 367 | /* call declareTypes before setting the clipboard members because |
367 | 368 | * declareTypes might clear the clipboard |
368 | 369 | */ |
369 | | types = _gtk_quartz_target_entries_to_pasteboard_types (targets, n_targets); |
370 | | clipboard->change_count = [clipboard->pasteboard declareTypes: [types allObjects] |
371 | | owner: owner]; |
| 370 | if (user_data && user_data == clipboard->user_data) |
| 371 | { |
| 372 | owner = [clipboard->owner retain]; |
| 373 | |
| 374 | owner->setting_same_owner = TRUE; |
| 375 | clipboard->change_count = [clipboard->pasteboard declareTypes: [types allObjects] |
| 376 | owner: owner]; |
| 377 | owner->setting_same_owner = FALSE; |
| 378 | } |
| 379 | else |
| 380 | { |
| 381 | owner = [[GtkClipboardOwner alloc] initWithClipboard:clipboard]; |
| 382 | |
| 383 | clipboard->change_count = [clipboard->pasteboard declareTypes: [types allObjects] |
| 384 | owner: owner]; |
| 385 | } |
| 386 | |
| 387 | [owner release]; |
372 | 388 | [types release]; |
373 | 389 | [pool release]; |
374 | 390 | |
| 391 | clipboard->owner = owner; |
375 | 392 | clipboard->user_data = user_data; |
376 | 393 | clipboard->have_owner = have_owner; |
377 | 394 | if (have_owner) |
… |
… |
clipboard_unset (GtkClipboard *clipboard) |
460 | 477 | clipboard->n_storable_targets = -1; |
461 | 478 | g_free (clipboard->storable_targets); |
462 | 479 | clipboard->storable_targets = NULL; |
463 | | |
| 480 | |
| 481 | clipboard->owner = NULL; |
464 | 482 | clipboard->get_func = NULL; |
465 | 483 | clipboard->clear_func = NULL; |
466 | 484 | clipboard->user_data = NULL; |