Changeset - r7413:3ccdde9800e0
[Not reviewed]
master
0 15 0
rubidium - 17 years ago 2007-08-05 21:20:55
rubidium@openttd.org
(svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
15 files changed with 48 insertions and 67 deletions:
0 comments (0 inline, 0 general)
src/depot.cpp
Show inline comments
 
@@ -34,12 +34,14 @@ Depot *GetDepotByTile(TileIndex tile)
 

	
 
/**
 
 * Clean up a depot
 
 */
 
Depot::~Depot()
 
{
 
	if (CleaningPool()) return;
 

	
 
	/* Clear the depot from all order-lists */
 
	RemoveOrderFromAllVehicles(OT_GOTO_DEPOT, this->index);
 

	
 
	/* Delete the depot-window */
 
	DeleteWindowById(WC_VEHICLE_DEPOT, this->xy);
 
	this->xy = 0;
src/group.h
Show inline comments
 
@@ -26,14 +26,12 @@ struct Group : PoolItem<Group, GroupID, 
 
	bool replace_protection;                ///< If set to true, the global autoreplace have no effect on the group
 
	uint16 num_engines[TOTAL_NUM_ENGINES];  ///< Caches the number of engines of each type the player owns (no need to save this)
 

	
 
	Group(StringID str = STR_NULL);
 
	virtual ~Group();
 

	
 
	void QuickFree();
 

	
 
	bool IsValid() const;
 
};
 

	
 

	
 
static inline bool IsValidGroupID(GroupID index)
 
{
src/group_cmd.cpp
Show inline comments
 
@@ -47,21 +47,16 @@ Group::Group(StringID str)
 
{
 
	this->string_id = str;
 
}
 

	
 
Group::~Group()
 
{
 
	this->QuickFree();
 
	DeleteName(this->string_id);
 
	this->string_id = STR_NULL;
 
}
 

	
 
void Group::QuickFree()
 
{
 
	DeleteName(this->string_id);
 
}
 

	
 
bool Group::IsValid() const
 
{
 
	return this->string_id != STR_NULL;
 
}
 

	
 
void InitializeGroup(void)
src/oldpool.cpp
Show inline comments
 
@@ -15,19 +15,21 @@
 
void OldMemoryPoolBase::CleanPool()
 
{
 
	uint i;
 

	
 
	DEBUG(misc, 4, "[Pool] (%s) cleaning pool..", this->name);
 

	
 
	this->cleaning_pool = true;
 
	/* Free all blocks */
 
	for (i = 0; i < this->current_blocks; i++) {
 
		if (this->clean_block_proc != NULL) {
 
			this->clean_block_proc(i * (1 << this->block_size_bits), (i + 1) * (1 << this->block_size_bits) - 1);
 
		}
 
		free(this->blocks[i]);
 
	}
 
	this->cleaning_pool = false;
 

	
 
	/* Free the block itself */
 
	free(this->blocks);
 

	
 
	/* Clear up some critical data */
 
	this->total_items = 0;
src/oldpool.h
Show inline comments
 
@@ -22,13 +22,13 @@ struct OldMemoryPoolBase {
 

	
 
protected:
 
	OldMemoryPoolBase(const char *name, uint max_blocks, uint block_size_bits, uint item_size,
 
				OldMemoryPoolNewBlock *new_block_proc, OldMemoryPoolCleanBlock *clean_block_proc) :
 
		name(name), max_blocks(max_blocks), block_size_bits(block_size_bits),
 
		new_block_proc(new_block_proc), clean_block_proc(clean_block_proc), current_blocks(0),
 
		total_items(0), item_size(item_size), first_free_index(0), blocks(NULL) {}
 
		total_items(0), cleaning_pool(false), item_size(item_size), first_free_index(0), blocks(NULL) {}
 

	
 
	const char* name;     ///< Name of the pool (just for debugging)
 

	
 
	const uint max_blocks;      ///< The max amount of blocks this pool can have
 
	const uint block_size_bits; ///< The size of each block in bits
 

	
 
@@ -37,12 +37,13 @@ protected:
 
	/// Pointer to a function that is called to clean a block
 
	OldMemoryPoolCleanBlock *clean_block_proc;
 

	
 
	uint current_blocks;        ///< How many blocks we have in our pool
 
	uint total_items;           ///< How many items we now have in this pool
 

	
 
	bool cleaning_pool;         ///< Are we currently cleaning the pool?
 
public:
 
	const uint item_size;       ///< How many bytes one block is
 
	uint first_free_index;      ///< The index of the first free pool item in this pool
 
	byte **blocks;              ///< An array of blocks (one block hold all the items)
 

	
 
	/**
 
@@ -81,12 +82,21 @@ public:
 
	 * @return the name
 
	 */
 
	inline const char *GetName() const
 
	{
 
		return this->name;
 
	}
 

	
 
	/**
 
	 * Is the pool in the cleaning phase?
 
	 * @return true if it is
 
	 */
 
	inline bool CleaningPool() const
 
	{
 
		return this->cleaning_pool;
 
	}
 
};
 

	
 
template <typename T>
 
struct OldMemoryPool : public OldMemoryPoolBase {
 
	OldMemoryPool(const char *name, uint max_blocks, uint block_size_bits, uint item_size,
 
				OldMemoryPoolNewBlock *new_block_proc, OldMemoryPoolCleanBlock *clean_block_proc) :
 
@@ -118,24 +128,21 @@ static void PoolNewBlock(uint start_item
 
		t->index = start_item++;
 
	}
 
}
 

	
 
/**
 
 * Generic function to free a new block in a pool.
 
 * This function uses QuickFree that is intended to only free memory that would be lost if the pool is freed.
 
 * @param start_item the first item that needs to be cleaned
 
 * @param end_item   the last item that needs to be cleaned
 
 */
 
template <typename T, OldMemoryPool<T> *Tpool>
 
static void PoolCleanBlock(uint start_item, uint end_item)
 
{
 
	for (uint i = start_item; i <= end_item; i++) {
 
		T *t = Tpool->Get(i);
 
		if (t->IsValid()) {
 
			t->QuickFree();
 
		}
 
		delete t;
 
	}
 
}
 

	
 

	
 
/**
 
 * Generalization for all pool items that are saved in the savegame.
 
@@ -154,21 +161,12 @@ struct PoolItem {
 
	virtual ~PoolItem()
 
	{
 
		if (this->index < Tpool->first_free_index) Tpool->first_free_index = this->index;
 
	}
 

	
 
	/**
 
	 * Called on each object when the pool is being destroyed, so one
 
	 * can free allocated memory without the need for freeing for
 
	 * example orders.
 
	 */
 
	virtual void QuickFree()
 
	{
 
	}
 

	
 
	/**
 
	 * An overriden version of new that allocates memory on the pool.
 
	 * @param size the size of the variable (unused)
 
	 * @return the memory that is 'allocated'
 
	 */
 
	void *operator new(size_t size)
 
	{
 
@@ -238,23 +236,23 @@ struct PoolItem {
 

	
 
protected:
 
	/**
 
	 * Allocate a pool item; possibly allocate a new block in the pool.
 
	 * @return the allocated pool item (or NULL when the pool is full).
 
	 */
 
	static T *AllocateRaw()
 
	static inline T *AllocateRaw()
 
	{
 
		return AllocateRaw(Tpool->first_free_index);
 
	}
 

	
 
	/**
 
	 * Allocate a pool item; possibly allocate a new block in the pool.
 
	 * @param first the first pool item to start searching
 
	 * @return the allocated pool item (or NULL when the pool is full).
 
	 */
 
	static T *AllocateRaw(uint &first)
 
	static inline T *AllocateRaw(uint &first)
 
	{
 
		uint last_minus_one = Tpool->GetSize() - 1;
 

	
 
		for (T *t = Tpool->Get(first); t != NULL; t = (t->index < last_minus_one) ? Tpool->Get(t->index + 1U) : NULL) {
 
			if (!t->IsValid()) {
 
				first = t->index;
 
@@ -268,12 +266,21 @@ protected:
 

	
 
		/* Check if we can add a block to the pool */
 
		if (Tpool->AddBlockToPool()) return AllocateRaw(first);
 

	
 
		return NULL;
 
	}
 

	
 
	/**
 
	 * Are we cleaning this pool?
 
	 * @return true if we are
 
	 */
 
	static inline bool CleaningPool()
 
	{
 
		return Tpool->CleaningPool();
 
	}
 
};
 

	
 

	
 
#define OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
 
	enum { \
 
		name##_POOL_BLOCK_SIZE_BITS = block_size_bits, \
src/signs.cpp
Show inline comments
 
@@ -25,21 +25,16 @@ Sign::Sign(StringID string)
 
{
 
	this->str = string;
 
}
 

	
 
Sign::~Sign()
 
{
 
	this->QuickFree();
 
	DeleteName(this->str);
 
	this->str = STR_NULL;
 
}
 

	
 
void Sign::QuickFree()
 
{
 
	DeleteName(this->str);
 
}
 

	
 
/**
 
 *
 
 * Update the coordinate of one sign
 
 * @param si Pointer to the Sign
 
 *
 
 */
src/signs.h
Show inline comments
 
@@ -24,14 +24,12 @@ struct Sign : PoolItem<Sign, SignID, &_S
 
	Sign(StringID string = STR_NULL);
 

	
 
	/** Destroy the sign */
 
	~Sign();
 

	
 
	bool IsValid() const { return this->str != STR_NULL; }
 

	
 
	void QuickFree();
 
};
 

	
 
enum {
 
	INVALID_SIGN = 0xFFFF,
 
};
 

	
src/station.cpp
Show inline comments
 
@@ -61,12 +61,17 @@ Station::Station(TileIndex tile)
 
 * is OT_GOTO_DEPOT.
 
 */
 
Station::~Station()
 
{
 
	DEBUG(station, cDebugCtorLevel, "I-%3d", index);
 

	
 
	DeleteName(this->string_id);
 
	free(this->speclist);
 

	
 
	if (CleaningPool()) return;
 

	
 
	MarkDirty();
 
	RebuildStationLists();
 
	InvalidateWindowClasses(WC_STATION_LIST);
 

	
 
	DeleteWindowById(WC_STATION_VIEW, index);
 

	
 
@@ -78,20 +83,12 @@ Station::~Station()
 

	
 
	xy = 0;
 

	
 
	for (CargoID c = 0; c < NUM_CARGO; c++) {
 
		goods[c].cargo.Truncate(0);
 
	}
 

	
 
	this->QuickFree();
 
}
 

	
 
void Station::QuickFree()
 
{
 
	DeleteName(this->string_id);
 
	free(this->speclist);
 
}
 

	
 
/** Called when new facility is built on the station. If it is the first facility
 
 * it initializes also 'xy' and 'random_bits' members */
 
void Station::AddFacility(byte new_facility_bit, TileIndex facil_xy)
 
{
src/station.h
Show inline comments
 
@@ -156,14 +156,12 @@ struct Station : PoolItem<Station, Stati
 

	
 
	static const int cDebugCtorLevel = 5;
 

	
 
	Station(TileIndex tile = 0);
 
	virtual ~Station();
 

	
 
	void QuickFree();
 

	
 
	void AddFacility(byte new_facility_bit, TileIndex facil_xy);
 
	void MarkDirty() const;
 
	void MarkTilesDirty(bool cargo_change) const;
 
	bool TileBelongsToRailStation(TileIndex tile) const;
 
	uint GetPlatformLength(TileIndex tile, DiagDirection dir) const;
 
	uint GetPlatformLength(TileIndex tile) const;
src/town.h
Show inline comments
 
@@ -157,14 +157,12 @@ struct Town : PoolItem<Town, TownID, &_T
 
	Town(TileIndex tile = 0);
 

	
 
	/** Destroy the town */
 
	~Town();
 

	
 
	bool IsValid() const { return this->xy != 0; }
 

	
 
	void QuickFree();
 
};
 

	
 
struct HouseSpec {
 
	/* Standard properties */
 
	Year min_date;                     ///< introduction year of the house
 
	Year max_date;                     ///< last year it can be built
src/town_cmd.cpp
Show inline comments
 
@@ -50,12 +50,16 @@ Town::Town(TileIndex tile)
 
	if (tile != 0) _total_towns++;
 
	this->xy = tile;
 
}
 

	
 
Town::~Town()
 
{
 
	DeleteName(this->townnametype);
 

	
 
	if (CleaningPool()) return;
 

	
 
	Industry *i;
 

	
 
	/* Delete town authority window
 
	 * and remove from list of sorted towns */
 
	DeleteWindowById(WC_TOWN_VIEW, this->index);
 
	_town_sort_dirty = true;
 
@@ -84,21 +88,15 @@ Town::~Town()
 
	}
 

	
 
	DeleteSubsidyWithTown(this->index);
 

	
 
	MarkWholeScreenDirty();
 

	
 
	this->QuickFree();
 
	this->xy = 0;
 
}
 

	
 
void Town::QuickFree()
 
{
 
	DeleteName(this->townnametype);
 
}
 

	
 
// Local
 
static int _grow_town_result;
 

	
 
static bool BuildTownHouse(Town *t, TileIndex tile);
 
static void DoBuildTownHouse(Town *t, TileIndex tile);
 

	
src/vehicle.cpp
Show inline comments
 
@@ -561,12 +561,14 @@ bool IsEngineCountable(const Vehicle *v)
 
		default: return false; // Only count player buildable vehicles
 
	}
 
}
 

	
 
void Vehicle::PreDestructor()
 
{
 
	if (CleaningPool()) return;
 

	
 
	if (IsValidStationID(this->last_station_visited)) {
 
		GetStation(this->last_station_visited)->loading_vehicles.remove(this);
 

	
 
		HideFillingPercent(this->fill_percent_te_id);
 
		this->fill_percent_te_id = INVALID_TE_ID;
 
	}
 
@@ -576,13 +578,12 @@ void Vehicle::PreDestructor()
 
		if (this->owner == _local_player) InvalidateAutoreplaceWindow(this->engine_type, this->group_id);
 

	
 
		if (IsValidGroupID(this->group_id)) GetGroup(this->group_id)->num_engines[this->engine_type]--;
 
		if (this->IsPrimaryVehicle()) DecreaseGroupNumVehicle(this->group_id);
 
	}
 

	
 
	this->QuickFree();
 
	if (this->type == VEH_ROAD) ClearSlot(this);
 

	
 
	if (this->type != VEH_TRAIN || (this->type == VEH_TRAIN && (IsFrontEngine(this) || IsFreeWagon(this)))) {
 
		InvalidateWindowData(WC_VEHICLE_DEPOT, this->tile);
 
	}
 

	
 
@@ -596,26 +597,25 @@ void Vehicle::PreDestructor()
 
		delete this->next;
 
	}
 
}
 

	
 
Vehicle::~Vehicle()
 
{
 
	DeleteName(this->string_id);
 

	
 
	if (CleaningPool()) return;
 

	
 
	UpdateVehiclePosHash(this, INVALID_COORD, 0);
 
	this->next_hash = NULL;
 
	this->next_new_hash = NULL;
 

	
 
	DeleteVehicleNews(this->index, INVALID_STRING_ID);
 

	
 
	new (this) InvalidVehicle();
 
}
 

	
 
void Vehicle::QuickFree()
 
{
 
	DeleteName(this->string_id);
 
}
 

	
 
/**
 
 * Deletes all vehicles in a chain.
 
 * @param v The first vehicle in the chain.
 
 *
 
 * @warning This function is not valid for any vehicle containing articulated
 
 * parts.
src/vehicle.h
Show inline comments
 
@@ -349,14 +349,12 @@ struct Vehicle : PoolItem<Vehicle, Vehic
 

	
 
	/** Destroy all stuff that (still) needs the virtual functions to work properly */
 
	void PreDestructor();
 
	/** We want to 'destruct' the right class. */
 
	virtual ~Vehicle();
 

	
 
	void QuickFree();
 

	
 
	void BeginLoading();
 
	void LeaveStation();
 

	
 
	/**
 
	 * Handle the loading of the vehicle; when not it skips through dummy
 
	 * orders and does nothing in all other cases.
src/waypoint.cpp
Show inline comments
 
@@ -401,23 +401,20 @@ Waypoint::Waypoint(TileIndex tile)
 
{
 
	this->xy = tile;
 
}
 

	
 
Waypoint::~Waypoint()
 
{
 
	if (this->string != STR_NULL) DeleteName(this->string);
 

	
 
	if (CleaningPool()) return;
 

	
 
	RemoveOrderFromAllVehicles(OT_GOTO_WAYPOINT, this->index);
 

	
 
	RedrawWaypointSign(this);
 
	this->xy = 0;
 

	
 
	this->QuickFree();
 
}
 

	
 
void Waypoint::QuickFree()
 
{
 
	if (this->string != STR_NULL) DeleteName(this->string);
 
}
 

	
 
bool Waypoint::IsValid() const
 
{
 
	return this->xy != 0;
 
}
src/waypoint.h
Show inline comments
 
@@ -27,14 +27,12 @@ struct Waypoint : PoolItem<Waypoint, Way
 

	
 
	byte deleted;      ///< Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is deleted.
 

	
 
	Waypoint(TileIndex tile = 0);
 
	~Waypoint();
 

	
 
	void QuickFree();
 

	
 
	bool IsValid() const;
 
};
 

	
 
static inline bool IsValidWaypointID(WaypointID index)
 
{
 
	return index < GetWaypointPoolSize() && GetWaypoint(index)->IsValid();
0 comments (0 inline, 0 general)