LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2005, 12:46 PM   #1
SCOSWriter
Member
 
Registered: Dec 2004
Posts: 57

Rep: Reputation: 15
Does ostringstream or stringbuf have a buffer max?


I was attempting to parse an XML file and store an XML node into a ostringstream object until I was finished with the node.

Each Node I'm storing is made up of about 8 other nodes, totaling on an average of 5000 bytes of data.

While storing each sub-node. for example.

my_oss_object << new_node

the operation seg faults.

Sometimes I get further in the process depending on whether I use std::strings or raw char*'s.

new_node is either std::string or a null termed char*, and each nodes memory is copied (ie mem, strcpy, or string( ) l) from the char* returned from Expat (XML parser)

I've verified the existence of new_node in many ways. First by

cout << new_node

which was fine and then

cout << my_oss_object.str()

which was fine. Performing

my_oss_object << new_node

seems to be a problem. Its as though it ran out of memory, and with no exception thrown anywhere it just does not make sense.


I tried all the methods such as flush, good, fail. Neither make a difference. I have also tried initalizing my_oss_object's stringbuf to be unbuffered ie.:

my_oss_object.rdbuf()->pubsetbuf(NULL,0);

This had no apparent effect as well.

I use the kdevelop IDE, and its GDB de-bugger inteface so its usually pretty obvious when somethings not right, but in this case nothing is apparently wrong. I use gcc 3.3, libstdc++ 3.3-23, and glib 2.3.2-6.

Has anyone else had this type of behavior before while using stringbuf's or ostringstream?

I suppose I could just try using a accumulating string object, but I really would like to get this to work as it is.

Last edited by SCOSWriter; 08-16-2005 at 12:48 PM.
 
Old 08-17-2005, 06:29 AM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
You might want to check the the size is dynamic, also make sure theres been no calls to freeze() and/or str(), ( str() calls freeze ).
 
Old 08-17-2005, 06:55 AM   #3
SCOSWriter
Member
 
Registered: Dec 2004
Posts: 57

Original Poster
Rep: Reputation: 15
A call is made to str() at the end of the processing, to retrieve the accumulated nodes, although the program never makes it that far.

Additionally, you are thinking of std::strstream<-ostrstream and not stringstream<--ostringstream. If I were using this depricated version I would certainly use static allocation.

stringstream use the std::string class as a buffer.

I just about gave up on using ostringstream. I went with an accumulating std::string through concatnation, go figure this did not work either (after all it is used as ostringstreams buffer), a seg fault occurs again a the same node to append.

This all very strange.

I abondoned this and went with lower level memory allocation and memcopy using char*'s. This works. Suprisingly it really was not much more code, and probably more efficient. The program is small so I'm pretty confident the Valgrinds check is accurate.

I'd prefer this to be a temporay solution. I don't like using such conventions because they error prone. In fact the company I work for has coding standards that prohit such, unless absolutly needed.
 
Old 08-17-2005, 05:08 PM   #4
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
If you move that particular node further along the file, just as a test, does it still segfault on that node, or at its original position.

i.e. is it a memory problem, or a problem with that node?

Moving to char's obviously solved your problem, but that doesn't neccsarily mean that it was something to do with allocation.
 
Old 08-18-2005, 05:48 AM   #5
SCOSWriter
Member
 
Registered: Dec 2004
Posts: 57

Original Poster
Rep: Reputation: 15
Not sure never tried that, but its a good idea. I'll try it.

Bear in mind that Expat lib is parsing the file for me and I'm simply using its element handlers.
 
Old 08-18-2005, 06:30 AM   #6
SCOSWriter
Member
 
Registered: Dec 2004
Posts: 57

Original Poster
Rep: Reputation: 15
Since Expat is doing the processing/verification of XML I see it very unlikly that the problem is with the node. But to be sure, I simply inserted a copy of the last added node. into the position of the nod ethat usaully caused the seg fault.

Then in parallel with raw memory alocation, I also used an ostringstream to buffer/accumulate new XML nodes.

A memory problem would indicate a similar seg fault on this node instead. Funny thing is that I could not produce any sort of seg fault when using an ostringstream object

Perhaps something was fixed... maybe a machine reboot helped...who knows. Since moving to raw memory aloc., I'm not sure I can revert back to reproduce the problem and I'm not going to.

Valgrind checks out ok, so I'm going to freeze this version.

Pitty, I would have liked to know what was happening. If it was something not caused by me it may had been beneficial to the community.
 
Old 08-18-2005, 12:48 PM   #7
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
Those type of bugs are the most annoying, had them myself. What will really get you though is when you do something similar and the bug reappears, since you don't what you did that fixed it the first time, your're stumped...

Glad you got it working though.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Max uptime? driftingremains Linux - General 46 12-28-2013 07:20 AM
IO Buffer Haafiz Linux - General 1 07-14-2004 03:24 PM
Max Possible IDE items, Max IDE interfaces? KendersPlace Linux - Hardware 2 08-01-2003 06:58 PM
Max Swap PionexUser Linux - Newbie 3 07-18-2003 09:17 AM
can not open 3d max shitatam Linux - Software 0 08-07-2002 02:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration