AddRef

This function increments the object reference count by one. Use this to allocate a new copy of an object to prevent the object from being deleted when Release() is called.

Syntax

Returns

Returns the new reference count.

Example

#include "Leadwerks.h"

using namespace Leadwerks;

Texture* texture = NULL;

int main(int argc, const char *argv[])
{
Leadwerks::Window* window = Leadwerks::Window::Create();
Context* context = Context::Create(window);
World* world = World::Create();
Camera* camera = Camera::Create();
DirectionalLight::Create()->SetRotation(35,45,0);

//Create a model
Model* model = Model::Load("Models/Characters/Barbarian/Barbarian.mdl");

//Get the first model surface
Surface* surface = model->GetSurface(0);

//Get the surface material
Material* material = surface->GetMaterial();

//Get the material diffuse texture
texture = material->GetTexture(Texture::Diffuse);
System::Print(texture->GetRefCount());// prints "1" because this is the default reference count for all objects

//Increment the texture reference count
texture->AddRef();//comment this line out to see a crash!
System::Print(texture->GetRefCount());// prints "2" because we incremented the reference count

//Release the model. Because we allocated another copy of the texture,
//it will stay in memory and we can continue to use it.
model->Release();
System::Print(texture->GetRefCount());// prints "1" because the reference count was decremented when the model material was deleted

while (true)
{
if (window->Closed() || window->KeyDown(Key::Escape)) return false;

Leadwerks::Time::Update();
world->Update();
world->Render();

//Draw the texture on screen
Draw::Image(texture,0,0);

context->Sync();
}
return 0;
}