Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DllManipulatorScript is not the last GameObject calling OnDestroy when scene is being destroyed. #27

Open
zhmt opened this issue Jul 7, 2020 · 5 comments

Comments

@zhmt
Copy link

zhmt commented Jul 7, 2020

The order of OnDestroy is not guaranteed, because the order of script excution doesn't work when calling OnDestroy.

class OneObj : MonoBehavior {
  void OnDestroy() { NativeApi.test(); }
}

The code above will raise IndexOutofBoundException randomly when stopping game in editor.

I dont know how to solve this problem.

I am using a stupid solution:

  1. invoke DllManipulatorScript.Reinitialize in Awake(){} when game started.
  2. manually invoke DllManipulatorScript.Reset via menu item before building cpp dll.

Is there better way?

@zhmt
Copy link
Author

zhmt commented Jul 7, 2020

How about initializing dll when game started, and reset befor DllManipulatorScript's destructor is called?

If My NativeApi object hold an refercence of DllManipulatorScript , it will stop DllManipulatorScript being GCed before all my GameObjects are destoryed.

@zhmt
Copy link
Author

zhmt commented Jul 7, 2020

Something like this:

class DllManipulatorScript {
  void Awake() { Reinitialize(); }
  ~DllManipulatorScript () { Reset(); }
}

class MyNative {
  object dllScriptRef;
  Awake() { dllScriptRef = GameObject.Find("DllManipulatorScript"); } 

  [DllImport]
  static extern void test();
}

@mcpiroman
Copy link
Owner

The problem is, at least when I last tested it, destructor is not called in editor when you just stop the game (or maybe only when it's the selected object in the scene, something of that nature). But if it turns out I'm wrong about that and it's gonna work fine, then yeah, I can change that.

@zhmt
Copy link
Author

zhmt commented Jul 7, 2020

I tested, you are right.
It is pretty tough to clean up at right time.

@zhmt
Copy link
Author

zhmt commented Jul 7, 2020

Luckly, mannally dll unloading works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants