Read large file into byte arrays and then use ref

D

devgrt

I am having what I think are some memory problems after a file read.
Is there any problem with reading a file into a byte array that is around
300KB and then passing that array by ref to another function?:

System.IO.FileStream inFile = new System.IO.FileStream(aName,
System.IO.FileMode.Open, System.IO.FileAccess.Read);
long len = inFile.Length;
byte [] binaryData = new Byte[(int)len];
long bytesRead = inFile.Read(binaryData, 0, (int)len);
inFile.Close();
hexstring r = HashFile(ref binaryData);

Thank you
 
N

Norman Rericha

D

devgrt

What is the error that you are seeing that is causing you grief?
The errors are are that a 3rd-party DLL that I load does not work
correctly -- so I was wondering if my file load code was fowling up memory.
It's odd because if my file load code loads a file <200KB the dlls work, if
200KB it fails. I assume the reason is the dll won't load. I tried using
LoadLibrary to preload the DLL at program starttime but that did not work
for this DLL (for other DLLs it does work though, preloading them and
keeping them loaded). The app uses CFCOM to wrap a large Map ActiveX object
so maybe that is the problem because if I comment out InitializeComponents
on the map form then the DLLs load fine everytime.
 
D

devgrt

You said:
I would pass the stream instead of a byte[].

I was wondering (just so I can understand more) what the benefit would be in
passing the stream instead of byte -- thanks!


Norman Rericha said:
What is the error that you are seeing that is causing you grief? You
should be able to do what you ask (although I would pass the stream
instead of a byte[]).

This link should give you more details on doing what you are asking...

http://msdn.microsoft.com/library/d...sref/html/vclrfpassingarraysusingrefoutpg.asp


--
I hope this helps

--
Norman Rericha
Senior Software Engineer
Applied Data Systems
www.applieddata.net
An ISO 9001:2000 Registered Company
Microsoft WEP Gold-level Member
 
Y

yoat42

Can you provide the declaraction of the function you are calling?
Is it part of a managed class library or a native DLL being called via
DllImport?
There are many benefits to passing a stream instead of a byte array but
since you have a 3rd party DLL it looks like you're stuck with what
you've got.
-MSenne
 
D

devgrt

I do not have a declaration for the DLL -- it is just a third-party dll that
I have a reference for.

From what you said about it being better to pass a stream do you think I
should change such that instead of loading a byte array and then calling my
func to passing the stream and then calling my func and then in my func fill
the byte array there:

Old way:

System.IO.FileStream inFile = new System.IO.FileStream(aName,
System.IO.FileMode.Open, System.IO.FileAccess.Read);
long len = inFile.Length;
byte [] binaryData = new Byte[(int)len];
long bytesRead = inFile.Read(binaryData, 0, (int)len);
inFile.Close();
hexstring r = HashFile(ref binaryData);

New way:

System.IO.FileStream inFile = new System.IO.FileStream(aName,
System.IO.FileMode.Open, System.IO.FileAccess.Read);
hexstring r = HashFile(ref inFile);

inFile.Close();

void HashFile(ref System.IO.FileStream inFile)
{
long len = inFile.Length;
byte [] binaryData = new Byte[(int)len];
long bytesRead = inFile.Read(binaryData, 0, (int)len);
//work with my byte [] here
....
return;
}

Thanks!
 
N

Norman Rericha

If you are using a 3rd party DLL, they you are going to have to provide
the data in the format required. As I thought about it, if your DLL
requires a byte[], then converting from stream to array really doesn't
buy you anything.

Try to P/Invoke your external DLL (assuming that it is unmanaged), as
you give more details it sounds like your problem is with the 3rd party
dll and that it might have interal limitations you are fighting with.


--
I hope this helps

--
Norman Rericha
Senior Software Engineer
Applied Data Systems
www.applieddata.net
An ISO 9001:2000 Registered Company
Microsoft WEP Gold-level Member
 
D

devgrt

The thing that is odd is that after we use a CFCOM wrapped ActiveX control
then no other DLLs will load after it. Below is a scenario that demonstrates
it:

-We cqan use functions from any DLL (ours, a third party, anyone)
-We use CFCOM to wrap a mapping ActiveX control
-We still can call funcs from any of our or any third party dlls
-We use the wrapped ActiveX control (call CFCOMs .Invoke)
-From here on we no longer can call functions from any of our or third party
dlls (even OpenNETCF crypto stuff no longer works!

Is it possible that the ActiveX control is somehow misbehavingand causing
mem problems???
The control loads a lot of map data.

The problem is intermitent unless we do any file IO then the problem is 100%
reporducable after that.

Thanks for your ideas and help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top