Andrew
Andrew

Reputation: 24866

boost shared_ptr get owner count

I'm using boost::shared_ptr to store a pointer to texture. I'm loading new textures as i need and share them among the program using shared_ptr. If my app is using too much memory i want to remove unused textures to clear memory. Is there a way I can determine how many objects are having access to the texture via shared_ptr ?

Upvotes: 4

Views: 2051

Answers (4)

Kerrek SB
Kerrek SB

Reputation: 477600

The shared_ptr class has member functions use_count() and unique() to give you access to its usage count.

It's a different question how that information will be useful to you, though.

Upvotes: 1

hammar
hammar

Reputation: 139930

There is use_count(), however note that as the documentation says, it's not necessarily too efficient.

Upvotes: 1

Nicola Musatti
Nicola Musatti

Reputation: 18236

You can use shared_ptr::use_count(), but read the documentation before doing so!

Upvotes: 2

Puppy
Puppy

Reputation: 147036

If it's unused then the shared_ptr will free it automatically. That's the point of shared_ptr. If you are holding a shared_ptr to a texture without actually using it, then you're violating the contract of shared_ptr and should not be using it.

Upvotes: 6

Related Questions