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

[SOLVED] IMPORTANT! delete[] operator doesn't work

Anonymous
Not applicable
Hi

Why AC crashs when I call delete[] operator
My code separately works correctly

Please give me an advise.

Here is my add-on code
typedef void(*get_building_info) (const char *user_id, const char * token, Building *buildings[]);

	LPCWSTR dllName = helper::developer::getDllPath();
	HINSTANCE dynamicLib = LoadLibraryW(dllName);

	
	if (dynamicLib)
	{
		Building * building_data;
		get_building_info getdata = (get_building_info)GetProcAddress(dynamicLib, "get_building_info");
		getdata(this->userId, this->accessToken, &building_data);

		if (!building_data->is_empty)
		{
			//Filling the building combobox
			m_comboBuilding.DeleteItem (DG::PopUp::AllItems);
			for (int c = 0; c < (int)building_data->size; c++)
			{
				m_comboBuilding.AppendItem ();
				m_comboBuilding.SetItemText (DG::PopUp::BottomItem, GS::UniString::Printf("%s", building_data.name));
				m_comboBuilding.SetItemValue (DG::PopUp::BottomItem, (int)building_data.id);
			}
			delete[] building_data;
		}
	}
And here is the dll code
__declspec(dllexport) void get_building_info(const char * user_id, const char * token, Building *building_data[])
	{
		get_buildings_cpprest(user_id, token, *building_data).wait();

		if (!building_id.empty())
		{
			Building *b = new Building[building_id.size()];

			b->is_empty = false;
			b->size = building_id.size();

			for (int i = 0; i < building_id.size(); i++)
			{
				b.id = const_cast<char*>(building_id.c_str());
				b.name = const_cast<char*>(building_name.c_str());
			}
			*building_data = b;
		}
		else {
			
		}
  	}
6 REPLIES 6
Karl Ottenstein
Moderator
I might not be seeing it ... but where did you do a 'new' to allocate the building_data object instantiation?
One of the forum moderators
AC 27 USA and earlier   •   macOS Ventura 13.6.6, MacBook Pro M2 Max 12CPU/30GPU cores, 32GB
Oleg
Expert
I think global new and delete overriden by API.
May be ::delete[] building_data will work;

But actually it is poor DLL design.
You need do and export function from DLL like
release_building_info(Building *building_data[])
and free the memory.
Anonymous
Not applicable
Karl wrote:
I might not be seeing it ... but where did you do a 'new' to allocate the building_data object instantiation?
I allocate with new operator in my dll code because I have to determine the size of building structure before filling it
_declspec(dllexport) void get_building_info(const char * user_id, const char * token, Building *building_data[]) 
   { 
      get_buildings_cpprest(user_id, token, *building_data).wait(); 

      if (!building_id.empty()) 
      { 
         Building *b = new Building[building_id.size()]; 

         b->is_empty = false; 
         b->size = building_id.size(); 

         for (int i = 0; i < building_id.size(); i++) 
         { 
            b.id = const_cast<char*>(building_id.c_str()); 
            b.name = const_cast<char*>(building_name.c_str()); 
         } 
         *building_data = b; 
      } 
      } 
     }
I believe the error occurred because of the delete[] operator overridden by API.
My code separately (in exe) works correctly
Please give me an advice to solve this problem
Anonymous
Not applicable
Oleg wrote:
I think global new and delete overriden by API.
May be ::delete[] building_data will work;

But actually it is poor DLL design.
You need do and export function from DLL like
release_building_info(Building *building_data[])
and free the memory.
I think so, global new and delete overridden by API
It crash also when I call ::delete[]

What you mean ?
>But actually it is poor DLL design.
>You need do and export function from DLL like
>release_building_info(Building *building_data[])
>and free the memory

I do allocated in dll and delete in add-on code.
Oleg
Expert
ggiloyan wrote:
I do allocated in dll and delete in add-on code.
You need both to allocate and to free in dll.

Implement a function in dll like this:

_declspec(dllexport) void release_building_info(Building *building_data[])
{
delete [] building data;
}

In addon code insted delete[] call release_building_info
Anonymous
Not applicable
Oleg wrote:
ggiloyan wrote:
I do allocated in dll and delete in add-on code.
You need both to allocate and to free in dll.

Implement a function in dll like this:

_declspec(dllexport) void release_building_info(Building *building_data[])
{
delete [] building data;
}

In addon code insted delete[] call release_building_info
Thank for the help.
It works correctly by this way
__declspec(dllexport) void release_building(Building *building_data[], int size)
	{
		for (int i = 0; i < size; ++i)
		{
			delete[] building_data;
		}
	}

Didn't find the answer?

Check other topics in this Forum

Back to Forum

Read the latest accepted solutions!

Accepted Solutions

Start a new conversation!