Bug 215551 - [Qyoto][PATCH] QApplication constructor crashes when run through NUnit
Summary: [Qyoto][PATCH] QApplication constructor crashes when run through NUnit
Status: RESOLVED FIXED
Alias: None
Product: bindings
Classification: Developer tools
Component: general (show other bugs)
Version: unspecified
Platform: Ubuntu Linux
: NOR crash
Target Milestone: ---
Assignee: kde-bindings
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-11-21 13:13 UTC by Tobias Kappé
Modified: 2009-11-29 00:44 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
Proposed patch (3.32 KB, patch)
2009-11-27 21:51 UTC, Tobias Kappé
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Kappé 2009-11-21 13:13:59 UTC
Version:            (using KDE 4.3.3)
OS:                Linux
Installed from:    Ubuntu Packages

When the constructor for QApplication is called through NUnit (for example, when initialising Qt in TestFixtureSetup to test dialogs), it throws a NullReferenceException. This is caused by System.Reflection.Assembly.GetEntryAssembly() returning null.

To work around this, System.Reflection.Assembly.GetExecutingAssembly() could be called when the call to GetEntryAssembly fails. A patch is included that provides for this. Also, somewhat redundant code has been factored out. 

=== modified file 'csharp/qyoto/gui/QApplicationExtras.cs'
--- csharp/qyoto/gui/QApplicationExtras.cs	2009-11-21 11:39:37 +0000
+++ csharp/qyoto/gui/QApplicationExtras.cs	2009-11-21 11:53:55 +0000
@@ -6,24 +6,34 @@
 	using System.Text;
 
 	public partial class QApplication : QCoreApplication, IDisposable {
+	
+		string[] GenerateArgs(string[] argv)
+		{
+			string[] args = new string[argv.Length + 1];
+			Assembly a = System.Reflection.Assembly.GetEntryAssembly();
+			
+			if(a == null)
+				a = System.Reflection.Assembly.GetExecutingAssembly();
+			
+			object[] attrs = a.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
+			if (attrs.Length > 0) {
+				args[0] = ((AssemblyTitleAttribute) attrs[0]).Title;
+			} else { 
+				QFileInfo info = new QFileInfo(a.Location);
+				args[0] = info.BaseName();
+			}
+			argv.CopyTo(args, 1);
+			
+			return args;		
+		}
 
 		public QApplication(string[] argv) : this((Type) null) {
 			CreateProxy();
 			Qt.qApp = this;
-      
-			string[] args = new string[argv.Length + 1];
-			Assembly a = System.Reflection.Assembly.GetEntryAssembly();
-			object[] attrs = a.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
-			if (attrs.Length > 0) {
-				args[0] = ((AssemblyTitleAttribute) attrs[0]).Title;
-			} else { 
-				QFileInfo info = new QFileInfo(a.Location);
-				args[0] = info.BaseName();
-			}
-			argv.CopyTo(args, 1);
+			
+			string[] args = GenerateArgs(argv);
 
-			interceptor.Invoke(	"QApplication$?", 
-								"QApplication(int&, char**)", 
+			interceptor.Invoke(	"QApplication$?", "QApplication(int&, char**)", 
 								typeof(void), typeof(int), args.Length, typeof(string[]), args );
 			SetupEventReceiver();
 		}
@@ -32,40 +42,20 @@
 			CreateProxy();
 			Qt.qApp = this;
 			
-			string[] args = new string[argv.Length + 1];
-			Assembly a = System.Reflection.Assembly.GetEntryAssembly();
-			object[] attrs = a.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
-			if (attrs.Length > 0) {
-				args[0] = ((AssemblyTitleAttribute) attrs[0]).Title;
-			} else { 
-				QFileInfo info = new QFileInfo(a.Location);
-				args[0] = info.BaseName();
-			}
-			argv.CopyTo(args, 1);
+			string[] args = GenerateArgs(argv);
 
-			interceptor.Invoke(	"QApplication$?", 
-								"QApplication(int&, char**)", 
+			interceptor.Invoke(	"QApplication$?", "QApplication(int&, char**)", 
 								typeof(void), typeof(int), args.Length, typeof(string[]), args, typeof(bool), GUIenabled );
 			SetupEventReceiver();
 		}
-    
+	
 		public QApplication(string[] argv, QApplication.TypeOf arg3) : this((Type) null) {
 			CreateProxy();
 			Qt.qApp = this;
 			
-			string[] args = new string[argv.Length + 1];
-			Assembly a = System.Reflection.Assembly.GetEntryAssembly();
-			object[] attrs = a.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
-			if (attrs.Length > 0) {
-				args[0] = ((AssemblyTitleAttribute) attrs[0]).Title;
-			} else { 
-				QFileInfo info = new QFileInfo(a.Location);
-				args[0] = info.BaseName();
-			}
-			argv.CopyTo(args, 1);
+			string[] args = GenerateArgs(argv);
 
-			interceptor.Invoke(	"QApplication$?", 
-								"QApplication(int&, char**)", 
+			interceptor.Invoke(	"QApplication$?", "QApplication(int&, char**)", 
 								typeof(void), typeof(int), args.Length, typeof(string[]), args, typeof(QApplication.TypeOf), arg3 );
 			SetupEventReceiver();
 		}
Comment 1 Arno Rehn 2009-11-27 17:41:23 UTC
Hi, could you please post your patch as an attachment? All the formatting and many non-linebreaks go lost when posting it in-line with the rest of the text,
Comment 2 Tobias Kappé 2009-11-27 21:51:57 UTC
Created attachment 38641 [details]
Proposed patch

My bad, here's the attachment (I could not add one when creating the bug but I now realise this would have worked too).
Comment 3 Arno Rehn 2009-11-29 00:26:47 UTC
Applied in r1055893. Thanks for the patch!
Comment 4 Tobias Kappé 2009-11-29 00:44:42 UTC
You're welcome, thanks for applying it :)