我有一个控制台应用程序,它使用ComImport调用windows搜索。虽然这在调试中运行良好,但mode...the控制台应用程序在发布模式下崩溃。可能的问题是什么?
[ComImport]
[Guid("9DAA54E8-CD95-4107-8E7F-BA3F24732D95")]
[ClassInterface(ClassInterfaceType.None)]
[TypeLibType(TypeLibTypeFlags.FCanCreate)]
public class WordBreaker : IWordBreaker
{
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
public virtual extern bool Init([In] bool query, [In] uint maxTokenSize);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
public virtual extern void BreakText([In, MarshalAs(UnmanagedType.LPStruct)] TEXT_SOURCE textSource,
[In] IWordSink wordSink, [In] IPhraseSink phraseSink);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
public virtual extern void ComposePhrase([In, MarshalAs(UnmanagedType.LPWStr)] string noun, [In] uint nounLen,
[In, MarshalAs(UnmanagedType.LPWStr)] string modifier, [In] uint modifierLen,
[In] uint attachmentType, [Out] out IntPtr phrase, [In, Out] ref uint phraseLen);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
public virtual extern IntPtr GetLicenseToUse();
}访问WordBreaker.BreakText函数时,代码在发布模式下失败。
在我的代码中使用了它,如下所示
if (!string.IsNullOrWhiteSpace(text))
try
{
IWordBreaker breaker = new WordBreaker();
bool reqLicense = breaker.Init(query, 256);
if (reqLicense)
{
IntPtr lic = breaker.GetLicenseToUse();
string licText = Marshal.PtrToStringUni(lic);
}
TEXT_SOURCE source = new TEXT_SOURCE();
source.fillTextBuffer = FillTextBuffer;
source.buffer = text;
source.cur = 0;
source.end = (uint)(text.Length);
breaker.BreakText(source, new WordSink(result), null);
}
catch (Exception ex)
{
Console.Out.WriteLine(ex.ToString());
//log4net.LogManager.GetLogger(typeof(WindowsIntegration)).Error("BreakText", ex);
}
var resultWithoutNoise = NoiseWord.Remove(result);
return resultWithoutNoise;
}崩溃发生在breaker.BreakText中,在应用崩溃之前,BreakText函数被调用了很多次( 500到7000次)。
crashdump会显示以下关于异常信息的The thread tried to read from or write to a virtual address for which it does not have the appropriate access.注释:我没有在代码中使用任何线程。
发布于 2011-06-29 19:49:41
代码至少需要一个线程,这就是错误所指的线程。至于崩溃的类型,它本质上是一种访问冲突,要么是空指针/对象引用(在本机代码中),要么是缓冲区溢出。检查哪些值。字符串。长度,等等,你在它失败的时候传入。
发布于 2013-09-10 07:27:34
这看起来像是垃圾收集器将文本移动到另一个位置,因此记录在source.buffer中的地址不再有效。您还没有包含声明TEXT_SOURCE的源代码,所以我无法对此发表评论。但是BreakText的声明是不正确的。MarshalAs(UnmanagedType.LPStruct)不应该在那里使用。仅在一种特定情况下支持UnmanagedType.LPStruct :将System.Guid值类型视为具有额外间接级别的非托管GUID。参见http://blogs.msdn.com/b/adam_nathan/archive/2003/04/23/56635.aspx和How do I marshal a structure as a pointer to a structure?。
发布于 2011-06-29 19:43:06
如果你不能重现这个问题,而且只能在发行版中重现,我强烈建议你添加logging来缩小你的问题范围。
当你缩小了你的问题范围后,我会编辑你上面的帖子,包括关于崩溃的更详细的信息。
https://stackoverflow.com/questions/6519660
复制相似问题