【发布时间】:2010-06-09 17:12:11
【问题描述】:
我正在封装一个读取自定义二进制数据文件并使数据可用于 .net/c# 类的类
然而,在代码下面几行,我开始收到内存访问冲突错误,我认为这是由于 GC 移动内存造成的,该类是托管的
这是 C# 中的调用代码 - 阅读器是混合代码(旧的非托管代码上的托管包装方法)
if ( ! reader.OpenFile(...) )
return ;
foreach(string fieldName in fields)
{
int colIndex = reader.GetColIndex( fieldName );
int colType = reader.GetColType( colIndex ); // error is raised here on 2nd iteration
}
for ( int r = 0 ; r < reader.NumFields(); r++ )
{
foreach(string fieldName in fields)
{
int colIndex = reader.GetColIndex( fieldName );
int colType = reader.GetColType( colIndex ); // error is raised here on 2nd iteration
switch ( colType )
{
case 0 : // INT
processField( r, fieldName, reader.GetInt(r,colIndex) );
break ;
....
}
}
}
....
Reader 有一个旧的非托管类实例引用,它将二进制数据保存在内存中 AND 它是指针类型,因为托管类不能拥有非托管类型
我查看了interior_ptr、pin_ptr,但它们给出了错误c3160 cannot be in a managed class
任何解决方法?顺便说一句,这是我很长一段时间以来的第一个 C++ 程序!
更新: 更新了Q,再次上面是调用代码和阅读器混合(托管+旧的非托管代码)
& 是的,参数都是有效的
【问题讨论】:
-
是 C++ 的读者吗?上面的代码看起来更像是带有 foreach 而不是 for each 的 C#。
-
reader是什么类型?它是托管类型吗?colType的值是多少?它是一个有效值吗? -
foreach是 C# 关键字还是您使用的是boost库? AFAIK,C++ 没有foreach关键字,这意味着你不是用 C++ 编写的! -
@Thomas 新的 C++/CLI 具有“for each(.. in ..)”blogs.msdn.com/b/arich/archive/2004/09/08/227139.aspx
-
错误所在的位置,不在此代码中。用 C++/CLI 代码破坏堆并不难。
标签: visual-c++ visual-studio-2008 garbage-collection c++-cli