Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 434393

Summary: [Cocoa] OS X: NPE in Control.internal_new_GC
Product: [Eclipse Project] Platform Reporter: Thomas Singer <eclipse>
Component: SWTAssignee: Lakshmi P Shanmugam <lshanmug>
Status: RESOLVED FIXED QA Contact:
Severity: critical    
Priority: P3 CC: abhishek.kishore, adripol1994, alexander.stahlberg, andreas.buchen, arunkumar.thondapu, boingolover, dave.nice, error-reports-inbox, eyck, fura, gattacus, hubert+eclipseorg, kate.labeeva, lshanmug, mail, marcel.bruch, nick, niraj.modi, picard, pujos.michael, superstippi, tom.schindl
Version: 4.4   
Target Milestone: 4.5.2   
Hardware: PC   
OS: Mac OS X   
Whiteboard:
Bug Depends on:    
Bug Blocks: 482454    
Attachments:
Description Flags
Sample to reproduce
none
patch none

Description Thomas Singer CLA 2014-05-08 04:55:45 EDT
We've got following exception sent by a user of our SWT-based application (SWT version 4.333, Mac OS X 10.9.2, java 1.7.0_45):

java.lang.NullPointerException
	at org.eclipse.swt.widgets.Control.internal_new_GC(Control.java:2134)
	at org.eclipse.swt.graphics.GC.<init>(GC.java:171)
	at org.eclipse.swt.graphics.GC.<init>(GC.java:134)
	at com.syntevo.q.swt.QTabFolder.updateItems(SourceFile:806)
...

The 2134th line of Control.java reads:

		NSGraphicsContext flippedContext = NSGraphicsContext.graphicsContextWithGraphicsPort(graphicsContext.graphicsPort(), true);

so it looks like under certain conditions graphicsContext might be null.
Comment 1 Thomas Singer CLA 2014-05-08 04:57:48 EDT
Another stacktrace after restarting the application is:

java.lang.NullPointerException
	at org.eclipse.swt.widgets.Control.internal_new_GC(Control.java:2134)
	at org.eclipse.swt.graphics.GC.<init>(GC.java:171)
	at org.eclipse.swt.graphics.GC.<init>(GC.java:134)
	at org.eclipse.swt.custom.CLabel.getTotalSize(CLabel.java:260)
	at org.eclipse.swt.custom.CLabel.onPaint(CLabel.java:400)
	at org.eclipse.swt.custom.CLabel$1.paintControl(CLabel.java:121)
	at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:230)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
	at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4166)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1466)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1489)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1474)
	at org.eclipse.swt.widgets.Control.drawWidget(Control.java:1244)
	at org.eclipse.swt.widgets.Canvas.drawWidget(Canvas.java:166)
	at org.eclipse.swt.widgets.Widget.drawRect(Widget.java:749)
	at org.eclipse.swt.widgets.Canvas.drawRect(Canvas.java:161)
	at org.eclipse.swt.widgets.Display.windowProc(Display.java:5534)
	at org.eclipse.swt.internal.cocoa.OS.objc_msgSend_bool(Native Method)
	at org.eclipse.swt.internal.cocoa.NSRunLoop.runMode(NSRunLoop.java:42)
	at org.eclipse.swt.widgets.Display.sleep(Display.java:4610)
Comment 2 Thomas Singer CLA 2014-07-14 13:09:35 EDT
*** Bug 439238 has been marked as a duplicate of this bug. ***
Comment 3 Stephan Aßmus CLA 2014-11-12 06:15:38 EST
This bug has been present in the OS X version of SWT for a while. I've never seen it myself, but only in crash-logs from users.

I've surrounded code which creates a new GC for a given Widget (for example to compute the size) with try-catch-blocks if it is non-critical code. From the logs we receive from users, the error seems to be non-permanent. It also appears to affect only certain places in the code, i.e. I have more places where a GC is created, but only some throw the error. In a certain version of our software, a place started to throw NPEs which previously never triggered it, but there were no changes in that part of the code for a number of versions. I have only one faint suspicion which is that the GC is created completely outside of any drawing or layout related event. I.e. a string changes in the interface, the app doesn't call layout() on any Composite directly, but instead wants to figure out if a layout is necessary by first computing whether the size actually changed in a certain way.
Comment 4 Thomas Singer CLA 2015-06-09 08:36:51 EDT
Also has been for the latest OS X version XI.
Comment 5 Thomas Singer CLA 2015-06-10 05:59:15 EDT
Alot of users report this problem for SmartGit 6.5 on OS X 10.11 preview - much more often than for any previous OS X version.
Comment 6 Thomas Singer CLA 2015-06-10 14:23:46 EDT
Here are a couple of more similar stacktraces:

java.lang.NullPointerException
	at org.eclipse.swt.widgets.Control.internal_new_GC(Control.java:2118)
	at org.eclipse.swt.graphics.GC.cocoa_new(GC.java:207)
	at org.eclipse.swt.widgets.Table.sendMeasureItem(Table.java:3088)
	at org.eclipse.swt.widgets.Table.cellSize(Table.java:235)
	at org.eclipse.swt.widgets.Display.windowProc(Display.java:5517)
	at org.eclipse.swt.internal.cocoa.OS.objc_msgSendSuper_stret(Native Method)
	at org.eclipse.swt.widgets.Widget.expansionFrameWithFrame_inView(Widget.java:798)
	at org.eclipse.swt.widgets.Table.expansionFrameWithFrame_inView(Table.java:1182)
	at org.eclipse.swt.widgets.Display.windowProc(Display.java:5896)
	at org.eclipse.swt.internal.cocoa.OS.objc_msgSend_bool(Native Method)
	at org.eclipse.swt.internal.cocoa.NSRunLoop.runMode(NSRunLoop.java:42)
	at org.eclipse.swt.widgets.Display.sleep(Display.java:4680)

java.lang.NullPointerException
	at org.eclipse.swt.widgets.Control.internal_new_GC(Control.java:2118)
	at org.eclipse.swt.graphics.GC.cocoa_new(GC.java:207)
	at org.eclipse.swt.widgets.Tree.sendMeasureItem(Tree.java:2593)
	at org.eclipse.swt.widgets.Tree.cellSize(Tree.java:288)
	at org.eclipse.swt.widgets.Display.windowProc(Display.java:5517)
	at org.eclipse.swt.internal.cocoa.OS.objc_msgSendSuper_stret(Native Method)
	at org.eclipse.swt.widgets.Widget.expansionFrameWithFrame_inView(Widget.java:798)
	at org.eclipse.swt.widgets.Tree.expansionFrameWithFrame_inView(Tree.java:1271)
	at org.eclipse.swt.widgets.Display.windowProc(Display.java:5896)
	at org.eclipse.swt.internal.cocoa.OS.objc_msgSend_bool(Native Method)
	at org.eclipse.swt.internal.cocoa.NSRunLoop.runMode(NSRunLoop.java:42)
	at org.eclipse.swt.widgets.Display.sleep(Display.java:4680)
Comment 7 Thomas Singer CLA 2015-06-17 02:17:51 EDT
Since the availability of OS X 10.10 preview this became the most often reported bug for us. Please fix in 4.5!
Comment 8 Lakshmi P Shanmugam CLA 2015-06-17 06:31:35 EDT
Can you please provide the SWT version number/build number along with the stack traces to figure out where the NPE is coming from?
Comment 9 Thomas Singer CLA 2015-06-17 12:43:31 EDT
(In reply to Thomas Singer from comment #6)
> Here are a couple of more similar stacktraces:

These stacktraces are from SWT 4.4.0 (version.txt shows "version 4.427").
Comment 10 Lakshmi P Shanmugam CLA 2015-06-19 07:49:19 EDT
(In reply to Thomas Singer from comment #9)
> (In reply to Thomas Singer from comment #6)
> > Here are a couple of more similar stacktraces:
> 
> These stacktraces are from SWT 4.4.0 (version.txt shows "version 4.427").

Line 2118 is - context = graphicsContext.id;
That means NSGraphicsContext.currentContext() is returning null in some cases. But, from the documentation, it looks like it should return a non-null value.
Comment 11 Stephan Aßmus CLA 2015-06-19 07:57:04 EDT
As noted in comment 3, I am observing these NPEs only where I want to measure the size of Widget, when the context is not a paint event or some other OS generated event. In many of the stack traces posted here, the situation seems to be similar... but not all of them (comment 1). Still maybe its worth exploring?
Comment 12 Stephan Aßmus CLA 2015-10-02 09:00:42 EDT
It seems that under El Capitan, the error has become much more frequent. I've upgraded my Mac to 10.11, downloaded Eclipse Mars, and the after a few minutes of use, Eclipse asked me whether I wanted to take part in the user error reporting. It turned out that it hit this bug. So it is Eclipse itself which is also affected. It happens in the Tree widget when it wants to measure items (SWT.MeasureItem event, if memory serves). This is exactly the same stack trace that we get with out application.
Comment 13 Thomas Singer CLA 2015-10-08 10:47:23 EDT
Created attachment 257162 [details]
Sample to reproduce
Comment 14 Thomas Singer CLA 2015-10-08 10:48:02 EDT
I've attached a sample to reproduce. Just hover the mouse over one (too narrow) cell on OS X 10.11 and it crashs.
Comment 15 Thomas Singer CLA 2015-10-13 08:19:31 EDT
Are you able to reproduce this NPE using the sent snippet?
Comment 16 Lakshmi P Shanmugam CLA 2015-10-14 07:23:10 EDT
(In reply to Thomas Singer from comment #15)
> Are you able to reproduce this NPE using the sent snippet?

Thanks for the snippet. I'm able to reproduce the NPE. It's interesting to see that the same code NSGraphicsContext.currentContext() that was working in OSX 10.10 & below, is failing in OSX 10.11.
I'm currently investigating the problem.
Comment 17 Lakshmi P Shanmugam CLA 2015-10-19 04:35:09 EDT
*** Bug 479007 has been marked as a duplicate of this bug. ***
Comment 18 Marcel Bruch CLA 2015-10-22 03:03:06 EDT
FWIW, this issue is actually looks big. I've seen this error on Trees:


https://dev.eclipse.org/recommenders/committers/confess/#/problems/56267294e4b0b60733e13bf1/details
https://dev.eclipse.org/recommenders/committers/confess/#/problems/5606f786e4b0d155bf9793d7/details
https://dev.eclipse.org/recommenders/committers/confess/#/problems/56254703e4b0be0032730160/details

I'm yet not able to express this in a comprehensive search query but if you are looking for other examples you may find the query below helpful (subjects should be NullPointerException in org.eclipse.swt.widgets.Control.internal_new_GC)



https://dev.eclipse.org/recommenders/committers/confess/#/problems/search?frame=org.eclipse.swt.widgets.Control.internal_new_GC
Comment 19 Thomas Singer CLA 2015-10-22 03:10:14 EDT
Yes, this is currently our most serious bug on OS X 10.11 in SWT with SmartGit/SmartSVN - we've already got ~40,000 reports.
Comment 20 Arun Thondapu CLA 2015-10-28 03:28:24 EDT
*** Bug 480816 has been marked as a duplicate of this bug. ***
Comment 21 Alexander Stahlberg CLA 2015-11-18 09:32:38 EST
(In reply to Thomas Singer from comment #19)
> Yes, this is currently our most serious bug on OS X 10.11 in SWT with
> SmartGit/SmartSVN - we've already got ~40,000 reports.

I noticed in recent SmartSVN versions this error does not occur anymore. Did you find a solution to this problem? We're currently facing the very same problems with our SWT based applications.
Comment 22 Thomas Singer CLA 2015-11-18 10:51:44 EST
In our products we catch the exceptions with Table.sendMeasureItem and Tree.sendMeasureItem and ignore them silently.
Comment 23 Sven Wende CLA 2015-11-24 18:24:44 EST
(In reply to Thomas Singer from comment #22)
> In our products we catch the exceptions with Table.sendMeasureItem and
> Tree.sendMeasureItem and ignore them silently.

Can you please describe how you catch those exceptions?
Comment 24 Thomas Singer CLA 2015-11-25 04:57:32 EST
(In reply to Sven Wende from comment #23)
> (In reply to Thomas Singer from comment #22)
> > In our products we catch the exceptions with Table.sendMeasureItem and
> > Tree.sendMeasureItem and ignore them silently.
> 
> Can you please describe how you catch those exceptions?

With Thread.setDefaultUncaughtExceptionHandler() you can set an own instance of Thread.UncaughtExceptionHandler. This will be used by SWT (and AWT). Then just either ignore the exception or otherwise process it, e.g. log.
Comment 25 Lakshmi P Shanmugam CLA 2015-11-30 06:04:36 EST
Created attachment 258355 [details]
patch

NSGraphicsContext currentContext() can return null when called from a
non-drawing method, for eg, Table.cellSize(). The fix to is to check if
the context returned is valid, if not we create a new context using
NSGraphicsContext.graphicsContextWithWindow.

The patch works with the above snippet. I'm still testing the different cases.
Comment 26 Lakshmi P Shanmugam CLA 2015-12-02 01:23:56 EST
Pushed above patch to master for 4.5M4 --> http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=7bbf8d4559e0a792751c656ac72cdd3326f6a324
Comment 27 Lakshmi P Shanmugam CLA 2015-12-02 01:28:08 EST
(In reply to Lakshmi Shanmugam from comment #26)
> Pushed above patch to master for 4.5M4 -->
Sorry, I meant pushed patch to master for 4.6M4
Comment 28 Lakshmi P Shanmugam CLA 2015-12-08 12:11:39 EST
Verified in I20151207-2000
Comment 30 Lakshmi P Shanmugam CLA 2016-01-22 02:39:59 EST
Verified in M20160120-1000.
Comment 31 Lakshmi P Shanmugam CLA 2016-01-27 05:53:27 EST
*** Bug 481821 has been marked as a duplicate of this bug. ***
Comment 32 Arun Thondapu CLA 2016-02-02 09:04:24 EST
*** Bug 486962 has been marked as a duplicate of this bug. ***
Comment 33 Thomas Singer CLA 2016-02-15 03:30:09 EST
We've got NPEs reported because the following new line in internal_new_GC()

 graphicsContext = NSGraphicsContext.graphicsContextWithWindow (window);

returns null, so the next line's graphicsContext.graphicsPort() will fail.
Comment 34 Lakshmi P Shanmugam CLA 2016-02-15 06:36:42 EST
(In reply to Thomas Singer from comment #33)
> We've got NPEs reported because the following new line in internal_new_GC()
> 
>  graphicsContext = NSGraphicsContext.graphicsContextWithWindow (window);
> 
> returns null, so the next line's graphicsContext.graphicsPort() will fail.

Can you attach the stacktrace and build-id on which the problem occurs?
Comment 35 Thomas Singer CLA 2016-02-15 10:32:44 EST
It was reported against the latest SmartGit 7.1.0 release which uses SWT 4.610 according to version.txt. Here is the stacktrace:

java.lang.NullPointerException
	at org.eclipse.swt.widgets.Control.internal_new_GC(Control.java:2174)
	at org.eclipse.swt.graphics.GC.<init>(GC.java:174)
	at org.eclipse.swt.graphics.GC.<init>(GC.java:135)
	at org.eclipse.swt.custom.StyledTextRenderer.setFont(StyledTextRenderer.java:1108)
	at org.eclipse.swt.custom.StyledText.<init>(StyledText.java:1252)
...
Comment 36 Lakshmi P Shanmugam CLA 2016-02-17 05:09:37 EST
(In reply to Thomas Singer from comment #35)
> It was reported against the latest SmartGit 7.1.0 release which uses SWT
> 4.610 according to version.txt. Here is the stacktrace:
> 
> java.lang.NullPointerException
> 	at org.eclipse.swt.widgets.Control.internal_new_GC(Control.java:2174)
> 	at org.eclipse.swt.graphics.GC.<init>(GC.java:174)
> 	at org.eclipse.swt.graphics.GC.<init>(GC.java:135)
> 	at
> org.eclipse.swt.custom.StyledTextRenderer.setFont(StyledTextRenderer.java:
> 1108)
> 	at org.eclipse.swt.custom.StyledText.<init>(StyledText.java:1252)
> ...

I looked at the complete list of the reported errors for this problem [1]. The NPE happens at 2 places. One is as shown in comment#6 (when MeasureItem is sent). 
Other is similar to comment#1 (in new GC constructor).
See -- https://dev.eclipse.org/recommenders/committers/confess/#/problems/55cc3af7e4b0176300a7b2b0/
-- https://dev.eclipse.org/recommenders/committers/confess/#/problems/56434cf9e4b0c02da2cfeb42/

The fix in this bug has addressed the first problem, NPE when MeasureItem event is sent. NPE in GC constructor happens in some cases and that needs to be addressed. I've opened a new bug to track this - Bug 487922.


[1] https://dev.eclipse.org/recommenders/committers/confess/#/problems/?stackTraceElement=org.eclipse.swt.widgets.Control.internal_new_GC&project=swt&os=macosx&projection=overview&page=0&size=20&sort=modifiedOn,desc
Comment 37 Lakshmi P Shanmugam CLA 2016-03-28 06:13:14 EDT
*** Bug 483379 has been marked as a duplicate of this bug. ***
Comment 38 Marc-André Laperle CLA 2017-05-17 14:36:22 EDT
*** Bug 487922 has been marked as a duplicate of this bug. ***
Comment 39 Rolf Theunissen CLA 2022-02-01 06:43:23 EST
*** Bug 578502 has been marked as a duplicate of this bug. ***