473,399 Members | 3,656 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,399 software developers and data experts.

Q about object assignment

Yesturday I chased down a memory leak being reported by our leak
detection library. It lead me to question this line of code:

str_instance = std::string();

I changed it to str_instance.clear() and the leak went away.
"str_instance" is an instance variable in a class that had the above
line in one of its member functions. I would get one reported "leak"
for each of the objects instantiated (outer class, not string) even
though I could verify without doubt that all objects were getting
deleted before the memory leak detection thing went about its work.
The memory the leaked pointer was pointing to was the internal char
buffer of a std::string.

It was my understanding that the above line of code was ok, even if
clear() is better anyway. str_instance should get destroyed and then
recreated with the copy constructor...right?

It isn't a big thing. The "leak" is fixed and the fix is the better
way to do what is desired, but it still leaves me with doubt and a
possible confusion about how the above code is supposed to work.

Dec 1 '05 #1
3 1214
On 2005-12-01, ro**********@gmail.com <ro**********@gmail.com>
wrote:
Yesturday I chased down a memory leak being reported by our
leak detection library. It lead me to question this line of
code:

str_instance = std::string();
There should be no resource leak here.

Perhaps you've simply covered up the symptom of some other bug.
Could the representation of str_instance be getting fouled up
somewhere else?
It isn't a big thing. The "leak" is fixed and the fix is the
better way to do what is desired, but it still leaves me with
doubt and a possible confusion about how the above code is
supposed to work.


It seems very unlikely that a std::string implementation would
leak resources in it's assignment operator.

--
Neil Cerutti
Dec 1 '05 #2

Neil Cerutti wrote:
On 2005-12-01, ro**********@gmail.com <ro**********@gmail.com>
wrote:
Yesturday I chased down a memory leak being reported by our
leak detection library. It lead me to question this line of
code:

str_instance = std::string();


There should be no resource leak here.

Perhaps you've simply covered up the symptom of some other bug.
Could the representation of str_instance be getting fouled up
somewhere else?


I don't believe so. I do pass a const reference of the string outside
the class but nobody tries to alter it and there's no const_cast trying
to get rid of its constness. There is only the one member function
where anything is done to that string, it is a cache that holds a
string value of some complex calculations that are meant to be drawn on
the screen.

Perhapse the implementation is doing some sort of fancy memory
management that tries to make string allocation faster. Maybe that
management is leaving pointers around and doesn't get told to delete
them until after the memory leak detection occurs. I have to admit I
don't really know how this detection library is supposed to work and
there are other false positives that we are aware of and ignore.

It's probably a non-issue.

Dec 1 '05 #3

ro**********@gmail.com wrote:
Yesturday I chased down a memory leak being reported by our leak
detection library. It lead me to question this line of code:

str_instance = std::string();

I changed it to str_instance.clear() and the leak went away.


Your leak detector doesn't understand your std::allocator. Your code is
OK,
and your std::allocator probably is OK too. However, similar bugs in
leak
detectors are fairly common.

HTH,
Michiel Salters

Dec 2 '05 #4

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

Similar topics

106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
2
by: Aaron | last post by:
Hi, I've seen javascript code where a constructor function is passed an argument "document", and inside the function itself the assignment "this.document = document;" is made. This is the code...
11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
8
by: nrhayyal | last post by:
hi all, is it a good idea to assign object to pointers without initialising it to NULL? suppose if i have a class TEST class ELEMENT { private: int a; string s1;
3
by: thomson | last post by:
Hi all, Can any one tell me what happens when an object is casted to an interface, somthing bit deeper, the case is i do have an interface Ifunction which has got one method display(), and i...
13
by: Mike S | last post by:
I came across the following paragraph in the "Semantics" section for simple assignment in N1124 (C99 draft) and I'm wondering if I'm interpreting it right: 6.5.16.1p3: If the value being...
14
by: Summercool | last post by:
The meaning of a = b in object oriented languages. ==================================================== I just want to confirm that in OOP, if a is an object, then b = a is only copying the...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.