Thread: Problem with stringstream

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Problem with stringstream

    Hallo,

    When I add the following function to my program it crashes. I dont even need to use the function to make it crash. I compiles fine. Any ideas on what I am doing wrong?

    Code:
    std::string intToString(int n)
    {
    	std::stringstream out;
    	out << n;
    	std::string a = out.str();
    
    	return a;
    }
    Here is the error message I get:
    Unhandled exception at 0x004898a6 in HelloWorld.exe: 0xC0000005: Access violation reading location 0xcccccca4.
    And it points me to this code, which is found in xiosbase at line 372
    Code:
    	fmtflags __CLR_OR_THIS_CALL flags() const
    		{	// return format flags
    		return (_Fmtfl);
    		}
    Thanks

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Can you post more code?
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Code:
    #ifndef COMMONMATH_H
    #define COMMONMATH_H
    
    #define PI 3.14159265
    
    // Header files
    // ----------------
        #include <math.h>
    	#include <iostream>
    	#include <string>
    	#include <sstream>
    
    
    // Vector2D class:
    class Vector2D
    {
    // Class stuff
    };
    
    float Dot(Vector2D v1, Vector2D v2);
    
    std::string intToString(int n);
    
    #endif
    And
    Code:
    #include "CommonMath.h"
    
    
    // All the class stuff
    //...
    //...
    
    float Dot(Vector2D v1, Vector2D v2)
    {   
    //stuff
    }
    
    std::string intToString(int n)
    {
    	std::stringstream ss;
    	ss << n;
    	std::string a = ss.str();
    
    	return a;
    }
    The accuall function is not used in my program, but it still crashes.

    Let me know if you want any more code

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The code looks fine.

    However, by the description of the symptoms, (the code is added, but not called, and your program blows up), this sounds like a typical stack overlay. Perhaps the overlay is not visible until such a point in time that the stack gets rearranged a little bit by the addition of this function.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here is a string stream I use to convert from a string to an int:

    Code:
    template<typename T> 
    T mystr_to_num(const string &s) { 
    	stringstream ss ; 
    	ss << s ; 
    	T temp ; 
    	ss >> temp ; 
    	
    	if ( ss.fail() ) { 
    		string sfail = "Unable to convert " ; 
    		sfail += s ; 
    		sfail += " to a valid number." ; 
    		f.error_count++ ; 
    		throw(sfail) ; 
    	}
    	return (temp) ; 
    }
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    However, by the description of the symptoms, (the code is added, but not called, and your program blows up), this sounds like a typical stack overlay. Perhaps the overlay is not visible until such a point in time that the stack gets rearranged a little bit by the addition of this function.
    What does that really mean?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >What does that really mean?
    The bug is not in the code you posted.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    are you aware that the same could be written a little bit shourter?

    Code:
    	std::istringstream ss(s) ; 
    	T temp ; 
    	ss >> temp ;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Thanks vart. Yeah, I remember reading about all the string initializers at some point. They're pretty flexible.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    For some weird reason my program crashes as soon as I use the >> on a stringstream. I just uses itoa() instead

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For some weird reason my program crashes as soon as I use the >> on a stringstream.
    Any specific example?

    I just uses itoa() instead
    itoa() is non-standard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Quote Originally Posted by laserlight View Post
    Any specific example?
    itoa() is non-standard.
    I am not really sure how to make a better example then the one posted above. The program crashed no matter where I enter the stringstream. I have tried using it in a function and in the main part. Both make it crash. If it is in a function, I dont even have to use the function to make it crash.

    If I create a new project stringstream works fine. I have no idea on what I am doing wrong.

    That itoa() is non-standard, does that mean that its a bad idea?

    Thanks for all the replies so far.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And it points me to this code, which is found in xiosbase at line 372
    if you go up the call-stack - what part of your code does it corresponds?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am not really sure how to make a better example then the one posted above. The program crashed no matter where I enter the stringstream. I have tried using it in a function and in the main part. Both make it crash. If it is in a function, I dont even have to use the function to make it crash.

    If I create a new project stringstream works fine. I have no idea on what I am doing wrong.
    That's problematic. I was hoping you could provide the smallest and simplest compilable program that demonstrates the problem, but it appears that you cannot do that by creating a new and simple project.

    You could still try changing a copy of your current program to simplify it as much as possible, and post something we can test with.

    That itoa() is non-standard, does that mean that its a bad idea?
    Yes, in the sense that you cannot guarantee it is available.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I added the function in again to try see in the call stack as vart said, but now I get pointed to a new error. I am almost 100% that the program is the same as it was last night.

    Here is the new error:
    A buffer overrun has occurred in HelloWorld.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program.

    For more details please see Help topic 'How to debug Buffer Overrun Issues'.
    And it points to this code in dbghook.c at line 60
    Code:
    __declspec(noinline)
    void __cdecl _CRT_DEBUGGER_HOOK(int _Reserved)
    {
        /* assign 0 to _debugger_hook_dummy so that the function is not folded in retail */
        (_Reserved);
        _debugger_hook_dummy = 0;
    }
    I am not really sure how the call stack work and which part of it you need to see, so here is the whole:
    > HelloWorld.exe!_crt_debugger_hook(int _Reserved=1) Line 62 C
    HelloWorld.exe!__report_gsfailure() Line 298 + 0x7 bytes C
    HelloWorld.exe!_XInputSetState@8() + 0x363b bytes
    ntdll.dll!76ef100b()
    [Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
    ntdll.dll!76eb29d7()
    ntdll.dll!76ef0e97()
    HelloWorld.exe!std::basic_ostream<char,std::char_t raits<char> >:perator<<() + 0x120 bytes
    HelloWorld.exe!HAPISPACE::ToString<unsigned int>() + 0x85 bytes
    HelloWorld.exe!HAPISPACE::CHapiRendererDirect3D::L oadTexture() + 0x8e0 bytes
    HelloWorld.exe!HAPISPACE::CHapiCore::LoadTexture() + 0x55 bytes
    HelloWorld.exe!Sprite::loadSprite(std::basic_strin g<char,std::char_traits<char>,std::allocator<char > > fileName="Data\Ui\Ui1.tga", bool alpha=false, bool animated=false) Line 19 + 0x39 bytes C++
    HelloWorld.exe!Visualisation::loadSprite(std::basi c_string<char,std::char_traits<char>,std::allocato r<char> > fileName="Data\Ui\Ui1.tga", int & spriteId=-842150451, bool alpha=false, bool animated=false) Line 54 + 0x2c bytes C++
    HelloWorld.exe!Ui::loadUi() Line 28 C++
    HelloWorld.exe!GameWorld::setup() Line 19 C++
    HelloWorld.exe!HAPI_Main() Line 22 C++
    HelloWorld.exe!HAPI_EntryPoint() + 0x16f bytes
    HelloWorld.exe!_WinMain@16() + 0xcf bytes
    HelloWorld.exe!__tmainCRTStartup() Line 263 + 0x2c bytes C
    HelloWorld.exe!WinMainCRTStartup() Line 182 C
    kernel32.dll!75e03833()
    ntdll.dll!76eca9bd()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stringstream to float problem
    By c0d3m0nk3y in forum C++ Programming
    Replies: 10
    Last Post: 02-01-2008, 04:07 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM