C++ and Grasshopper passing native variables from one component to another

Hi,

I am wrapping some native C++ library variables to C#.
Before I had one C# component where I had everything working fine calling CLI wrapped variables and solver. But now I want to distribute my code into multiple components.

The thing is that I disposed some variables by placing them into brackets so that jointElementMatsSet and ele_link_tnl2 are deleted. I could also call Dispose method.
The small bit of code is here:

{
  var jointElementMatsSet = new OpenSees.Materials.Uniaxials.UniaxialMaterialWrapper[] { Axial_perimeter_long_leftMat, Shear_1_perimeter_long_leftMat, rigidmattagMat, FreemattagMat, FreemattagMat, FreemattagMat };
  var ele_link_tnl2 = new OpenSees.Elements.TwoNodeLinkWrapper(2, 3, 16, 1601, dirIds2, jointElementMatsSet, new OpenSees.VectorWrapper(new double[] { 1, 0, 0 }), new OpenSees.VectorWrapper(new double[] { 0, 0, 1 }), new OpenSees.VectorWrapper(0), new OpenSees.VectorWrapper(0), 0, 0);
  theDomain.AddElement(ele_link_tnl2);
}

But now since I want to distribute different classes into different components I have a question:
Can I create one variable in one component and dispose it in another one?
More simply: when two components are connected and second component receives the variable, is it duplicated or it points to the same memory location?

I don’t see why not. Objects that inherit from IDisposable are usually holding onto a C++ pointer via an IntPtr object. So it’s just a matter of disposing, which involved deleting the C++ object.

– Dale

Does it mean that in grasshopper when you pass data from one component to another parameters are not duplicated and are referenced to the same address in memory?

This is dependent on what you are passing and on what components are involved. But if this is all your own stuff, then the reference (e.g. pointer) is passed.

– Dale

1 Like

@Petras_Vestartas, did you solve the issue? I am starting this journey of wrapping native C library variables to C#.

I am following the example shown here. In addition, I am looking for a simple example for Grasshopper. By any chance do you have a simple example that can help me to get started?

Thanks,
Rolando

I think pinvoke of Dale’s example is the best one.
I use the same. You create c++ Project with pinvoke decorations and c# projects and that is it. Grasshopper is c# .

Advice pause using Rhino before you learn that and call c++ unsafe methods in c# simple console application. You won’t need to wait until Rhino loads since debugging for native code will take much more time. Also, start from simple hello world and the simple variables and then arrays. You need to be quite tidy and instead or copy paste, understand what you do at each line, so just follow Dale’s example and slowly adapt to your case.

2 Likes