Skip to content

Commit

Permalink
Make using new for allocating DF objects with vtables a compile-time …
Browse files Browse the repository at this point in the history
…error.

When done from plugins, it doesn't correctly initialize the vtable
because of some weird things MSVC does, so the only safe way is to
use df::allocate<df::foo>(). For consistency, it is also enforced
for code in the main library. It reveals the issue in the digging
invaders plugin, first found by warmist.

This change is linked to a modification in df-structures codegen.
  • Loading branch information
angavrilov committed Apr 30, 2014
1 parent 9832575 commit 415cdad
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions library/include/df/custom/viewscreen.methods.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
friend struct df::interfacest;
8 changes: 7 additions & 1 deletion library/modules/Items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,16 +997,22 @@ df::proj_itemst *Items::makeProjectile(MapExtras::MapCache &mc, df::item *item)
if (!ref)
return NULL;

auto proj = df::allocate<df::proj_itemst>();
if (!proj) {
delete ref;
return NULL;
}

if (!detachItem(mc, item))
{
delete ref;
delete proj;
return NULL;
}

item->pos = pos;
item->flags.bits.in_job = true;

auto proj = new df::proj_itemst();
proj->link = new df::proj_list_link();
proj->link->item = proj;
proj->id = (*proj_next_id)++;
Expand Down
2 changes: 1 addition & 1 deletion library/xml
8 changes: 4 additions & 4 deletions plugins/diggingInvaders/assignJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df:
df::job* job = new df::job;
job->job_type = df::enums::job_type::DestroyBuilding;
//job->flags.bits.special = 1;
df::general_ref_building_holderst* buildingRef = new df::general_ref_building_holderst;
df::general_ref_building_holderst* buildingRef = df::allocate<df::general_ref_building_holderst>();
buildingRef->building_id = building->id;
job->general_refs.push_back(buildingRef);
df::general_ref_unit_workerst* workerRef = new df::general_ref_unit_workerst;
df::general_ref_unit_workerst* workerRef = df::allocate<df::general_ref_unit_workerst>();
workerRef->unit_id = firstInvader->id;
job->general_refs.push_back(workerRef);
getRidOfOldJob(firstInvader);
Expand All @@ -118,7 +118,7 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df:
if ( construction2 ) {
df::job* job = new df::job;
job->job_type = df::enums::job_type::RemoveConstruction;
df::general_ref_unit_workerst* workerRef = new df::general_ref_unit_workerst;
df::general_ref_unit_workerst* workerRef = df::allocate<df::general_ref_unit_workerst>();
workerRef->unit_id = firstInvader->id;
job->general_refs.push_back(workerRef);
job->pos = pt2;
Expand Down Expand Up @@ -189,7 +189,7 @@ int32_t assignJob(color_ostream& out, Edge firstImportantEdge, unordered_map<df:
job->pos = workHere;
firstInvader->path.dest = goHere;
location = goHere;
df::general_ref_unit_workerst* ref = new df::general_ref_unit_workerst;
df::general_ref_unit_workerst* ref = df::allocate<df::general_ref_unit_workerst>();
ref->unit_id = firstInvader->id;
job->general_refs.push_back(ref);
firstInvader->job.hunt_target = NULL;
Expand Down

0 comments on commit 415cdad

Please sign in to comment.