Jump to content

Leadwerks News

  • entries
    190
  • comments
    1,264
  • views
    769,968

Contributors to this blog

C++11 Shared Pointers Make Leadwerks 5 Simpler


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.

  • Upvote 1

1 Comment


Recommended Comments

Josh

Posted

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.");

 

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.

×
×
  • Create New...