Plugin not accepting non-static char arrays?

I’m trying to create a plugin to create an inspector properties drop-down menu from a char array.

I can do this with a static array. For example:

const char* charArray[] {
"a",
"b",
"c",
"d",
"e"
};
static REALproperty VectorProperties[] = {
	{"New Image", "VectorImage", "Integer", REALpropInvalidate, NULL, NULL,  0, nil, sizeof(charArray) / sizeof(charArray[0]), charArray}

Will give me a drop down menu containing the array values.

However trying to insert values into a globally defined non-static array inside the plugin constructer method crashes Xojo on load but is working in debugger. For example: (same properties method)

//inserting "0, 1, 2, 3, 4" into an array

const char* charArray[5];

static void VectorConstructor(REALcontrolInstance control)
{
	for (int i = 0; i < 5; i++) {
		string newNumString = "";
		const char* newNumChar = "";
		newNumString = to_string(i);
		newNumChar = newNumString.c_str();
		charArray[i] = newNumChar;
	}

Can Xojo/Xojo plugins not accept arrays that are non-static? Is there something i’m missing?

Thanks.

First it looks like you make a definition of the property with a list of enum labels. Looks fine.

But the code there is strange.
Since you use std::string there, the ptr you get from c_str() function is temporary. putting it in charArray[] won’t be good, since on access later it will point to released string.

Maybe you think again what you want to do?

Okay that’s understandable, but for example lets just make a very simple for loop to insert a char containing “abc” into the array like this:

const char* charArray[5];
const char* newChar = "abc";

static void VectorConstructor(REALcontrolInstance control) {
	for (int i = 0; i < 5; i++) {
		charArray[i] = newChar;
	}
}

This causes Xojo to crash on load. All variables are globally defined? So what is making this plugin fail?

I’m trying to create a drop-down menu property of a class containing file names from a specified folder.
The properties definition requires a const char* and the file names are returned as a stringstream hence the example of code before containing the data type conversions.
As you pointed out my current method won’t work, is there a better way of doing this conversion to work with what i’m trying to accomplish?

You would have to new some memory area for it with malloc or new operator and let it persist over the lifetime of the whole plugin.

Crash log?
Do you know what line crashes exactly?

Code compiles fine into dll file through visual studio, but no crash log as it’s failing when Xojo is loading in plugins. As far as i know Xojo doesn’t create crash logs during load time.

I suspect that Björn may be correct and i have been testing solutions with malloc memory allocation, but am yet to have a working project.

You say the plugin defines a drop-down menu in the inspector properties pane of the IDE. That is only possible with a static array. Usually this is being done for a REALcontrol. Any other way other than a static array will crash Xojo. Our plugins use these static arrays in many cases to ease the setup of a plugin for runtime purposes.

1 Like

That’s exactly the answer i was looking for, Alfred. Thank you very much

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.