Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

Unmanaged C++ object lifetime

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Altman - 13 Mar 2005 20:45 GMT
Hi all,

I have a basic question regarding holding references to objects in unmanaged
C++.  Suppose my unmanaged C++ class has a method that accepts a reference
to an object as an argument, and stores the object reference in a stack
object, and another method that uses the stored objects in the stack object,
like this:

 void MyClass::AcceptString(const string& myArg) {
   m_myStack.push(myArg);    // declared as stack<string> m_myStack
 }

 void MyClass::DoSomething() {
   string& s = m_myStack.Top();
   <Do something with s>
 }

Now, suppose this method is called with code that looks like this:

 MyClass t;
 t.AcceptString("Some text");
 t.DoSomething();

As I understand things, the compiler creates a temporary string object
containing "Some text" and passes it to my method.  My question is, what is
the lifetime of that temporary string object?  I assume that nothing in C++
does reference counting or anything like that to keep my private reference
"alive".  Since my method has grabbed a reference to the string, I assume
that I'm at risk of trying to access it after the caller to my method has
deallocated it.  Is this all correct?  To be safe, do I need to store a copy
of the string in the stack object, even though 99.9% of the time the caller
will not destroy the string while I'm using it?
Carl Daniel [VC++ MVP] - 13 Mar 2005 20:48 GMT
> Hi all,
>
[quoted text clipped - 22 lines]
> containing "Some text" and passes it to my method.  My question is,
> what is the lifetime of that temporary string object?

It survives up to the end of the complete statement in which it was created.
In this case, your AcceptString function made a copy of the temporary and
pushed that onto the stack, and the temporary is now gone.

> I assume that
> nothing in C++ does reference counting or anything like that to keep
> my private reference "alive".

Correct.

> Since my method has grabbed a
> reference to the string, I assume that I'm at risk of trying to
> access it after the caller to my method has deallocated it.  Is this
> all correct?

Your method took a reference, but then made a copy of the whole object, so
there's no way you can access the temporary beyond it's lifetime.

> To be safe, do I need to store a copy of the string in
> the stack object, even though 99.9% of the time the caller will not
> destroy the string while I'm using it?

You already are storing a copy.

-cd
Bob Altman - 13 Mar 2005 23:24 GMT
Thanks a million for the quick answer.  That raises the obvious  question of
why a copy of my object is given to stack.push() rather than a reference to
it.  Intellisense tells me that the function signature is:

 void std::stack<string>::push(const std::stack<string>::value_type & _Val)

As I read this gibberish, it looks like my variable is being passed by
reference to the push() routine.  I would have expected the string that was
given to my function by reference to be given to push() by reference, not by
value (which would create a copy of the string).
Arnaud Debaene - 13 Mar 2005 23:49 GMT
> Thanks a million for the quick answer.  That raises the obvious question
> of why a copy of my object is given to stack.push() rather
[quoted text clipped - 6 lines]
> that was given to my function by reference to be given to push() by
> reference, not by value (which would create a copy of the string).

All STL containers are value-based, which means they store copy of the
objects pushed on them. The push method take a reference to an opject and
make  a copy of that object that is put on the stack.

Arnaud
MVP - VC

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.