Error encountered when copying from Marvin

User 74b72c099e

14-06-2011 14:02:15

I am using the .NET API to launch Marvin, draw a structure and return it to my application. This all works fine. However, I have started encountering problems when trying to copy a structure in the MarvinSketchForm window I have opened from my WPF application. I cannot reproduce the error the first time I launch the MarvinSketchForm from my application but if I launch it a second time from the same instance of my application it will crash whenever I mark the structure I have drawn and press ctrl + c (or right click and choose "Copy"). The error I get is the following:


The runtime has encountered a fatal error. The address of the error was at 0x6d087788, on thread 0x14d0. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

Since this only happens after opening the Marvin window from the application more than once I suspet that I might not close it properly in my application when returning the structure. However, using "Dispose()" on my MarvinSketchForm-object has not seemed to solve the problem.


Copy/Paste of structures works fine within my application using the chemaxon.marvin.modules.datatransfer.ClipboardHandler.

ChemAxon eb65a25631

15-06-2011 15:45:40

Hi,


I tried to reproduce the issue:


I created a WPF application.


Added the necessary assemblies.


Changed the target to '.NET 4 Framework'.


Added a simple button.


And, added the following lines into the buttons eventhandler:



 MarvinSketchForm msf = new MarvinSketchForm();




msf.ShowAsDialog();




 



 



This way, I could not reproduce the issue you posted. I succesfully managed to copy/paste every time I opened the MarvinSketchForm in the same application instance.


I was using JChemAPI 5.5


Which version do you use?


Is it possible to send an example code, that produces this issue?

User 74b72c099e

16-06-2011 07:26:33

Here are examples of the code I use:


private IStructure OpenDrawEditorXChemAxon(IStructure structure)
{
var returnStructure = structure;
var renditor = new ChemAxonRenditorWrapper();
renditor.MolfileString = structure.Molfile;

renditor.EditorReturned += delegate(object sender, EditorReturnedEventArgsWrapper e)
{
if (_isEditable && e.Validated)
returnStructure = new Structure(renditor.MolfileString);
};

renditor.FireEditor();

return returnStructure;
}

public void FireEditor()
{
var type = ChemAxon.ChemAxonAssemblies.ChemAxonNetIKVM.GetType("ChemAxon.NET.IKVM.Chemistry.MolConverter", true);
dynamic converter = Activator.CreateInstance(type);

var editorType = ChemAxon.ChemAxonAssemblies.ChemAxonNetWindowsForms.GetType("ChemAxon.NET.Windows.Forms.MarvinEditorControl.MarvinSketchForm", true);
dynamic editor2 = Activator.CreateInstance(editorType);

if (!(string.IsNullOrWhiteSpace(MolfileString) || MolfileString.Equals(Structure.NoStructure.Molfile) || MolfileString.Equals(Structure.Empty.Molfile)))
editor2.MoleculeString = (string)ReflectionHelper.InvokeMethod(converter, "ConvertToStringFormat", null, MolfileString, "MOL", "MRV");

try
{
if (editor2.ShowAsDialog() ?? false)
{
MolfileString = ReflectionHelper.InvokeMethod(converter, "ConvertToStringFormat", null, editor2.MoleculeString, "MRV", "MOL");
EditorReturned(this, new StructureUtils.EditorReturnedEventArgsWrapper(true));
}
else
{
EditorReturned(this, new StructureUtils.EditorReturnedEventArgsWrapper(false));
}
}
finally
{
editor2.Dispose();
editor2 = null;
converter = null;
}
}

public event EventHandler<StructureUtils.EditorReturnedEventArgsWrapper> EditorReturned;

I also use the JChem API 5.5 and .NET 4.

ChemAxon eb65a25631

17-06-2011 11:40:37

I created a sample code based on your code, but I still cannot reproduce the issue.


Do you experience the issue with the code I posted earlier?


One more thing might be relevant: is it an x64 .NET application built with release configuration?

User 74b72c099e

22-06-2011 08:02:38

I compile the application using "Any CPU" and Debug configuartion.


 


When I run my application, the time it takes to open MarvinSketch the first time is much longer than when I open it again later from the same instance. The problem only occurs when I have pressed "OK" in any of the MarvinSketch windows opened previously in the application session. In other words: The problem does not occur as long as I press "Cancel" in every single MarvinSketch window, even if I open them several times. However, as soon as I press "OK" (and thereby return a structure to my application) any MarvinSketch window opened later will return the mentioned error if i try to copy the structure.

ChemAxon bd13b5bd77

22-06-2011 12:14:40

Hi Mikkel,

 



have you tried out hosting the 
ChemAxon.NET.Windows.Forms.MarvinEditorControl.MarvinEditorControl

 



directly on a WPF host (for WInForms controls) and you can implement the ok cancel very easily to listen to the

 




ExitEditor event.

 


To hide the form on the perfect thread you can use this solution:



        private void editor_ExitEditor(object sender, EventArgs e) {
           try {
              var dialogResult = MessageBox.Show("Would you like to transfer the molecule back?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
              Button btnToPress = (dialogResult == DialogResult.Yes) ? this.OK : this.Cancel;
              this.BeginInvoke(new MethodInvoker(() => btnToPress.PerformClick()));
           }
           catch { }
        }


It is just an example and upon on your taste.


Viktor 


 


 

 



 

 


 






 



 


User 74b72c099e

05-07-2011 08:15:48

I never managed to get the control working. Embedding other (simpler) Windows Forms controls in my WPF application was no problem but not the MarvinEditorControl. I still experience problems with the MarvinSketchForm when trying to copy a structure.

ChemAxon eb65a25631

05-07-2011 08:22:06

Hi,


 


Did you try the sugggestion I posted earlier?


'


















Posted: Wed Jun 15, 2011 4:45 pmPost subject:  




Hi,

 


I tried to reproduce the issue:

 


I created a WPF application.

 


Added the necessary assemblies.

 


Changed the target to '.NET 4 Framework'.

 


Added a simple button.

 


And, added the following lines into the buttons eventhandler:

 



 MarvinSketchForm msf = new MarvinSketchForm();

 




msf.ShowAsDialog();

 














'


What are the results?

ChemAxon bd13b5bd77

05-07-2011 08:27:16

Hi,


 


have you checked the post of this guy:


https://www.chemaxon.com/forum/ftopic7717.html


https://www.chemaxon.com/forum/ftopic7400.html


https://www.chemaxon.com/forum/ftopic7426.html


?


I think he is using WPF too, maybe it is worth asking him if he used something special in WPF to get the control working. If you need me to help in that communication please let me know.


 


Viktor

User 74b72c099e

18-07-2011 09:08:35










akovacs wrote:

Hi,


 


Did you try the sugggestion I posted earlier?


'


















Posted: Wed Jun 15, 2011 4:45 pmPost subject:  




Hi,


 


I tried to reproduce the issue:


 


I created a WPF application.


 


Added the necessary assemblies.


 


Changed the target to '.NET 4 Framework'.


 


Added a simple button.


 


And, added the following lines into the buttons eventhandler:


 




 MarvinSketchForm msf = new MarvinSketchForm();


 



 


msf.ShowAsDialog();


 


 


 



 



 


 


 


 


 


 


 


 



'


What are the results?



Yeah, I've tried something similar (although I of course need to set msf.MoleculeString to load my structure) and experience the same issues.


If I instead of pressing ctrl + C press ctrl + K (Copy As...) and try to use any of the special formats there's no problem at all. Only 'Marvin Object (OLE)' and 'EMF Image' makes the problem appear - but this is however consistently appearing.

ChemAxon eb65a25631

18-07-2011 10:28:02

I could not reproduce your problem even with copying as Marvin OLE or EMF object.


It is very much likely, there is something with your machine, or what you installed earlier (maybe some clipboard-viewer app.?)


I suggest trying the code on other machines as well.


 

User 74b72c099e

18-07-2011 10:53:53

The problem appears on multiple different machines.

User 74b72c099e

05-09-2011 09:13:23

I have tried to make a small WPF application only containing the essential parts of the structure drawing and can't reproduce the problem here meaning that there must be some kind of conflict or mistake in the code of my main application. However, I really have problems locating the source of the problem making it very hard to fix.


I have encountered the exact same error even without opening the Marvin Editor: If I use the chemaxon.marvin.modules.datatransfer.ClipboardHandler.putMoleculeToClipboard function to copy the structure to the Clipboard as an OLE object, my application crashes in the same manner - but not until I attempt to copy a structure the second time during a given instance. I haven't been able to reproduce this in my small test application either. I realize that makes it very difficult for you to suggest any possible solutions. The problems only occur when I try to handle an OLE object as mentioned previously.


Oh, and I don't think the problem stems from my specific machine. The one I am currently using is completely new with very few applications installed. As mentioned earlier the problem has been reproduced on several different machines.

ChemAxon bd13b5bd77

14-09-2011 05:30:22

Hi Mikkel,


what if you start separating out the code of Edtor integration part into a small component? As you wrote down if you use a smaller code base the error does not come at all.


Please add a new dll library with the editor adapter, which can be veryfied from a test application, and if it works the next step can be to use this component assembly from the "bigger" more complexed environment.


Lets see what happens.


Viktor

User 74b72c099e

15-09-2011 07:00:35

I have managed to create a small example code that (sometimes) crashes when I try to copy the structure as a Marvin OLE object. The crash happens when I try to run a unit test using NUnit (using the debug option). Weirdly I can't reproduce the problem every time I run the code - but it does happen often.


using System;
using System.Diagnostics;
using NUnit.Framework;

namespace ChemAxonCrash
{
public static class Program
{
const string structureString = @"
Mrv0541 09151108482D

46 55 0 0 0 0 999 V2000
-3.6830 3.5357 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-4.3975 3.9482 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-5.1120 3.5357 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-5.1120 2.7107 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-4.3975 2.2982 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-3.6830 2.7107 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-2.9686 2.2982 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-2.2541 2.7107 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-2.2541 3.5357 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-2.9686 3.9482 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.8251 2.7107 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-1.5395 3.1232 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-2.2541 1.8857 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-1.5395 1.4732 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.8251 1.8857 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.1106 1.4732 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.6039 1.8857 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.6039 2.7107 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.1106 3.1232 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.0329 1.8857 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.3184 2.2982 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.6039 1.0607 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.3184 0.6482 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.0329 1.0607 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.7473 0.6482 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.4619 1.0607 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
3.4619 1.8857 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.7473 2.2982 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.6040 0.2357 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.1105 0.6482 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.8250 0.2357 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.8250 -0.5893 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.1105 -1.0018 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.6040 -0.5893 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.3184 -1.0018 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.0330 -0.5893 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.0330 0.2357 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.8249 -1.4143 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-1.5394 -1.0018 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-2.2539 -1.4143 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-2.2539 -2.2393 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-1.5394 -2.6518 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.8249 -2.2393 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.1105 -2.6518 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.6041 -2.2393 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.6041 -1.4143 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 0 0
1 6 2 0 0 0 0
1 10 1 0 0 0 0
2 3 2 0 0 0 0
3 4 1 0 0 0 0
4 5 2 0 0 0 0
5 6 1 0 0 0 0
6 7 1 0 0 0 0
7 8 2 0 0 0 0
8 9 1 0 0 0 0
9 10 2 0 0 0 0
12 8 2 0 0 0 0
8 13 1 0 0 0 0
11 12 1 0 0 0 0
11 15 2 0 0 0 0
11 19 1 0 0 0 0
13 14 2 0 0 0 0
14 15 1 0 0 0 0
15 16 1 0 0 0 0
16 17 2 0 0 0 0
17 18 1 0 0 0 0
18 19 2 0 0 0 0
21 17 2 0 0 0 0
17 22 1 0 0 0 0
20 21 1 0 0 0 0
20 24 2 0 0 0 0
20 28 1 0 0 0 0
22 23 2 0 0 0 0
23 24 1 0 0 0 0
24 25 1 0 0 0 0
25 26 2 0 0 0 0
26 27 1 0 0 0 0
27 28 2 0 0 0 0
37 23 2 0 0 0 0
29 23 1 0 0 0 0
29 30 1 0 0 0 0
29 34 2 0 0 0 0
30 31 2 0 0 0 0
31 32 1 0 0 0 0
32 33 2 0 0 0 0
33 34 1 0 0 0 0
34 35 1 0 0 0 0
35 36 2 0 0 0 0
36 37 1 0 0 0 0
46 33 2 0 0 0 0
38 33 1 0 0 0 0
38 39 1 0 0 0 0
38 43 2 0 0 0 0
39 40 2 0 0 0 0
40 41 1 0 0 0 0
41 42 2 0 0 0 0
42 43 1 0 0 0 0
43 44 1 0 0 0 0
44 45 2 0 0 0 0
45 46 1 0 0 0 0
M END
";

static void Main()
{
ExecuteCopying();
Console.ReadKey();
}

public static void ExecuteCopying()
{
const string structure1 = structureString;
for (var i = 0; i < 100; i++)
{
CopyToClipboard(structure1);
}
Console.WriteLine("All done");
}

public static void CopyToClipboard(string structure)
{
var properties = new java.util.Properties();
var molecule = chemaxon.formats.MolImporter.importMol(structure);
chemaxon.marvin.modules.datatransfer.ClipboardHandler.putMoleculeToClipboard(molecule, properties);
}
}

class Test
{
[Test]
public static void RunTest()
{
Program.ExecuteCopying();
}
}
}

ChemAxon bd13b5bd77

15-09-2011 07:02:39

Thank you, we start working on it ....

User 74b72c099e

15-09-2011 08:19:37

Thank you - I look forward to hear if you can reproduce the problem.

ChemAxon eb65a25631

15-09-2011 12:47:32

Hello,


 


I think, I managed to reproduce the problem in one case:


Your code crashes sometimes th QTAgent32.exe with access violation using a unittest in release mode.


However the access violation cannot bre reproduced with the same code called in a simple console application.

User 74b72c099e

21-09-2011 07:30:20

I have not been able to reproduce the error in the console application either. But it appears consistently in my main WPF application. The error I encounter when running the unit test appears to be the same as the one in my WPF application. Do you think it could be QTAgent32.exe that is also my problem there? And in this case, do you have any suggestions on how to solve the problem?

ChemAxon eb65a25631

21-09-2011 10:19:33

Hi Mikkel,


 


I've done some in-depth debug and I faced these not too 'invigorating' results:


- I completely replaced the clipboard-handling implementation with with a latter version of IKVM (the two code were essentially different): I got the same result.


- I experienced with stripping down the exisiting clipboard-handling code to see exactly what lines cause the exception: any one line of code related to clipboard left  in, the result were the same.


These lead me to the following observation:


- It is possible, that a problem might exists in the core of the IKVM implementation, very unlikely to uncover it anytime if ever.


- The exception is a native exception, and it suggests me, the problem is strictly tied to how the managed code accesses and stores (maybe disposes) data on the native clipboard.


- I also suspecting QTAgent32 has to do sometihing with the clipboard, or the data on the clipboard during the tests run.


I have one idea left: i try to recreate the testcase without ikvm, and see what's happening, otherwise I'm completely puzzled.


What still bugs me though, that you can reproduce the issue with the user clicking only on the UI, and the test case can only reproduce it once in every 15-60 repetition when the operation  runs in a close repetition.


 


Regards,


András

User 74b72c099e

26-09-2011 07:32:21

I'm glad you can at least reproduce the problem now. Do you think it is something that can/will be fixed? Or do you have any suggestions for a workaround? Copying/pasting structures is an essential feature for the users of the application I'm working on.

ChemAxon eb65a25631

26-09-2011 10:39:19

I agree, copy/paste is essential for the users.


Currently, I'm trying to find a fix for the issue, but I'm not any closer yet.


Could you check one thing for me:


In the wpf application, how many running 'IKVM AWT WinForms Message Loop' thread are there, in the moment of the exception?

User 74b72c099e

27-09-2011 08:59:32

There are two running 'IKVM AWT WinForms Message Loop' threads when the WPF application crashes. This is the case in both cases (either if I crash the program via the MarvinSketch editor or by copying compounds using the same commands used in the small test application with the unit test posted above).

ChemAxon eb65a25631

30-09-2011 08:01:21

Hi Mikkel,


I may have resolved the issue with the unittest framework. There is no longer an access violation exception thrown during cosecutive execution of putting molecule to the clipboard.


I have to negotiate wih my colleagues, how we can provide a patch, or a patched development release package, that you can use, to test it against your wpf application.


I'll notify you when it's ready.


András

ChemAxon eb65a25631

30-09-2011 14:22:05

It's a little bit slow process to create a custom patched release.


So, Is it OK for you if I just send you the patched assemblies, and write down any necessary steps for 'installing' and uninstalling?


 

User 74b72c099e

03-10-2011 07:19:33

That would be fine :) You can send them to ja at macom dot dk.

ChemAxon eb65a25631

03-10-2011 09:07:28

Hi Mikkel,


 


You can download the package from:


http://www.chemaxon.com/shared/JChem_5.8pre_test.zip


i cannot reproduce the probém with the unittest fw., please try this patched package with your wpf app.


Depending on how you want to access the assemblies, you can either put them (ikvm*.dll, Jchem*.dll) into the GAC or to a directory your project loads from.


In a Visual Strudio 2010 commadn prompt:


regasm shared\ManagedEmfExporter.dll


You may also put the Interop.CXNEmfExporter.dll assembly to the same place where the ikvm/jchem dll's reside.


I hope these are all the steps are needed.


uninstallation:


regasm /unregister shared\ManagedEmfExporter.dll


In case of GAC usage remove all the ikvm*.dll (0.42.1.75), JChem*.dll(5.8), Interop.CXNEmfExporter.dll from the GAC.


A reinstallation of the previous JChem.NET package may be needed as well.


 

User 74b72c099e

03-10-2011 12:36:38

Thank you very much! However, I get an error when I try to execute the regasm command. It seems like it is looking for a IKVM.AWT.WinForms assembly with version 0.42.1.76 which I don't have. Shouldn't it be 0.42.1.75?

ChemAxon eb65a25631

03-10-2011 13:02:32

I attached the ManagedEmfExporter assembly built against the assembly versions in the package.


 

User 74b72c099e

03-10-2011 13:22:10

Amazing! It seems to work :) My WPF application doesn't crash anymore when executing the copy commands from within the application. However, it still crashes if I attempt to copy a structure in the MarvinSketchEditor opened through my application. Is there a chance that this could be fixed in a similar manner?

ChemAxon eb65a25631

03-10-2011 13:28:36

That's good news! :)


Is your MarvinSketchEditor using the patched or the old assemblies?


 

User 74b72c099e

03-10-2011 13:38:28

Since I haven't changed the ChemAxon.NET.Windows.Forms assembly it might still be referencing the old IKVM-assemblies, I guess?

ChemAxon eb65a25631

03-10-2011 15:03:36

Yep, you are right indeed.

ChemAxon eb65a25631

03-10-2011 15:05:17

So, as it seems to fix the issue, I plan to include this fix in the next release.

User 74b72c099e

04-10-2011 06:59:43

That sounds great! Do you have any kind of estimate on when this could be released?

ChemAxon bd13b5bd77

05-10-2011 06:58:49

We still have a suspecion that the issue is related to the clipboard handling between Marvin and OLE,


and this causes a side effect in our component. If we cannot find the real issue for the time being this can be a workaround. But if we want to apply this workaround in the releases it means that we have to massively change our installation strategy, how we support the parallel versions , and so on ...


So we have to consider this again, which means that you can get a script arranging the direct installation of the managed component.


 

ChemAxon eb65a25631

11-10-2011 08:58:12

Hi Mikkel,


 


Could you do me another test?


Please change the used runtime from 4.0 to 3.5, and re-run the tests.


Thanks,


András