Jump to content

Ultra Software Company Blog

  • entries
    188
  • comments
    1,259
  • views
    699,886

Contributors to this blog

C++11 Shared Pointers Make Leadwerks 5 Simpler


Josh

2,012 views

 Share

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
 Share

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.

×
×
  • Create New...