DrawViewportMeshes : where do I delete cached geometry?

I have a component that instanciates a custom class, and then caches some geometry to display.

Mesh [] m_meshes = null;
Curve[]  m_curves = null;
 
protected override void SolveInstance(IGH_DataAccess DA)
{
  // delete cache ??
  m_meshes = null;
  m_curves = null;
   
  object input = null;
  if (! DA.GetData(0, ref input)) return;

  var myClassInstance = new MyClass(input);
  m_meshes  = myClassInstance.GetPreviewMeshes();
  m_curves  = myClassInstance.GetPreviewCurves();
  DA.SetData(0, myClassInstance);
}

When I disconnect input wires on my component, SolveInstance is not called, so the cache is not cleared and the previous preview is still displayed.
The way I solved this for now was to set all input params to optional, so that SolveInstance is always called. But then I have to manually “re-mandatorize” all inputs, i.e. checking for null and throwing an informative exception for each one. Presumably there is a better way to do this!

Hi @capitaine_fred, sorry I missed your post, I think I was on holiday a month ago.

Quick tip before I dig into the actual question, you can encapsulate your code with triple ticks, makes it look properly formatted:

```
multiple lines;
of code;
here…
```

becomes:

multiple lines;
of code;
here...

I already made the change in the original post.

Two tips:

  1. Make your input parameter Optional=true, that way SolveInstance() will get called even when it’s empty.
  2. You can use the BeforeSolveInstance() and AfterSolveInstance() methods to perform caching/cleanup. It’s easier there because they’re only called once per solution.