Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Leadwerks Company Blog

  • entries
    179
  • comments
    1,161
  • views
    337,333

Contributors to this blog

C++11 Shared Pointers Make Leadwerks 5 Simpler


Josh

1,116 views

This shows the fundamental difference between shared pointers and manual reference counting.

Leadwerks 4:

void Material::SetTexture(Texture* texture, const int index)
{
	if (this->texture[index] != texture)
	{
		if (this->texture[index]) this->texture[index]->Release();
		this->texture[index] = texture;
		if (this->texture[index]) this->texture[index]->AddRef();
	}
}

Leadwerks 5:

void Material::SetTexture(shared_ptr<Texture> texture, const int index)
{
	this->texture[index] = texture;
}

The second example automatically does the same thing as the first (basically) but you don't have to worry about making mistakes.  When the texture is no longer referred to by any variable, it will be automatically deleted from system and video memory.

1 Comment


Recommended Comments

I omitted this line that checks the index to make the example more obvious:

if ((index<0)||(index>31)) Debug::Error("Texture index out of range.");

 

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Facebook Twitter Youtube Github Steam
×
×
  • Create New...