Modules for plugin classes are dangerous

Lets see if I can explain this, it took me all night to get grips of what is going on.

First of I have several times in the past removed Module name space from plugins because of issues that I have not been able to explain. And then on others things have seemed fine, but probably are not fine.

Lets consider this Module and Class here:

EinhugurMacOSBridge.NSImage

This is put on push button:

[i]Sub Action() Handles Action
Dim image as EinhugurMacOSBridge.NSImage

image = EinhugurMacOSBridge.NSImage.FromFileType(“txt”)
End Sub[/i]

We crash…

It took me long while to figure out why we are crashing there. Since I had other projects that seemed just like that using same class and no issue.

Crash happens because:
REALclassRef ref = REALGetClassRef(“EinhugurMacOSBridge.NSImage”); // Returns NULL
REALobject instance = REALnewInstanceWithClass(ref); // Returns NULL (because ref was NULL)

In same way this one will also return NULL:
REALobject instance = REALnewInstanceOfClass(&NSImageClass); // Returns NULL.

At this point I did not know what to think since I have same thing working in other projects using same class:

Few hours later I had nailed it…

If I add property to the Window1 in the Xojo project like: dummy as EinhugurMacOSBridge.NSImage

then situation becomes this:

REALclassRef ref = REALGetClassRef(“EinhugurMacOSBridge.NSImage”); // Returns valid reference
REALobject instance = REALnewInstanceWithClass(ref); // Returns valid object

REALobject instance = REALnewInstanceOfClass(&NSImageClass); // Returns valid object.

And of course finally removing the Module namespace then it works always without adding dummy property to the Window1 in the project.

is it possible that you’ve just forgot a “new”.
Would you please try it with

Dim image as new EinhugurMacOSBridge.NSImage

and tell if it works?

Its not a class you can new like that, the class only has Private constructors.

The static method here is creating the instance the FromFileType function.
And its internally in the plugin where the “New” fails since Xojo magically does not know the type when its in module unless a dummy property has been added to any window then suddenly Xojo knows the type create new instance from plugin.

Sub Action() Handles Action
Dim image as EinhugurMacOSBridge.NSImage

image = EinhugurMacOSBridge.NSImage.FromFileType(“txt”)
End Sub

This part here:
REALclassRef ref = REALGetClassRef(“EinhugurMacOSBridge.NSImage”); // Returns NULL
REALobject instance = REALnewInstanceWithClass(ref); // Returns NULL (because ref was NULL)

Is inside the plugin where the “New” fails

Added bug issue and 2 examples that can be run (one works the other does not)

(They only differ by dummy property that is not used on the Window)

59196 - Plugin classes in namespace modules cause issues

(and of course both examples work without crash if recompiling the plugin and removing the Module namespace from it)

strange when adding “new” your not working file works for me

think the point is that the line

 image = EinhugurMacOSBridge.NSImage.FromFileIcon(f)

is a shared method that returns an EinhugurMacOSBridge.NSImage so the “new” really should not be required

[quote=475501:@Norman Palardy]think the point is that the line

 image = EinhugurMacOSBridge.NSImage.FromFileIcon(f)

is a shared method that returns an EinhugurMacOSBridge.NSImage so the “new” really should not be required[/quote]

ja it breaks in that line without the “new” though with “new” the line before it doesn’t break.
But it breaks not when doing this:

dim iiii as new EinhugurMacOSBridge.NSImage
Dim image as EinhugurMacOSBridge.NSImage

image = EinhugurMacOSBridge.NSImage.FromFileIcon(f)
//image = EinhugurMacOSBridge.NSImage.FromFileType("txt")

so it is basically a placeholder or dummy what Björn did with his dummy window property.

The new does same as adding the dummy property. Lets the compiler know this type exist it seems.

It is some sort of compiler mess where it seems to have lost track of that this type exist until it is “touched” in hard enough way for the compiler to know it.

affirm, there’s a bug in xojo
or be gentle to your objects :wink:

I found a workaround that is not so bad. Basically all return types in the method signature in the plugin just need to return full namespace.