Visio Guy

Visio Discussions => Visio Bugs (er...Issues) => Topic started by: borisu on December 28, 2008, 03:50:06 AM

Title: AccessViolation while setting src property
Post by: borisu on December 28, 2008, 03:50:06 AM
I am using Visio OCX (SDK 2007) control in C# application. While trying to load document and putting src property in .NET C# application I get AccessViolation exception.

The code is as simple as this. It is being executed in "open" button handler routine.

visioControl.Src = @"C:\Documents and Settings\Borisu\Desktop\Drawing1.vsd";

I've googled it but didn't come to any conclusion.

Please help.



See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Microsoft.Office.Interop.VisOcx.DrawingControlClass.set_Src(String pVal)
   at AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl.set_Src(String value)
   at callfloweditor.MainForm.visioOpen() in C:\SOLUTIONS\altalena\callfloweditor\MainForm.cs:line 135
   at callfloweditor.MainForm.openToolStripButton_Click(Object sender, EventArgs e) in C:\SOLUTIONS\altalena\callfloweditor\MainForm.cs:line 86
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
callfloweditor
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/SOLUTIONS/altalena/callfloweditor/bin/Debug/callfloweditor.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
Microsoft.Office.Interop.Visio
    Assembly Version: 12.0.0.0
    Win32 Version: 12.0.4518.1014
    CodeBase: file:///C:/WINDOWS/assembly/GAC/Microsoft.Office.Interop.Visio/12.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Visio.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
AxInterop.Microsoft.Office.Interop.VisOcx
    Assembly Version: 12.0.0.0
    Win32 Version: 12.0.0.0
    CodeBase: file:///C:/SOLUTIONS/altalena/callfloweditor/bin/Debug/AxInterop.Microsoft.Office.Interop.VisOcx.DLL
----------------------------------------
Microsoft.Office.Interop.VisOcx
    Assembly Version: 12.0.0.0
    Win32 Version: 12.0.4518.1014
    CodeBase: file:///C:/WINDOWS/assembly/GAC/Microsoft.Office.Interop.VisOcx/12.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.VisOcx.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
Title: Re: AccessViolation while setting src property
Post by: Paul Herber on December 28, 2008, 05:00:30 AM
What code comes before this?
How is 'visioControl' defined and created?

Title: Re: AccessViolation while setting src property
Post by: borisu on December 28, 2008, 10:36:31 AM
The control itself is dropped from tool box.

I am not sure which property you are interested in. SRC property is empty.


Then some custom stencil is open like this

Code
GetDocument().PaperSize = VisPaperSizes.visPaperSizeA4;

string currDirectory = System.Environment.CurrentDirectory;

Document altStencil =
 GetApplication().Documents.OpenEx(
 currDirectory + @"\Altalena.vss",
 (short)VisOpenSaveArgs.visOpenDocked);

Then user presses toolbar button and src property is changed.
Title: Re: AccessViolation while setting src property
Post by: borisu on December 28, 2008, 10:38:17 AM
visioControl is

Code
private AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl visioControl;
Title: Re: AccessViolation while setting src property
Post by: borisu on December 28, 2008, 10:53:24 AM
If that helps it works when form is created. But if src property is changed in toolbar handler it causes exception.
Title: Re: AccessViolation while setting src property
Post by: Paul Herber on December 28, 2008, 01:49:37 PM
Ok, but where is visioControl set? You've defined it and set its .src property but is it set?
Title: Re: AccessViolation while setting src property
Post by: borisu on December 28, 2008, 03:12:48 PM
I am not sure I am answering your question but the control is initialized in automatically generated InitializeComponents in form ctor, function like this
Code
this.visioControl.Dock = System.Windows.Forms.DockStyle.Fill;
            this.visioControl.Enabled = true;
            this.visioControl.Location = new System.Drawing.Point(0, 0);
            this.visioControl.Name = "visioControl";
            this.visioControl.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("visioControl.OcxState")));
            this.visioControl.Size = new System.Drawing.Size(785, 559);
            this.visioControl.TabIndex = 3;

I am basically trying to use it as described in this link - http://msdn.microsoft.com/en-us/library/aa168480.aspx (http://msdn.microsoft.com/en-us/library/aa168480.aspx)

Is it correct that I cannot use SRC property after the form is intialized???

May be, as it appears in the link, "Dynamically destroy and reinitialize the Visio drawing control in the document. " is the way to go?
Title: Re: AccessViolation while setting src property
Post by: Paul Herber on December 29, 2008, 07:23:59 AM
visioControl.Src = @"C:\Documents and Settings\Borisu\Desktop\Drawing1.vsd";

Try
Code
visioControl.Src = @"C:\\Documents and Settings\\Borisu\\Desktop\\Drawing1.vsd";

Title: Re: AccessViolation while setting src property
Post by: borisu on December 29, 2008, 07:31:06 AM
It is beyond my understanding ??? it is somehow connected to calling  EventList properties when adding advices or even referring to the class itself.

private void setAddAdvise()
        {

          EventList applicationEvents = eventApplication.EventList;
         EventList documentEvents = eventDocument.EventList;

            return;
}

It doesn't matter if add advise or not after calling the above lines, (or even initiating them to null!) the exception is thrown

WTF?

Title: Re: AccessViolation while setting src property
Post by: Visio Guy on December 29, 2008, 08:57:48 AM
I wonder if you are listening to Visio events that don't make sense when there is no drawing. When you reset Src, perhaps Visio is "empty" or null for a brief moment, or perhaps your events get whacked somehow.

When you set Src, maybe you should kill and re-build your event-listening stuff?

Just a quick guess....
Title: Re: AccessViolation while setting src property
Post by: borisu on December 29, 2008, 11:16:00 AM
I can reproduce the same error with visio SDK Office Plan example it happens if I open the saved diagram twice (without changing anything)

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Microsoft.Office.Interop.VisOcx.DrawingControlClass.set_Src(String pVal)
   at AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl.set_Src(String value)
   at Microsoft.Samples.Visio.OfficePlan.CSharp.OfficePlanSampleForm.onMenuFileOpenClicked(Object sender, EventArgs e) in C:\Program Files\Microsoft Office\Office12\VisSDK\Samples\Office Plan\CSharp\frmOfficePlanSample.cs:line 1006
   at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.MenuItem.MenuItemData.Execute()
   at System.Windows.Forms.Command.Invoke()
   at System.Windows.Forms.Command.DispatchID(Int32 id)
   at System.Windows.Forms.Control.WmCommand(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
OfficePlanSample
    Assembly Version: 1.0.3285.32861
    Win32 Version: 1.0.3285.32861
    CodeBase: file:///C:/Program%20Files/Microsoft%20Office/Office12/VisSDK/Samples/Office%20Plan/CSharp/Visual%20Studio%2080/bin/debug/OfficePlanSample.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Data
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
AxInterop.Microsoft.Office.Interop.VisOcx
    Assembly Version: 12.0.0.0
    Win32 Version: 12.0.0.0
    CodeBase: file:///C:/Program%20Files/Microsoft%20Office/Office12/VisSDK/Samples/Office%20Plan/CSharp/Visual%20Studio%2080/bin/debug/AxInterop.Microsoft.Office.Interop.VisOcx.DLL
----------------------------------------
Microsoft.Office.Interop.Visio
    Assembly Version: 12.0.0.0
    Win32 Version: 12.0.4518.1014
    CodeBase: file:///C:/WINDOWS/assembly/GAC/Microsoft.Office.Interop.Visio/12.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Visio.dll
----------------------------------------
Microsoft.Office.Interop.VisOcx
    Assembly Version: 12.0.0.0
    Win32 Version: 12.0.4518.1014
    CodeBase: file:///C:/WINDOWS/assembly/GAC/Microsoft.Office.Interop.VisOcx/12.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.VisOcx.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.


Title: Re: AccessViolation while setting src property
Post by: Visio Guy on December 29, 2008, 01:20:59 PM
Can you put a Visio control on a form, then successfully set the SRC property?

Start there, then slowly add functionality. You ought to try and isolate the bug.

I know I've had weird behavior with various debug settings, that I finally just switched off, but I can't remember what they are, nor did I really understand what they were for at the time.
Title: Re: AccessViolation while setting src property
Post by: borisu on December 29, 2008, 01:43:10 PM
In one of the forums I saw that disabling "suppress JIT ..." in Tools->Options->C# Debugging may help but it doesn't. I have already isolated the problem, and posted it before. It is very weird and not very helpful. I still have to add advices at some moment in time. I do believe that is has to do something with events race condition because when do "slow" debugging it is not reproducible. Just to mention it is not only SRC property which may cause this exception but many others.

The problem is  reproducible with Visio Sample - not in my code. And I succeeded to do it with two different OS (XP SP2 .NET2 and 2003  .NET1).

I am confused by the fact that it is such a basic functionality. How can it possibly be not working?

By the way can someone advise how to disable events and reset all event/advises functionality? It may help before setting SRC property.
Title: Re: AccessViolation while setting src property
Post by: Visio Guy on December 29, 2008, 05:01:32 PM
For AddAdvise events, I think you remove them, something like this:

doc_or_shape_or_page_or_app.EventList.Item( something ).Delete

I would imagine that you have event objects defined, so you should only delete the events you are listening for, not all of them.
Title: Re: AccessViolation while setting src property
Post by: Visio Guy on December 29, 2008, 05:12:18 PM
Ok, I just tried the SDK sample and it gives me the same problem when I try to open a new file.

The filePath is valid, and the file does exist...this is really weird!
Title: Re: AccessViolation while setting src property
Post by: Visio Guy on December 29, 2008, 05:37:19 PM
So as a test (using the SDK's Office Plan sample), I added another Visio control and a button to the form. When I click the button, it loads a hard-coded Visio file into the new control.

This works fine, so I guess that the event sinks are causing problems with the Office Plan's Visio control.

However, when I close the application, I get a LoaderLock error, and the application doesn't shut down properly.

"LoaderLock Detected:

Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang."

I get this error regardless of whether I do anything with the new control, and it is still there when I remove the new button and new Visio control. So I wonder if the LoaderLock problem was there before I started messing around.

By the way, I've seen the LoaderLock problem before, and was able to get rid of it by unchecking something under Tools > Options > Debugging, but I don't remember what it was, or whether it was a good idea to uncheck it.
Title: Re: AccessViolation while setting src property
Post by: borisu on December 30, 2008, 05:11:34 AM
try the following in SDK sample

private void setAddAdvise() {

         // The Sink and TargetArgs values aren't needed.
         const string sink = "";
         const string targetArgs = "";

         Event newEvent = null;
return; // << ---------- THIS WILL WORK (YOU WILL BE ABLE TO LOAD FILE)

         EventList applicationEvents = eventApplication.EventList;
         EventList documentEvents = eventDocument.EventList;

            return; // <--- THIS WON'T WORK (YOU WON'T BE ABLE TO LOAD FILE)

}

It seems like only referencing to EventList breaks the visio. It does seem like "suppress JIT optimizarion" problem but it doesn't help. :(
Title: Re: AccessViolation while setting src property
Post by: borisu on December 30, 2008, 06:02:47 AM
May be you meant disabling LeaderLock exception like explained here.

http://www.tech-archive.net/Archive/Visio/microsoft.public.visio.developer/2006-07/msg00038.html

Still doesn't help
Title: Re: AccessViolation while setting src property
Post by: borisu on December 30, 2008, 06:08:54 AM
 ;D ;D ;D

Good news!!!!

GC.Collect();
GC.WaitForPendingFinalizers();

before putting SRC property solves the problem !!!
Title: Re: AccessViolation while setting src property
Post by: Visio Guy on December 30, 2008, 11:26:20 AM
Nice job, borisu!

I had a feeling that the visControl was somehow half dead, half alive, but I didn't know enough technical nitty-gritty to do anything about it (you won't find me looking at MSIL code or anything...)
Title: Re: AccessViolation while setting src property
Post by: borisu on December 30, 2008, 01:32:03 PM
Hope this will be useful for somebody.
Thanks a lot for the help. This is truly great resource for visio guys.
Title: Re: AccessViolation while setting src property
Post by: borisu on December 30, 2008, 02:02:32 PM
BTW I think it is only part of much bigger problem. Seems like Viiso C# API is not built well for situations where destructing objects takes part in different thread and in undetermined time. It is not only SRC which caused problems but other API's also (Close for example).
Title: Re: AccessViolation while setting src property
Post by: IRDC on February 09, 2009, 05:11:01 AM
Hi, we encountered a similar problem: Our application won't start on Windows XP (runs fine on Vista) and crashes on the second Visio command (or the third, if we comment out the second):
1. _axVisioDrawingControl.HostID = VisioHostId;
2. _axVisioDrawingControl.Window.Application.Settings.EnableAutoConnect = false;
3. _axVisioDrawingControl.Window.Application.Settings.ZoomOnRoll = true;
with an Access Violation Exception (System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.)
Invoking the Garbage Collector didn't help. Any ideas?  :-\
Title: Re: AccessViolation while setting src property
Post by: kg_amat on October 20, 2014, 11:02:09 AM
I have run into the same problem with this control. I was able to get around it by opening and re-saving the document in Visio 2007. Good luck.