2015-03-01
07:49 PM
- last edited on
2023-08-01
01:37 PM
by
Doreena Deng
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 {
}
}
2015-03-01 08:35 PM
2015-03-02 09:12 AM
2015-03-02 10:47 AM
Karl wrote:I allocate with new operator in my dll code because I have to determine the size of building structure before filling it
I might not be seeing it ... but where did you do a 'new' to allocate the building_data object instantiation?
_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.2015-03-02 10:52 AM
Oleg wrote:I think so, global new and delete overridden by API
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.
2015-03-02 11:04 AM
ggiloyan wrote:You need both to allocate and to free in dll.
I do allocated in dll and delete in add-on code.
2015-03-03 09:27 AM
Oleg wrote:Thank for the help.ggiloyan wrote:You need both to allocate and to free in dll.
I do allocated in dll and delete in add-on code.
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
__declspec(dllexport) void release_building(Building *building_data[], int size)
{
for (int i = 0; i < size; ++i)
{
delete[] building_data;
}
}