BIM Coordinator Program (INT) April 22, 2024

Find the next step in your career as a Graphisoft Certified BIM Coordinator!

Archicad C++ API
About Archicad add-on development using the C++ API.

Archicad crashes sometimes when calling API function

Anonymous
Not applicable
Hi,

in our team, we develop an Archicad plugin which uses the Archicad API. Here I encountered the following problem:
When I call the API function ACAPI_Attribute_Modify with a MaterialType attribute shortly before or after I click somewhere on the UI, Archicad crashes.
The values of the material attribute seem to be OK. Calling ACAPI_Attribute_Get immediately before does not cause any problems. Do you have
an idea of what I could do not to make it crash?

Thanks in advance.
7 REPLIES 7
Ralph Wessel
Mentor
Would it be possible to post some of the code around the call to ACAPI_Attribute_Modify? I haven't seen a problem with it before – perhaps there is a bug in the code?
Ralph Wessel BArch
Anonymous
Not applicable
Here is a code example I am able to reconstruct the problem with:
GSErrCode makeRed(int id)
	{
		API_Attribute attrib;
		BNClear(attrib);

		attrib.header.typeID = API_MaterialID;
		attrib.header.index = API_AttributeIndex(id);
		if (auto err = ACAPI_Attribute_Get(&attrib); err != 0)
		{
			return err;
		}

		attrib.material.emissionAtt = 0;
		attrib.material.emissionRGB = API_RGBColor{0, 0, 0};
		attrib.material.mtype = APIMater_GeneralID;
		attrib.material.specularPc = 74;
		attrib.material.transpAtt = 0;
		attrib.material.transpPc = 0;
		attrib.material.surfaceRGB = API_RGBColor{ 1, 0, 0 };
		IO::Location loc;
		API_Texture texture;
		BNZeroMemory(&texture, sizeof(API_Texture));
		attrib.material.texture = texture;

		return ACAPI_Attribute_Modify(&attrib, NULL);
	}
I run this code and click somewhere in the UI very fast after I started it.
Ralph Wessel
Mentor
Could you also tell me:
- Which version of Archicad you're targeting
- On what platform
- With which compiler and version
- And (if using VS) which toolset you're using, or (if Xcode) which C++ dialect you are using, e.g. C++14

I only noticed a couple if unusual things in your code, but they shouldn't cause a crash:
1) This line: IO::Location loc;
The variable "loc" is declared but never used for anything

2) These lines:
API_Texture texture;
BNZeroMemory(&texture, sizeof(API_Texture));
attrib.material.texture = texture;
…could be just:
BNZeroMemory(&attrib.material.texture, sizeof(API_Texture));
Ralph Wessel BArch
Anonymous
Not applicable
Hello Ralph,

I tried it with Archicad 22, 23, and 24 on Windows. I compile with platform toolset Visual Studio 2019 (v142), C++20

These code findings are due to the fact that I removed some code from my example (of course made sure it still crashes).
Ralph Wessel
Mentor
eziebarth wrote:
I tried it with Archicad 22, 23, and 24 on Windows. I compile with platform toolset Visual Studio 2019 (v142), C++20
These code findings are due to the fact that I removed some code from my example (of course made sure it still crashes).
I suspect the cause of the problem may be due to the VS toolset you're using. It's important to observe the system requirements for each API version, e.g.: http://archicadapi.graphisoft.com/documentation/system-requirements

For AC22 you need to use the 2015 toolset (v140). For AC23 and 24 you need to use the 2017 toolset (v141). You can do this with VS 2019, but just make sure each target has the correct toolset selected (that's what we do). I would also recommend not exceeding C++14 at the moment – I don't know if this is critical, but GS does this in the API examples.
Ralph Wessel BArch
Anonymous
Not applicable
Hello Ralph,

Thanks for your advice. I tried it with AC 24 and 2017 toolset (v141). Unfortunately, the crashes still happen. I can try if I can reproduce the crash by using one of the code examples which are delivered with the dev kit. That will take me some time. Would that be helpful?
Ralph Wessel
Mentor
It is sometimes difficult to pinpoint the cause of a crash. The API has bugs, but it is rare for this to be the cause and it can require a lot of effort to prove it.

I recommend trying to narrow the code to do as little as possible while still causing a crash. If you find that the crashing stops after a change, that could provide a clue to the source of the problem. It will be very useful if you can reproduce the crash using an API example. If the fault is narrowed to a specific function, forward it to the API support team and they will help to resolve it (either confirming a bug or providing guidance to avoid the problem).

There are many approaches to debugging. A tool that performs bounds checking on all memory accesses could trap memory-handling errors. This is the worst kind of problem because a memory-handling fault does not always cause an immediate crash. The crash (or other odd behaviour) might follow much later.
Ralph Wessel BArch
Learn and get certified!