Object reference problem

  • Thread starter Thread starter db_from_mn
  • Start date Start date
D

db_from_mn

I've got a very strange error.
I get an error message:
"Object reference not set to an instance of an object",
which I've seen many times before, for good reason. But
this time, I cannot find fault with my code.

Here's my function call:
eError = CUCanApi.eUCanGetBootFirmwareInfo( hDevice, out
fw, 1000 );

The problem appears to be with the argument hDevice,
which is an IntPtr type. I set it with another function
called hDUT(), which returns an IntPtr type.

If I check hDevice just before the call of
eUCanGetBootFirmwareInfo, it appears to have a valid
value.

If I replace the hDevice argument with "(IntPtr)0", then
the error message goes away, indicating that I've
resolved the object reference issue.
Any ideas on what the problem is?
 
db_from_mn said:
I've got a very strange error.
I get an error message:
"Object reference not set to an instance of an object",
which I've seen many times before, for good reason. But
this time, I cannot find fault with my code.

Here's my function call:
eError = CUCanApi.eUCanGetBootFirmwareInfo( hDevice, out
fw, 1000 );

The problem appears to be with the argument hDevice,
which is an IntPtr type. I set it with another function
called hDUT(), which returns an IntPtr type.

If I check hDevice just before the call of
eUCanGetBootFirmwareInfo, it appears to have a valid
value.

If I replace the hDevice argument with "(IntPtr)0", then
the error message goes away, indicating that I've
resolved the object reference issue.
Any ideas on what the problem is?

Hi db_from_mn!

Just an idea... have you yet tried to get the integer value of hDevice
and cast it to IntPtr when calling the function? Just like this:

eError = CUCanApi.eUCanGetBootFirmwareInfo( (IntPtr) intValueOfhDevice,
out fw, 1000 );

Best regards,

Michael

--
How to contact me
~~~~~~~~~~~~~~~~~
directly via mail: remove the "NOTUSEABLE" and the point after it and
reverse the characters
via my portal: go to
http://great.dynu.com/tools/contact.4s?lang=de&ref=usenet
 
Hello Michael,
Yes, I've tried all kinds of different casts with no success.

Dennis
 
db_from_mn said:
If I check hDevice just before the call of
eUCanGetBootFirmwareInfo, it appears to have a valid
value.

If I replace the hDevice argument with "(IntPtr)0", then
the error message goes away...

hDevice is not a valid pointer/handle ... could it be an error value rather
than a pointer? or could something have happened since setting hDevice such
that the hDevice object gets freed/released? If you set hDevice immediately
before the uCanGetBootFirmwareInfo call is there still a problem?

Brad Williams
 
Hello Brad,

Thanks for the attention.
I have isolated this problem, and learned quite a bit in the process!
The function call was to unmanaged code, implemented in a dll. I had
assumed that the exception was being thrown by the .NET framework,
complaining about the IntPtr parameter passed to the function. What was
really happening, was that deep in the unmanaged code, I had a pointer
error: I was trying to access a member of a data object with a pointer
that had not yet been assigned a value.
I learned two things:
1. Exceptions can be propogated up from unmanaged code to managed code.
2. How to debug across the managed-unmanaged boundary.


Thanks for the help,
Best Regards,
Dennis
 

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

Back
Top