By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,590 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Access violation in MSCORWKS.DLL

P: n/a
I have created an event sink in my ATL COM project. The event sink receives
events from a C# component. There is no problem with receiving events but
when my COM object is released I get an access violation - (MSCORWKS.DLL):
0xC0000005: Access Violation.

At first I thought it could be due to my C++ code but after quite a lot of
investigation I don't think there is nothing wrong with it. The access
violation occurs after the destructor of my COM object. It seems to be
coming
from the .NET garbage collector. Could it be that the GC is trying to
delete memory twice? How do I fix this problem? The events are being sent
from the TransferModule.

I'm very new to C#. Should I implment a dispose method?

Here's the code for my Transfer Module:

using System;
using System.Runtime.InteropServices;
using ATWSConsumer.TransferServer;
using System.IO;
using System.Windows.Forms;

namespace ATWSConsumer
{
public delegate void UpdateDelegate(int percentage);

[Guid("2278B7FD-3DD8-42cc-93D7-824C2FB32EA9")]
public interface ITransferModule
{
int TransferBlockSize {get; set;}
ConnectionModule Connection {get; set;}

bool Discover();
}

[Guid("2E638A47-161F-4b6c-9BBC-AD4AC2EC53FA"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
public interface ITransferModuleEvent
{
[DispId(1)] void UpdateProgress(int percentage);
}

[Guid("40A69B6B-7F46-4929-BEA5-CFCBB45D5443"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ITransferModuleEvent))]
public class TransferModule : ITransferModule
{
public event UpdateDelegate UpdateProgress;

public ConnectionModule m_oConnection;
public string m_sError;
public int m_nTBSize;

public ConnectionModule Connection
{
get{return m_oConnection;} set{m_oConnection = value;}
}

public string GetLastError()
{
return m_sError;
}

public int TransferBlockSize
{
get {return m_nTBSize;} set {m_nTBSize = value;}
}

private TransferService GetTransferService()
{
if ( m_oConnection == null || m_oConnection.m_serviceCache == null )
return null;
ServiceInfo si = m_oConnection.m_serviceCache.GetServiceByType
(ServiceInfo.ServiceType.TransferService);

if (si == null)
{
m_oConnection.SetErrorString
(m_oConnection.m_serviceCache.GetLastError());
return null;
}

TransferService transferService = new TransferService ();
transferService.Credentials =
System.Net.CredentialCache.DefaultCredentials; // to resolve security
problems with ASP by Yuriy E. Zatuchnyy at 23 Sep 2004
transferService.Url = si.m_sServiceAddress;

return transferService;
}

public TransferModule()
{
// default transfer block size (4k)
m_nTBSize = 4096;
}

public TransferModule(ConnectionModule connectionModule)
{
m_oConnection = connectionModule;
// default transfer block size (4k)
m_nTBSize = 4096;
}

public bool Discover()
{
TransferService transferService = GetTransferService();
if (transferService == null)
return false;

return true;
}

public string UploadFile(string sFilePath)
{
int nBytesRead = 0;
byte[] byteBuffer;
string sUploadToken = "";
FileStream fileStream = null;
long lTotalBytesRead = 0;

TransferService transferService = GetTransferService();
if (transferService==null)
return "";

// get the upload token to allow an upload
WSResult wsResult = transferService.RequestUploadToken
(/*m_oConnection.m_strSessionKey,*/0); // by Yuriy E. Zatuchnyy at 28 July
2004
if (!wsResult.Success)
{
m_sError = wsResult.Description.InnerText;
return "";
}

sUploadToken = wsResult.Description.InnerText;

try
{
fileStream = new FileStream (sFilePath, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
}
catch (Exception E)
{
m_sError = E.Message;
CleanupUpload(sUploadToken);
return "";
}

long lFileSize = fileStream.Length;
byteBuffer = new byte[m_nTBSize];
nBytesRead = fileStream.Read (byteBuffer,0,m_nTBSize);
while (nBytesRead > 0)
{
wsResult = transferService.Upload
(sUploadToken,nBytesRead,byteBuffer);
if (!wsResult.Success)
{
m_sError = wsResult.Description.InnerText;
fileStream.Close ();
CleanupUpload(sUploadToken);

return "";
}
lTotalBytesRead += nBytesRead;
if (UpdateProgress!=null)
{
UpdateProgress((int)((lTotalBytesRead * 100) / lFileSize));
}
nBytesRead = fileStream.Read (byteBuffer,0,m_nTBSize);
}

fileStream.Close ();
return sUploadToken;
}

public bool CleanupUpload(string sToken)
{
TransferService transferService = GetTransferService();
if (transferService == null)
return false;

WSResult wsResult = transferService.UploadComplete(sToken);

if (!wsResult.Success)
{
m_sError = wsResult.Description.InnerText;
return false;
}

return true;
}

public bool DownloadFile(string sFileLocation, string sToken, long
lFileSize)
{
long lChunkNumber = 0;
long lBytesReceived = 0;
FileStream fileStream = null;
TransferService transferService = GetTransferService();
if (transferService == null)
return false;

try
{
fileStream = new FileStream (sFileLocation, FileMode.Create);
}
catch (Exception E)
{
m_sError = E.Message;
return false;
}

TransferServer.BinaryResult binResult =
transferService.Download(sToken, lChunkNumber++, m_nTBSize);
while (binResult.lBytesRead > 0)
{
if (!binResult.Success)
{
m_sError = binResult.Description.InnerText;
fileStream.Close ();
return false;
}
lBytesReceived += binResult.lBytesRead;
if (UpdateProgress!=null)
{
UpdateProgress((int)((lBytesReceived * 100) / lFileSize));
}
fileStream.Write (binResult.Content,0,(int)binResult.lBytesRead);
binResult = transferService.Download(sToken, lChunkNumber++,
m_nTBSize);
}

fileStream.Close ();

return true;
}
}
}
Nov 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.