[Bug c++/86256] Lambda will not add ref count for class intelligent pointer member when capture 'this' or & as argument

2018-06-24 Thread kangchuanbo at 126 dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86256

--- Comment #4 from kangchuanbo at 126 dot com ---
OK, thanks for your response, I just met this issue and think it's better to
give warn or error when Lambda capture this or reference with std::shared_ptr
member inside.
So add one Lambda usage attention maybe a better way to prevent such issue
happen again.
Have a nice day, thanks.






At 2018-06-24 14:15:37, "redi at gcc dot gnu.org" 
wrote:
>https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86256
>
>Jonathan Wakely  changed:
>
>   What|Removed |Added
>
> Status|WAITING |RESOLVED
> Resolution|--- |INVALID
>
>--- Comment #3 from Jonathan Wakely  ---
>But this is true for any lambda that captured 'this' or captures by reference.
>It doesn't make sense to warn for every use of that language feature, just in
>case it's used incorrectly.
>
>-- 
>You are receiving this mail because:
>You reported the bug.

[Bug c++/86256] Lambda will not add ref count for class intelligent pointer member when capture 'this' or & as argument

2018-06-23 Thread kangchuanbo at 126 dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86256

--- Comment #2 from kangchuanbo at 126 dot com ---
Once Lamdba capture this ( one class instance which have std::shared_ptr<>
member), the std::shared_ptr member will not increase ref count inside Lambda
body, this is dangerous, once this member has been freed, the Lambda body will
access null pointer. 








At 2018-06-21 19:47:22, "redi at gcc dot gnu.org" 
wrote:
>https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86256
>
>Jonathan Wakely  changed:
>
>   What|Removed |Added
>
> Status|UNCONFIRMED |WAITING
>   Last reconfirmed||2018-06-21
> Ever confirmed|0   |1
>
>--- Comment #1 from Jonathan Wakely  ---
>N.B. GCC 5.4 is no longer supported or maintained.
>
>I don't understand your bug report, GCC is compiling the code correctly.
>
>What do you think should happen?
>
>-- 
>You are receiving this mail because:
>You reported the bug.

[Bug c++/86256] New: Lambda will not add ref count for class intelligent pointer member when capture 'this' or & as argument

2018-06-20 Thread kangchuanbo at 126 dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86256

Bug ID: 86256
   Summary: Lambda will not add ref count for class intelligent
pointer member when capture 'this' or & as argument
   Product: gcc
   Version: 5.4.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: kangchuanbo at 126 dot com
  Target Milestone: ---

For example:

#include 
#include 

class A {
public:
A(){
tmp = std::make_shared(1);
}
void start() {
std::cout << "ref count : " << tmp.use_count() << std::endl;
std::shared_ptr tmp2 = tmp;
std::cout << "ref count : " << tmp.use_count() << std::endl;
auto xxfunca = [this]()  { std::cout<<"func1 ref count :
"< tmp;
};

int main()
{
A a;
a.start();
return 0;
}

result and analyse:
[root~]]# ./test
ref count : 1// tmp init ref count = 1
ref count : 2// copy to tmp2,tmp ref count will incrase to 2
ref count : 2// Lambda capture this,tmp ref count no incrase
ref count : 3// Lambda capture tmp2,tmp ref count incrase to 3
ref count : 3// Lambda capture &,tmp ref count no incrase

==
The compiler will not copy class member, may feel too complex to check class
member, but should give warning or error to user when lambda capture Class with
intelligent member, or user may meet null pointer which lead to coredump.
Thanks.