Bug 127975 (Part2) - Exception thrown when disposing caret

Change-Id: If99ad981ca50290493551db9de604aa6dc1d02cb
Signed-off-by: Niraj Modi <niraj.modi@in.ibm.com>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Caret.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Caret.java
index 69011e8..4ce9537 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Caret.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Caret.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2011 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -286,7 +286,10 @@
 @Override
 void releaseParent () {
 	super.releaseParent ();
-	if (this == parent.getCaret ()) parent.setCaret (null);
+	if (parent != null && this == parent.caret) {
+		if (!parent.isDisposed()) parent.setCaret (null);
+		else parent.caret = null;
+	}
 }
 
 @Override
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Caret.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Caret.java
index b71f625..53773e7 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Caret.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Caret.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2016 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -324,7 +324,10 @@
 @Override
 void releaseParent () {
 	super.releaseParent ();
-	if (this == parent.getCaret ()) parent.setCaret (null);
+	if (parent != null && this == parent.caret) {
+		if (!parent.isDisposed()) parent.setCaret (null);
+		else parent.caret = null;
+	}
 }
 
 @Override