Skip to content

Commit

Permalink
It's wrong to allocate memory with 'new' and deallocate with 'free'.
Browse files Browse the repository at this point in the history
  • Loading branch information
intorr committed Feb 13, 2018
1 parent 2dff3de commit 80433fa
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/Layers/xrRenderDX10/StateManager/dx10State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ HRESULT dx10State::Apply()
void dx10State::Release()
{
dx10State* pState = this;
xr_delete<dx10State>(pState);
xr_delete/*<dx10State>*/(pState);
}

void dx10State::InitSamplers(tSamplerHArray& SamplerArray, SimulatorStates& state_code, int iBaseSamplerIndex)
Expand Down
44 changes: 4 additions & 40 deletions src/xrCore/xrMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,10 @@ extern XRCORE_API xrMemory Memory;
#define CopyMemory(a, b, c) memcpy(a, b, c)
#define FillMemory(a, b, c) memset(a, c, b)

// delete

template <bool _is_pm, typename T>
struct xr_special_free
{
IC void operator()(T*& ptr)
{
void* _real_ptr = dynamic_cast<void*>(ptr);
ptr->~T();
Memory.mem_free(_real_ptr);
}
};

template <typename T>
struct xr_special_free<false, T>
{
IC void operator()(T*& ptr)
{
ptr->~T();
Memory.mem_free(ptr);
}
};

template <class T>
IC void xr_delete(T*& ptr)
{
if (ptr)
{
xr_special_free<std::is_polymorphic<T>::value, T>()(ptr);
ptr = NULL;
}
}
template <class T>
IC void xr_delete(T* const& ptr)
{
if (ptr)
{
xr_special_free<std::is_polymorphic<T>::value, T>(ptr);
const_cast<T*&>(ptr) = NULL;
}
#define xr_delete(x)\
{\
delete (x);\
(x) = nullptr;\
}

// generic "C"-like allocations/deallocations
Expand Down
3 changes: 0 additions & 3 deletions src/xrEngine/PS_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class ENGINE_API CPS_Instance : public SpatialBase, public ScheduledBase, public
{
friend class IGame_Persistent;

template <bool _is_pm, typename T>
friend struct xr_special_free;

private:
bool m_destroy_on_game_load;

Expand Down
2 changes: 1 addition & 1 deletion src/xrGame/Actor_Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ void CActor::net_Destroy()
if (m_pPhysicsShell)
{
m_pPhysicsShell->Deactivate();
xr_delete<CPhysicsShell>(m_pPhysicsShell);
xr_delete/*<CPhysicsShell>*/(m_pPhysicsShell);
};
m_pPhysics_support->in_NetDestroy();

Expand Down
2 changes: 2 additions & 0 deletions src/xrGame/UIGameDM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ void CUIGameDM::SetWarmUpCaption(LPCSTR str) { m_warm_up_caption->SetTextST(str)
void CUIGameDM::SetVoteMessage(LPCSTR str)
{
if (!str)
{
xr_delete(m_voteStatusWnd);
}
else
{
if (!m_voteStatusWnd)
Expand Down
2 changes: 1 addition & 1 deletion src/xrPhysics/PHShellActivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ void CPHShell::Deactivate()
{
VERIFY(PhysicsRefObject());
PhysicsRefObject()->ObjectProcessingDeactivate();
xr_delete<CPhysicsShellAnimator>(m_pPhysicsShellAnimatorC);
xr_delete/*<CPhysicsShellAnimator>*/(m_pPhysicsShellAnimatorC);
}

if (!isActive())
Expand Down

0 comments on commit 80433fa

Please sign in to comment.