Note that there are some explanatory texts on larger screens.

plurals
  1. POSystem.NullReferenceException when using gcroot
    primarykey
    data
    text
    <p>Turns out this error had nothing to do with gcroot - I connected some constructors shoddily and lost the instantiated object on the way.</p> <p>For the record, all versions of the code below worked after this fix, but adding the simple Wrapper class allows you to use &amp; and * operators on the wrapped object, which gcroot forbids. I am using the version outlined in the third code block below.</p> <hr> <p>I am trying to use some measuring instruments via a .dll, but I am having problems with the implementation. Writing all the code into the main.cpp and using the object like this works fine:</p> <pre><code>#using "M3D_FP_USB.dll" int main(){ M3D_FP_USB ^obj = gcnew M3D_FP_USB; obj-&gt;Connect(); ... } </code></pre> <p>However, when I put it into a class and wrap it with gcroot, I get an "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of the object" on execution.</p> <pre><code>class User{ ... public: User(); gcroot&lt;M3D_FP_USB^&gt; obj; } User::User(){ obj = gcnew M3D_FP_USB; obj-&gt;Connect(); ... } </code></pre> <p>I suspect that the gcroot or it being called inside the unmanaged class might prevent the instruments from accessing the M3D_FP_USB object. I tried adding a managed Wrapper class, and give a handle to the Wrapper object to the User class. That didn't solve the problem, though.</p> <pre><code>class User{ ... public: gcroot&lt;Wrapper^&gt; wobj; } ref class Wrapper{ ... public: M3D_FP_USB^ iobj; ... } Wrapper::Wrapper(){ iobj = gcnew M3D_FP_USB; iobj-&gt;Connect(); ... } </code></pre> <p>The new code I have looks like this, but I couldn't test it with the instruments yet. I will update when I get the chance.</p> <pre><code>Wrapper::Wrapper(M3D_FP_USB^ obj){ iobj = obj; iobj-&gt;Connect(); ... } int main(){ M3D_FP_USB ^obj = gcnew M3D_FP_USB; Wrapper Ex(obj); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload