Skip to content

Commit 6c9ced7

Browse files
committed
Prevent an access violation. The reproduction step for the access violation in Access is as follows:
* With an existing Access file containing a VBA project, decompile, compile, and compact & repair the Access file. * Open Access, load the Access file, open VBIDE * As soon as Rubberduck finishes loading and the parse button is available, hit it This usually will yield an access violation when executing IVBEProject.CompileProject(). To avoid this from happening, we need to call IVBEProject.Placeholder3(). That seems to prevent the access violation. Using a known method such as GetCompilationArgs() does not seem to work in preventing the access violation. Nor does pumping the message pump using UiDispatcher.FlushMessageQueue() nor ComMessagePumper.PumpMessage(). Placeholder3() seems to have no permanent side effect.
1 parent c853e05 commit 6c9ced7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Rubberduck.VBEEditor/ComManagement/TypeLibs/TypeLibVBEExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Runtime.InteropServices;
66
using System.Runtime.InteropServices.ComTypes;
7+
using NLog;
78
using Rubberduck.VBEditor.ComManagement.TypeLibs.Abstract;
89
using Rubberduck.VBEditor.ComManagement.TypeLibs.Unmanaged;
910

@@ -66,6 +67,7 @@ internal class TypeLibVBEExtensions : ITypeLibVBEExtensions
6667
{
6768
private readonly string _name;
6869
private readonly IVBEProject _target_IVBEProject;
70+
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
6971

7072
public ITypeLibReferenceCollection VBEReferences { get; }
7173

@@ -87,6 +89,24 @@ public bool CompileProject()
8789
{
8890
try
8991
{
92+
// Prevent a potential access violation by first calling the Placeholder3().
93+
// Under certain conditions where the VBA project is fully compiled, clicking parse
94+
// as soon as Rubberduck has loaded into VBIDE can cause an access violation when
95+
// accessing CompileProject(). We've tried using ComMessagePumper and
96+
// UiDispatcher.FlushMessageQueue but neither helped in preventing the
97+
// access violation. We at least know that Placeholder3() does not save the unsaved
98+
// change so hopefully there should be no permanent side effects from invoking the
99+
// Placeholder3().
100+
try
101+
{
102+
_target_IVBEProject.Placeholder3();
103+
}
104+
catch(Exception ex)
105+
{
106+
Logger.Info(ex, $"Cannot compile the VBA project '{_name}' because there may be a potential access violation.");
107+
return false;
108+
}
109+
90110
_target_IVBEProject.CompileProject();
91111
return true;
92112
}

0 commit comments

Comments
 (0)