overriding the role of shared_ptr

hello, i have this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class B;

std::vector<std::shared_ptr<B>> b;
b.emplace_back(std::make_shared<B>(B()));


std::shared_ptr<B> ptr1 = b[0];
std::shared_ptr<B> ptr2 = b[0];

//somewhere in my code
int index = someindex;
if ( !b[index].get()->isAlive() )
{
    if ( b[index].use_count() == 1 )
    {
        b.erase(b.begin()+index);
    } 
}


what i did here is have share_ptrs in one object, and then in the middle of program i constantly check if isAlive() will return true, and then if its false im just going to wait until other shared_ptr that manages it destroyed before i delete it.

is this only okay to do this? will this defeat its purpose? or is there any disadvantages?
Is there a specific reason to not use std::weak_ptr<>?
http://en.cppreference.com/w/cpp/memory/weak_ptr
@JLBorges hi, would you tell me why would i use std::weak_ptr here?
im just starting in smart pointers, and shared_ptr is the first one that i needed

at any rate, whether im using shared_ptr or not, i will need to wait the other user of b[index] to be finished before removing the object to the vector.
Aren't you trying to implement something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>

struct B { ~B() { std::cout << "B::destructor " << this << '\n' ; } };

void debug_clean_cache( std::vector< std::weak_ptr<B> >& cache )
{
    static int cnt = 0 ;
    std::cout << "\ndebug_clean # " << ++cnt << "\n--------------\n" ;

    // erase all expired objects
    cache.erase( std::remove_if( std::begin(cache), std::end(cache),
                                 [] ( auto& wp ) { return wp.expired() ; } ),
                 cache.end() ) ;

    // dump addresses of objects that are still alive
    std::cout << "size == " << cache.size() << "  [  " ;
    for( const auto& wp : cache ) std::cout << "object@" << wp.lock().get() << "   " ;
    std::cout << "]\n\n" ;
}

int main()
{
    std::vector< std::weak_ptr<B> > cached_weak_refs ;

    auto ptr1 = std::make_shared<B>() ;
    cached_weak_refs.emplace_back(ptr1) ;

    auto ptr2 = std::make_shared<B>() ;
    cached_weak_refs.emplace_back(ptr2) ;

    auto ptr3 = std::make_shared<B>() ;
    cached_weak_refs.emplace_back(ptr3) ;

    debug_clean_cache(cached_weak_refs) ;

    ptr3 = nullptr ;
    debug_clean_cache(cached_weak_refs) ;

    ptr1 = nullptr ;
    debug_clean_cache(cached_weak_refs) ;

    ptr2 = nullptr ;
    debug_clean_cache(cached_weak_refs) ;
}

http://coliru.stacked-crooked.com/a/634ca6f00975a250
http://rextester.com/XZM28987
@JLBorges okay i read it thoroughly, but this one deletes it when the object it manages is destroyed by checking wp.expired() in the predicate function,

what im doing is the opposite, it should only delete it when no one is needed the managed object.



EDIT:
heres the minimal code from my program
https://hastebin.com/ladikerosu.cpp
Last edited on
Topic archived. No new replies allowed.