473,378 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Unmanaged C++ object lifetime

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?
Nov 17 '05 #1
3 1127
Bob Altman wrote:
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?
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
Nov 17 '05 #2
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).
Nov 17 '05 #3
Bob Altman wrote:
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).


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
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: MuZZy | last post by:
HI, Say i have a class(on C++) which has an unmanaged STL member of STRMAP which is a define for map<string, string>. class CL { public CL(){mp = new STRMAP()}; public void Dispose(delete...
1
by: Hamad Deshmukh | last post by:
Hey everyone, I have an issue that requires an urgent solution. I had a look at the Technote above, and it states that the AppDomainUnloaded Exception occurs with ASP.NET when using Managed C++...
2
by: The unProfessional | last post by:
In my current project (my first project using vc w/ managed extensions), I'm directly #using <mscorlib.dll>, so it's necessary for me to use the __nogc and __gc constructs when defining classes or...
6
by: Egbert Nierop \(MVP for IIS\) | last post by:
Is it possible to create events without using sendmessage? sample: class a can retrieve a pointer to a member function of class b that instanciates class a? class b, sets the pointer to its...
7
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
2
by: MeGeek | last post by:
Hi, I wanted to develop an unmanaged application of .Net Remoting in VC++. When I tried compiling the object dll, it is giving the following error, "C3625: 'ObjectNamespace::RefObject': an...
20
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post by:
Sorry to bring up a topic that is just flogging a dead horse.... but... On the topic of memory management.... I am doing some file parcing that has to be done as quick as posible, but what I...
9
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.