Choose your top Archicad wishes!

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

Why this code of create element always crashing Archicad app?

Newbie
Participant

Is there anyone could help to fix below code to avoid crashing?.

 
=======

ACAPI_Element element;
element.header.guid = guid;
GSErrCode err = ACAPI_Element_Get(&element);
if (err == NoError)
{
API_Element newelement = {};
API_ElementMemo newmemo = {};
API_ElementMemo memo = {};
BNZeroMemory(&newelement, sizeof(API_Element));
BNZeroMemory(&newmemo, sizeof(API_ElementMemo));

newelement.header.type.typeID = element.header.type.typeID;
GSErrCode err = ACAPI_Element_GetDefaults(&newelement, &newmemo);
newelement.header.guid = element.header.guid;
newelement.header.floorInd = element.header.floorInd;
if (element.header.hasMemo && ACAPI_Element_GetMemo(element.header.guid, &memo, APIMemoMask_All) == NoError)
{
newmemo = memo;
}
err = ACAPI_CallUndoableCommand("Create element", [&]() -> GSErrCode {
return ACAPI_Element_Create(&newelement, &newmemo);
});
}
=============
2 REPLIES 2
Joel Buehler
Enthusiast

hi newbie, i did not try out your code but i compared it to the maze tutorial. here are my comments: 

ACAPI_Element element;
element.header.guid = guid; // from where does this element come from? do you want to copy an existing element? 
GSErrCode err = ACAPI_Element_Get(&element); // here you retrieve an existing element, right? 
if (err == NoError)
{
API_Element newelement = {};
API_ElementMemo newmemo = {};
API_ElementMemo memo = {};
BNZeroMemory(&newelement, sizeof(API_Element)); // waht ever you retrieved in the first place will get here deleted 
BNZeroMemory(&newmemo, sizeof(API_ElementMemo)); // same here. after that call you can be sure that the memory of that element is empty
 
newelement.header.type.typeID = element.header.type.typeID; // you are using an variable of a deleted objected. 
GSErrCode err = ACAPI_Element_GetDefaults(&newelement, &newmemo); // you cant retrieve anything of a deleted element. 
newelement.header.guid = element.header.guid;
newelement.header.floorInd = element.header.floorInd;
if (element.header.hasMemo && ACAPI_Element_GetMemo(element.header.guid, &memo, APIMemoMask_All) == NoError)
{
newmemo = memo;
}
err = ACAPI_CallUndoableCommand("Create element", [&]() -> GSErrCode {
return ACAPI_Element_Create(&newelement, &newmemo);
});
}

 

 

 

Hi Joel Buehler,

Referring your questions;

1. Yes, the guid from an existing element which I want to copy.

2.

BNZeroMemory(&newelement, sizeof(API_Element));

This is a newly declare element not element, not sure it should use or not.

BTW, I have try to take those 2 lines of BNZ... out but still  crashing. Any suggestion?.

 

Thanks in adv.