Assigning smart/shared_ptr pointer to C struct

Greetings... What is wrong in assigning a smart/shared_ptr pointer in C++ code to a struct which may then be passed to a C code? Consider the sample source towards the end of this email. When I run this through valgrind (memory debugger, see http://www.valgrind.org), it points out potential problem (also listed after the sample code). It also seems to cause memory corruption. What may be a better way of saving/storing a smart/shared_ptr pointer in a C struct (and yet have the reference counter in the smart/shared_ptr pointer increment)? Best regards, -Arun. --------------------- The sample program -------------------- #include <boost/shared_ptr.hpp> class A { public: int val; }; typedef boost::shared_ptr<A> APtr; struct S { public: APtr aptr; }; struct S *func(void) { A *a = new A; a->val = 12; APtr aptr(a); struct S *s = (struct S *)malloc(sizeof(struct S)); s->aptr = aptr; return s; } int main(int ac, char *av[]) { struct S *s; s = func(); APtr aptr = s->aptr; } ------------------------------------------------------ Output from valgrind: Conditional jump or move depends on uninitialised value(s) at 0x8048A20: boost::detail::shared_count::operator= (boost::detail::shared_count const&) (shared_count.hpp:179) by 0x80487FD: boost::shared_ptr<A>::operator= (boost::shared_ptr<A> const&) (shared_ptr.hpp:148) by 0x80486E8: func() (c.cpp:22) by 0x8048742: main (c.cpp:30)

adharankar@verizon.net wrote:
class A { public: int val; }; typedef boost::shared_ptr<A> APtr;
struct S { public: APtr aptr; };
struct S *func(void) { A *a = new A; a->val = 12;
APtr aptr(a);
struct S *s = (struct S *)malloc(sizeof(struct S));
s->aptr is uninitialized here because you haven't invoked the constructor of S yet. s now points to raw bytes, not to an object of type S. Use new( s ) S; to construct an object in this storage, or better yet, just use S * s = new S;
s->aptr = aptr; return s; }

adharankar@verizon.net wrote:
What is wrong in assigning a smart/shared_ptr pointer in C++ code to a struct which may then be passed to a C code?
There is no safe way to pass any non-POD type to C code. In your example, this means both the shared_ptr object and the pointed-to A object. You need to ensure the lifetime of the pointer manually, and you need to extract the raw pointer and pass that to the C code. You also need to make it point to a POD type. Sebastian Redl

Hello, The answer which Peter Dimov provided (http://lists.boost.org/boost-users/2006/03/17717.php) is correct, at least the way I wanted it to. Also, Valgrind does not complain any more with the change Peter Dimov suggested. Thanks for responding! Best regards, -Arun. -------- Original Message -------- From: Sebastian Redl <sebastian.redl@getdesigned.at> To: boost-users@lists.boost.org Subject: Re:[Boost-users] Assigning smart/shared_ptr pointer to C struct Date: 3/3/2006 02:57
adharankar@verizon.net wrote:
What is wrong in assigning a smart/shared_ptr pointer in C++ code to a struct which may then be passed to a C code?
There is no safe way to pass any non-POD type to C code. In your example, this means both the shared_ptr object and the pointed-to A object. You need to ensure the lifetime of the pointer manually, and you need to extract the raw pointer and pass that to the C code. You also need to make it point to a POD type.
Sebastian Redl _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
adharankar@verizon.net
-
Arun Dharankar
-
Peter Dimov
-
Sebastian Redl