
----Original Message---- From: H. van Niekerk [mailto:h.v.niekerk@hccnet.nl] Sent: 20 June 2005 19:22 To: boost@lists.boost.org Subject: [boost] shared_ptr question
I'm afraid you didn't. Could you write some sample (pseudo-) code just using raw C pointers and not worrying about memory leaks? Then we can probably work out how to rewrite using smart pointers. Incidentally, I don't think the presence of three .cpp files is directly relevent. I suspect that the relevent factor is that you have three *functions*, and your problem is passing the data between them. -- Martin Bonner Martin.Bonner@Pitechnology.com Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ, ENGLAND Tel: +44 (0)1223 441434

Martin Bonner wrote:
Thank you for your response. I hope the code below explains more: Master.cpp { Client c; c.readFromSatellite(input); cout << "input = " << input << endl; } Client.cpp { Satellite s; readFromSatellite(input) { s.readFromInterface(input); return input; } } Satellite.cpp { readFromInterface(input) { // Here the actual reading is done fti->writeAndReadAllData(output, input); return input; } }

On 6/21/05, H <h.v.niekerk@hccnet.nl> wrote:
Thank you for your response. I hope the code below explains more:
I don't think it does. You're leaving out far too much information about the types of things and the prototypes of the functions you are calling. There's no way to tell what is going on here. -- Caleb Epstein caleb dot epstein at gmail dot com

From: H <h.v.niekerk@hccnet.nl>
As Martin said, the separate files aren't relevant. Also, the usage and declarations of your functions aren't consistent. Let's see if I can recast your problem to something a little simpler to discuss: void function2(some_type data) // stores stuff in caller's object { read_something_from_somewhere(data); } void function1(some_type data) // stores stuff in caller's object { function2(data); } void function3(some_type data) { // do something useful with data } int main() { some_type data; // some pointer type function1(data); // stores stuff in data function3(data); } Is that a good approximation of what you want? If so, some_type should be boost::shared_ptr<something> and the rest should just work. Of course, you can avoid using boost::shared_ptr if function1() and function2() take non-const references to some_type: void function2(some_type & data) // stores stuff in caller's object { read_something_from_somewhere(data); } void function1(some_type & data) // stores stuff in caller's object { function2(data); } void function3(some_type const & data) { // do something useful with data } int main() { some_type data; // not a pointer type function1(data); // stores stuff in data function3(data); } -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

Yes, that looks like it.
OK. The original way was: auto_ptr<FtInterface> fti (ftGetIfFromFactory(device, connection)); With shared_ptr would it become like this: void function2(boost::shared_ptr fti (ftGetIfFromFactory(device, connection)) { ... fti->writeAndReadAllData(out,inp); } void function1(boost::shared_ptr fti (ftGetIfFromFactory(device, connection)) { function2(data); } int main() { boost::shared_ptr<FtInterface> fti (ftGetIfFromFactory(device, connection)); function1(fti (ftGetIfFromFactory(device, connection)); } What would be the benefit of using non-const references? Thank you for helping. Huub

From: H <h.v.niekerk@hccnet.nl>
Absolutely not. I'm not sure where you got the idea that you should somehow get the object in the formal parameter list, but that's for the object passed from the caller.
That should be something like this: void function1(boost::shared_ptr<FtInterface> fti) { function2(fti); } int main() { boost::shared_ptr<FtInterface> fti(ftGetIfFromFactory(...)); function1(fti); } Do you see that "boost::shared_ptr<FtInterface>" appears everwhere I wrote "some_type" previously in this version and not in yours?
What would be the benefit of using non-const references?
I told you that already. You don't need pointers (since you're using a factory function to create the instance, you'll need one pointer, but the other uses don't have to be pointers). void function1(FtInterface & fti) { function2(fti); } int main() { std::auto_ptr<FtInterface> fti(ftGetIfFromFactor(...)); function1(*fti); } The bottom line is that your problems stem not from boost::shared_ptr usage but from fundamental misunderstandings of how C++ works. The Boost Users List or comp.lang.c++.moderated are better places to handle such questions. It also appears that you need to find a good tutorial on C++ to help you understand things better. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

From: David Abrahams <dave@boost-consulting.com>
I do. I thought the context was appropriate in that case. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

Thank you for this. Question tho: when function1 is in a different .cpp file than main(), I would still have to declare FtInterface in that .cpp file, right?
As I pointed out when I started this thread, I'm not very experienced and have been using about 6 different (online) tutorials about C++. Thank you for your help. H.

From: H <h.v.niekerk@hccnet.nl>
FtInterface.h: #ifndef _included_FtInterface_h #define _included_FtInterface_h struct FtInterface { ... }; #endif function1.cpp: #include "FtInterface.h" void function1(FtInterface & fti) { function2(fti); } main.cpp: #include <memory> #include "FtInterface.h" int main() { std::auto_ptr<FtInterface> fti(...); function1(*fti); }
I was confirming that the problem wasn't with shared_ptr, but was more fundamental. BTW, don't look at the implementation of Boost libraries to help you figure out C++. They are too advanced for that purpose.
Thank you for your help.
No problem. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

H <h.v.niekerk@hccnet.nl> writes: <snip long quoted section>
That might be more appropriate, but on whichever group you choose to post, please try to limit the amount of quoted text in your postings. -- Dave Abrahams Boost Consulting www.boost-consulting.com

From: H <h.v.niekerk@hccnet.nl>
Yep. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;
participants (5)
-
Caleb Epstein
-
David Abrahams
-
H
-
Martin Bonner
-
Rob Stewart