[Trilinos-Users] Teuchos::RCP behavior

Bartlett, Roscoe A. bartlettra at ornl.gov
Fri May 10 08:44:10 MDT 2013


To be safe, the initial object must be dynamically allocated and then given sole ownership to a strong RCP.  If you do that a never expose a raw pointer or raw C++ reference, it will be impossible to access undefined behavior.  Think about what RCP can and can't know given the limits of C++ (i.e., there is no way to know if a pointer to an object is on the stack or on the heap).

Just follow the creation idioms like the non-member constructor idiom defined in "5.8.1 The non-member constructor function idiom" in:

    http://www.ornl.gov/~8vt/TeuchosMemoryManagementSAND.pdf

and as long as you don't expose a raw pointer or raw reference (or do so very very carefully and only for non-persisting associations) you will never be able to  access undefined behavior with respect to your handling of memory in your programs.

What we really need is dynamic analysis applying these rules built into the compiler (which I think we can do with Clang C++) and then it would warn (or error out) about all of these types of extra things the Teuchos Memory Management classes can't know on their own.

Cheers,
-Ross


From: trilinos-users-bounces at software.sandia.gov [mailto:trilinos-users-bounces at software.sandia.gov] On Behalf Of Villa Andrea (RSE)
Sent: Friday, May 10, 2013 10:12 AM
To: Holger Brandsmeier
Cc: trilinos-users at software.sandia.gov
Subject: Re: [Trilinos-Users] Teuchos::RCP behavior


At page 68 of "Teuchos C++ Memory Management Classes, Idioms , and Related Topics" I think that the same problem is treated.
As I have understood the built-in debug mode should identify this dangling reference...

AV


-----Original Message-----
From: Holger Brandsmeier [mailto:holger.brandsmeier at sam.math.ethz.ch]
Sent: Fri 5/10/2013 3:36 PM
To: Villa Andrea (RSE)
Cc: trilinos-users at software.sandia.gov<mailto:trilinos-users at software.sandia.gov>
Subject: Re: [Trilinos-Users] Teuchos::RCP behavior

Enabling:
>> -D Teuchos_ENABLE_DEBUG:BOOL=ON
>> -D Teuchos_ENABLE_DEBUG_RCP_NODE_TRACING:BOOL=ON
Does not give you an "internal" Debugger. You should use `valgrind`
for that. If you use an RCP and some other class outside of RCP is
deleting the underlying object, then trilinos has no way to warn you
about it. You likely will get an segfault.

`rcpFromRef` is a dangerous function. You should really be sure that
the reference you generate stays alive longer than the RCP. If not you
are in trouble and there is nothing that Trilinos can do about it.

I am not an expert on those debug settings, they mostly help you to
see if an RCP is deleted not at all (still alive when trilinos exits)
and for some protections using weak RCPs. Plus I need to go for Tennis
now ;-)

-Holger

On Fri, May 10, 2013 at 3:26 PM, Villa Andrea (RSE)
<Andrea.Villa at rse-web.it<mailto:Andrea.Villa at rse-web.it>> wrote:
> In fact I was triggering a serious trouble to see whether the internal
> debugger can recognize it.
> How to activate the run-time debugging mode?
> Only making Trilinos with
>
>
>> -D Teuchos_ENABLE_DEBUG:BOOL=ON
>> -D Teuchos_ENABLE_DEBUG_RCP_NODE_TRACING:BOOL=ON
>
> ?
>
> Thanks.
> Andrea.
>
>
>
>
> -----Original Message-----
> From: Holger Brandsmeier [mailto:holger.brandsmeier at sam.math.ethz.ch]
> Sent: Fri 5/10/2013 3:24 PM
> To: Villa Andrea (RSE)
> Cc: trilinos-users at software.sandia.gov<mailto:trilinos-users at software.sandia.gov>
> Subject: Re: [Trilinos-Users] Teuchos::RCP behavior
>
> Andrea,
>
> indeed you are running in serious trouble here ...
>
>> void assign(Teuchos::RCP<UInt> & P)
>> {
>>   UInt i = 4;
>>   P = Teuchos::rcpFromRef(i);
>>   cout << *P << endl;
>> }
> So you generate a class i of type "UInt" and P is a pointer on that
> piece of memory. Once `assign` exists `i` is deleted and `P` referes
> to invalid memory. This is bad.
>
> Why don't you do:
> void assign(Teuchos::RCP<UInt> & P)
> {
>    UInt* i = new UInt();
>    *i = 4;
>    P = Teuchos::rcp(i);
>    cout << *P << endl;
>  }
>
> Or at least tell us what you try to achieve.
>
> -Holger
>
>
> On Fri, May 10, 2013 at 3:15 PM, Villa Andrea (RSE)
> <Andrea.Villa at rse-web.it<mailto:Andrea.Villa at rse-web.it>> wrote:
>>
>> I've implemented a very simple test code:
>>
>> void assign(Teuchos::RCP<UInt> & P)
>> {
>>   UInt i = 4;
>>   P = Teuchos::rcpFromRef(i);
>>   cout << *P << endl;
>> }
>>
>> int main(int argc, char *argv[])
>> {
>>   Teuchos::RCP<UInt> P;
>>   assign(P);
>>
>>   cout << *P << endl;
>>   cout << "count: " << P.total_count() << endl;
>> }
>>
>> I think that this is a non-correct usage of the RCP class, since a
>> temporary
>> reference is passed to P. In fact I get the following output:
>>
>> 4
>> 0
>> count: 1
>>
>> However even I have built Trilinos using
>> -D Teuchos_ENABLE_DEBUG:BOOL=ON \
>> -D Teuchos_ENABLE_DEBUG_RCP_NODE_TRACING:BOOL=ON \
>> no exception is thrown. Am I missing something?
>>
>> Thank you very much.
>> Andrea Villa.
>>
>>
>> RSE SpA ha adottato il Modello Organizzativo ai sensi del D.Lgs.231/2001,
>> in
>> forza del quale l'assunzione di obbligazioni da parte della Società
>> avviene
>> con firma di un procuratore, munito di idonei poteri. RSE adopts a
>> Compliance Programme under the Italian Law (D.Lgs.231/2001). According to
>> this RSE Compliance Programme, any commitment of RSE is taken by the
>> signature of one Representative granted by a proper Power of Attorney.
>>
>> Le informazioni contenute in questo messaggio di posta elettronica sono
>> riservate e confidenziali e ne e' vietata la diffusione in qualsiasi modo
>> o
>> forma. Qualora Lei non fosse la persona destinataria del presente
>> messaggio,
>> La invitiamo a non diffonderlo e ad eliminarlo, dandone gentilmente
>> comunicazione al mittente. The information included in this e-mail and any
>> attachments are confidential and may also be privileged. If you are not
>> the
>> correct recipient, you are kindly requested to notify the sender
>> immediately, to cancel it and not to disclose the contents to any other
>> person.
>>
>> _______________________________________________
>> Trilinos-Users mailing list
>> Trilinos-Users at software.sandia.gov<mailto:Trilinos-Users at software.sandia.gov>
>> http://software.sandia.gov/mailman/listinfo/trilinos-users
>>
>
>
>
> --
> Holger Brandsmeier, SAM, ETH Zürich
> http://www.sam.math.ethz.ch/people/bholger
>
>
> RSE SpA ha adottato il Modello Organizzativo ai sensi del D.Lgs.231/2001, in
> forza del quale l'assunzione di obbligazioni da parte della Società avviene
> con firma di un procuratore, munito di idonei poteri. RSE adopts a
> Compliance Programme under the Italian Law (D.Lgs.231/2001). According to
> this RSE Compliance Programme, any commitment of RSE is taken by the
> signature of one Representative granted by a proper Power of Attorney.
>
> Le informazioni contenute in questo messaggio di posta elettronica sono
> riservate e confidenziali e ne e' vietata la diffusione in qualsiasi modo o
> forma. Qualora Lei non fosse la persona destinataria del presente messaggio,
> La invitiamo a non diffonderlo e ad eliminarlo, dandone gentilmente
> comunicazione al mittente. The information included in this e-mail and any
> attachments are confidential and may also be privileged. If you are not the
> correct recipient, you are kindly requested to notify the sender
> immediately, to cancel it and not to disclose the contents to any other
> person.



--
Holger Brandsmeier, SAM, ETH Zürich
http://www.sam.math.ethz.ch/people/bholger

RSE SpA ha adottato il Modello Organizzativo ai sensi del D.Lgs.231/2001, in forza del quale l'assunzione di obbligazioni da parte della Società avviene con firma di un procuratore, munito di idonei poteri. RSE adopts a Compliance Programme under the Italian Law (D.Lgs.231/2001). According to this RSE Compliance Programme, any commitment of RSE is taken by the signature of one Representative granted by a proper Power of Attorney.

Le informazioni contenute in questo messaggio di posta elettronica sono riservate e confidenziali e ne e' vietata la diffusione in qualsiasi modo o forma. Qualora Lei non fosse la persona destinataria del presente messaggio, La invitiamo a non diffonderlo e ad eliminarlo, dandone gentilmente comunicazione al mittente. The information included in this e-mail and any attachments are confidential and may also be privileged. If you are not the correct recipient, you are kindly requested to notify the sender immediately, to cancel it and not to disclose the contents to any other person.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://software.sandia.gov/pipermail/trilinos-users/attachments/20130510/c496b74b/attachment.html 


More information about the Trilinos-Users mailing list