AccessViolation while setting src property

Started by borisu, December 28, 2008, 08:50:06 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

borisu

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.

Paul Herber

What code comes before this?
How is 'visioControl' defined and created?

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

borisu

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


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.

borisu

visioControl is


private AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl visioControl;

borisu

If that helps it works when form is created. But if src property is changed in toolbar handler it causes exception.

Paul Herber

Ok, but where is visioControl set? You've defined it and set its .src property but is it set?
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

borisu

#6
I am not sure I am answering your question but the control is initialized in automatically generated InitializeComponents in form ctor, function like this

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

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?

Paul Herber

Quote from: borisu on December 28, 2008, 08:50:06 AM
visioControl.Src = @"C:\Documents and Settings\Borisu\Desktop\Drawing1.vsd";

Try

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


Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

borisu

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?


Visio Guy

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....
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

borisu

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.



Visio Guy

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.
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

borisu

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.

Visio Guy

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.
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Visio Guy

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!
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010