473,554 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::string Destructor Throwing Exception

Hi, all,

I've a fairly small piece of code that is causing me problems. I'm using
std::string and am building a string of several dozen characters using
several of std::string's functions: a blank constructor, operator=, and
operator+=. This sequence of function calls causes a crash either at the
call to operator+= or when the string object leaves scope (which must be the
result of the destructor). Specifically, my Windows-based system reports
that an exception was not caught in some heap management code. This crash
occurs only in some binaries, and specifically not the small one I created
to post here.

After a lot of investigation, I determined that its operator+= that is the
culprit. Through the addition of a call to reserve() I have been able to
avoid the unhandled exception and subsequent crash (both the operator+= and
destructor crashes). My first thought was that these dumbasses (the
providers of my STL library; the obvious people) have a bug in
string::operato r+=. However, then I realized that I know far to little
about C++ to get all high-and-mighty and assume that a library provider gave
me a buggy version of the STL.

So, my question to the group is this: what kind of misuse of std::string,
using only operator=, and operator+= could cause exceptions to be raised in
operator+= or the destructor? What combination of these functions is
illegal according to the language specifications? What requirements are
incumbent upon the user in terms of memory management? It was my
understanding that the beauty of using std::string was that I shouldn't have
to worry about memory management. reserve() could be called to help out,
but no special consideration is required of the user to inform the object of
its intended size.

Have I misunderstood something here? Since my strings are all automatic
variables, managed on the stack, and no other function calls occur in
between the object's creation and the unhandled exception, I can eliminate
the possibility of some of *my* other code corrupting the string, right?

Scott

--
Remove .nospam from my e-mail address to mail me.

http://www.e-scott.net
Jul 23 '05 #1
3 5035

"Scott Brady Drummonds" <sc************ **********@inte l.com> wrote in
message news:d1******** **@news01.intel .com...
Hi, all,

I've a fairly small piece of code that is causing me problems. I'm using
std::string and am building a string of several dozen characters using
several of std::string's functions: a blank constructor, operator=, and
operator+=. This sequence of function calls causes a crash either at the
call to operator+= or when the string object leaves scope (which must be
the
result of the destructor). Specifically, my Windows-based system reports
that an exception was not caught in some heap management code. This crash
occurs only in some binaries, and specifically not the small one I created
to post here.

After a lot of investigation, I determined that its operator+= that is the
culprit. Through the addition of a call to reserve() I have been able to
avoid the unhandled exception and subsequent crash (both the operator+=
and
destructor crashes). My first thought was that these dumbasses (the
providers of my STL library; the obvious people) have a bug in
string::operato r+=. However, then I realized that I know far to little
about C++ to get all high-and-mighty and assume that a library provider
gave
me a buggy version of the STL.

So, my question to the group is this: what kind of misuse of std::string,
using only operator=, and operator+= could cause exceptions to be raised
in
operator+= or the destructor? What combination of these functions is
illegal according to the language specifications? What requirements are
incumbent upon the user in terms of memory management? It was my
understanding that the beauty of using std::string was that I shouldn't
have
to worry about memory management. reserve() could be called to help out,
but no special consideration is required of the user to inform the object
of
its intended size.

Have I misunderstood something here? Since my strings are all automatic
variables, managed on the stack, and no other function calls occur in
between the object's creation and the unhandled exception, I can eliminate
the possibility of some of *my* other code corrupting the string, right?


Post the code you're having problems with. We'd just be guessing, given the
information you have here. (The statement that you use a "blank
constructor" is especially confusing. Whose constructor is "blank"? Are
you trying to inherit from std:string, or...?)

-Howard

Jul 23 '05 #2
Scott Brady Drummonds wrote:
I've a fairly small piece of code that is causing me problems. I'm using
std::string and am building a string of several dozen characters using
several of std::string's functions: a blank constructor, operator=, and
operator+=. This sequence of function calls causes a crash either at the
call to operator+= or when the string object leaves scope (which must be the
result of the destructor). Specifically, my Windows-based system reports
that an exception was not caught in some heap management code. This crash
occurs only in some binaries, and specifically not the small one I created
to post here.
And were is that small one you created to post here? What stopped you?
[...] My first thought was that these dumbasses (the
providers of my STL library; the obvious people) have a bug in
string::operato r+=. However, then I realized that I know far to little
about C++ to get all high-and-mighty and assume that a library provider gave
me a buggy version of the STL.
Actually, there have been some corrections Dinkumware posted on their site
for the Standard Library implementation they licensed to MS in 1996. You
should check them out. If you're using VC++ v6, that is. You can always
switch to a different implementation, like STLport, for example, and see
if the trouble goes away.
[...]
Have I misunderstood something here? Since my strings are all automatic
variables, managed on the stack, and no other function calls occur in
between the object's creation and the unhandled exception, I can eliminate
the possibility of some of *my* other code corrupting the string, right?


No, there is always a possibility of that. Post your code and we will
help you eliminate those from the list of suspects.

V
Jul 23 '05 #3
Scott Brady Drummonds wrote:
...


The crash symptoms that you describe indicate that at some point the
integrity of the heap has been destroyed. It does not necessary happen
due to some error in 'std::string's compound assignment operator or
destructor. My guess is that most likely some portion of _your_ code
damaged the heap before (maybe, _long_ before) the moment when the
actual exception in 'std::string's methods took place (and the fact that
you can't reproduce the problem in a smaller piece of code indirectly
supports that guess). The fact that using 'std::string::r eserve' seems
to help doesn't really mean anything, since problems like this are very
easy to shoo away by almost any change in heap-related code. They will
re-appear later in some other completely unrelated place, like
'std::vector's destructor, for example.

--
Best regards,
Andrey Tarasevich

Jul 23 '05 #4

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

Similar topics

10
8152
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That...
24
11010
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include <string> #include <vector> #include <cstring> // presume s from somewhere, such as:
22
13218
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; char c; std::string str;
2
1790
by: Susan Baker | last post by:
Hi, I have declared a class MyClass in a header file MyClass.h I have then gone onto define the class in MyClass.cpp. This is (roughly) what the definition (.cpp) file looks like: #include "BaseHeader.h" /****************************************************/ /* Constructors/Destructor */
3
2327
by: Mathieu Malaterre | last post by:
Hello, I have currently a piece a code which look like this: foo.h ------------------- extern const std::string NotFound; foo.cxx
6
11484
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for example string str1; as a class member and then add text to it. or do I have to specify it's length when defining? 2. How to convert from std::string...
24
17446
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET managed C++. It seems to work fine, but everyone knows that programs with errors can still appear to "work fine" :) I am working with VS .NET...
6
4847
by: SteelSide | last post by:
Ive searched and searched,but havent been able to get it to work. ----------------- #include <iostream> using namespace std; std::string tempHostNameStr = "s1e2.hidden.thingy.org"; char szHostName;
3
3213
by: doubts | last post by:
Hi all, I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net. when using std::string type variable, the application causes exception at one instance and does not cause an exception at other. i have two functions in the same .cpp file //exception occurs at this function void call(const char* var2 ) {
0
7605
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7808
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6156
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5162
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3556
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2022
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1141
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
845
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.