FileStreamDataStream crashes on close

Problems building or running the engine, queries about how to use features etc.
Post Reply
simedj
Goblin
Posts: 262
Joined: Fri Nov 18, 2011 6:50 pm
x 3

FileStreamDataStream crashes on close

Post by simedj »

I think I'm missing something silly...

Code: Select all

int _tmain(int argc, _TCHAR* argv[])
{
	std::fstream fstream(testInputFile.c_str(),fstream::in | fstream::out);
	FileStreamDataStream stream(&fstream);

	cout<<stream.getAsString();
	stream.close();
	return 0;
}
It works but crashes on stream.close, or if I comment that line (expecting destructors to tidy up automatically) it crashes on exit.

1)What did I miss?
2)Is this the right way to get a DataStream from a file? It seems there should be a shortcut but I couldn't see one.
Looking to find experienced Ogre & shader developers/artists. PM me with a contact email address if interested.
User avatar
runfrodorun
Gremlin
Posts: 154
Joined: Fri Jun 24, 2011 3:50 am
Location: 192.168.0.1
x 3

Re: FileStreamDataStream crashes on close

Post by runfrodorun »

Problem number 1: This isn't a problem but I don't like it; you are naming your variable the same thing as a class or namespace. I try to avoid that in general. (i.e. variables like fstream are not very descriptive/possibly risky/not very readable.)

2. try the member read( char*, size_t ) in fstream. Also exists in FileStreamDataStream

3. This is a general personal problem of mine, but I try to stay away from most std:: crap, with occasional exceptions for std::string and STL (used properly). believe it or not, I find a lot of the C++ wrapper classes to actually confuse things more rather then simplify them.

Short answer:

if I were asked to read a file, I would use good old fopen and fclose. Check out the interwebs for examples. I tend to not use a lot of ogre things like FileStreamDataStream because I see ogre as a rendering library, not a complete API package that does things like this for me... same story for Ogre::String (not taking a shot at the Ogre API here, I think it's rather well done especially for free software) I use ogre every day. Good luck!
Sass not tolerated
LAY DOWN THE LAW!
When I don't understand a problem, I punch it in the face and try again.
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: FileStreamDataStream crashes on close

Post by bstone »

This is a general personal problem of mine but I don't like people calling STL crap without even bothering to understand and learn it first :wink: Ogre::String is just a typedef by the way, not a wrapper class and has nothing to do with the API package. It's just a clever way of keeping the code consistent and ready for changes (from replacing string allocators to substituting for another strings implementation completely with just a few lines changed). But I guess you have to spend a few years maintaining large projects the right way to see that.

Anyway, back to the problem. It all boils down to ownership - FileStreamDataStream takes ownership of the stream. That means you can't pass a pointer to a stack allocated stream to its constructor because FileStreamDataStream's destructor will delete the stream and crash since the latter has been allocated on the stack and not on the heap. The proper code would be:

Code: Select all

int _tmain( int argc, _TCHAR* argv[] )
{
    FileStreamDataStream stream( OGRE_NEW_T( std::fstream, MEMCATEGORY_GENERAL )( testInputFile.c_str(), fstream::in | fstream::out ) );

    cout << stream.getAsString();
    stream.close();

    return 0;
}
simedj
Goblin
Posts: 262
Joined: Fri Nov 18, 2011 6:50 pm
x 3

Re: FileStreamDataStream crashes on close

Post by simedj »

Thanks bstone, this makes sense (the code, I'll stay out of the arguing ;))

Although I can't bear to use OGRE_NEW_T everywhere, but that's a whole other discussion for another day!
Looking to find experienced Ogre & shader developers/artists. PM me with a contact email address if interested.
simedj
Goblin
Posts: 262
Joined: Fri Nov 18, 2011 6:50 pm
x 3

Re: FileStreamDataStream crashes on close

Post by simedj »

runfrodorun wrote:Problem number 1: This isn't a problem but I don't like it; you are naming your variable the same thing as a class or namespace
Good point, but this is just a quick test I threw together.
if I were asked to read a file, I would use good old fopen and fclose. Check out the interwebs for examples. I tend to not use a lot of ogre things like FileStreamDataStream because I see ogre as a rendering library, not a complete API package that does things like this for me...
Ogre uses DataStream class internally for resource loading. If you want to modify/extend/reuse any of that you have no option. Ogre is described as 'only a rendering engine' but this isn't true, it is also a resource manager. It's abstracted so it can load from filesystem, ZIP archive or anything else you want to implement (network loading). It allows you to have a more fine-grained approach to which data is held in memory at any time, etc.

If you are using Ogre you don't need to bother and I'd agree that using Ogre functions to load files is foolish... but if you're working below the surface you have to use the internal API.
Looking to find experienced Ogre & shader developers/artists. PM me with a contact email address if interested.
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: FileStreamDataStream crashes on close

Post by bstone »

simedj wrote:Thanks bstone, this makes sense (the code, I'll stay out of the arguing ;))

Although I can't bear to use OGRE_NEW_T everywhere, but that's a whole other discussion for another day!
You're welcome. You don't have to use OGRE_NEW_T everywhere in your code - only when interfacing with Ogre classes. Or you can drop it completely for the price of not using an optimized memory allocator - simply compile Ogre with OGRE_MEMORY_ALLOCATOR = OGRE_MEMORY_ALLOCATOR_STD.
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: FileStreamDataStream crashes on close

Post by bstone »

simedj wrote:If you are using Ogre you don't need to bother and I'd agree that using Ogre functions to load files is foolish... but if you're working below the surface you have to use the internal API.
That's a funny thing to say because you don't have to use those functions if you don't want to. Furthermore, I'm sure one of the reasons you opted for using Ogre is that it provides a lot of functionality besides the pure rendering API and lots of tools based on that extra functionality (e.g. 3dsMax exporters) - otherwise you'd stick to OpenGL or D3D :)
simedj
Goblin
Posts: 262
Joined: Fri Nov 18, 2011 6:50 pm
x 3

Re: FileStreamDataStream crashes on close

Post by simedj »

I have to use those functions and data-structures if I want to interact with Ogre functionality. But I wouldn't use Ogre classes every time I simply want to read from a file for my application, anymore than I would use Ogre::String through my application... (I know String is a special case)... it's better to keep my code distinct from Ogre where possible.

i.e. use Ogre functions for "Ogre-y" stuff, but keep application logic separate from Ogre where possible. Avoid unnecessary dependencies, etc.
Looking to find experienced Ogre & shader developers/artists. PM me with a contact email address if interested.
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: FileStreamDataStream crashes on close

Post by bstone »

Makes sense. But what I mean is that you could go "bare bones" if needed, i.e. create meshes/materials/textures manually and load some raw data from your files into them. Ogre exposes all related API in case you really want to go hardcore. That is, you wouldn't need DataStream and all related stuff. But the amount of time spent setting all the low-level bits together would cost you an arm. Then testing and debugging all that would cost you a leg. It's wiser to leverage the man-hours invested in the "unneeded non-rendering API" if you ask me (even if you have to type a few OGRE_NEW_T invocations occasionally).
simedj
Goblin
Posts: 262
Joined: Fri Nov 18, 2011 6:50 pm
x 3

Re: FileStreamDataStream crashes on close

Post by simedj »

If it's a big chunk of functionality I probably would. For something as simple as reading a text file, it's overkill IMO - the Ogre setup with Datastreams is more complex since it abstracts different datasource types. It's choosing where to draw the line...
Looking to find experienced Ogre & shader developers/artists. PM me with a contact email address if interested.
User avatar
runfrodorun
Gremlin
Posts: 154
Joined: Fri Jun 24, 2011 3:50 am
Location: 192.168.0.1
x 3

Re: FileStreamDataStream crashes on close

Post by runfrodorun »

bstone wrote:This is a general personal problem of mine but I don't like people calling STL crap without even bothering to understand and learn it first :wink: Ogre::String is just a typedef by the way, not a wrapper class and has nothing to do with the API package. It's just a clever way of keeping the code consistent and ready for changes (from replacing string allocators to substituting for another strings implementation completely with just a few lines changed). But I guess you have to spend a few years maintaining large projects the right way to see that.
Your point is valid, but if you don't mind I'm a C++ expert as well: What I say next is not meant in offense to you. I know exactly how most of the standard libraries work and there's a reason I prefer the C specification over most of the C++ wrapper classes, and in all the years that I've been coding, I have always disliked libraries that I use managing which wrapper implementations that I use. Now I admit, I was not aware that this is how Ogre::String worked, but it does not make me any more inclined to use it. It suffices for me to typecast c strings to Ogre::String when interacting with the API and keeping them vanilla otherwise. I don't understand how that could ever go wrong, even with localization and typesets bigger then 1 byte.
Sass not tolerated
LAY DOWN THE LAW!
When I don't understand a problem, I punch it in the face and try again.
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: FileStreamDataStream crashes on close

Post by bstone »

I don't mind C++ experts, quite the opposite in fact, but let's say even if I don't like C and specifically do dislike C-style coding for a C++ compiler (to me it's like buying a Porsche and driving 20mph max), I never call C crap for that matter.

And why would you typecast vanilla C-strings to Ogre::String by the way? Don't you trust C++ when it comes to implicit type conversions with constructors?
User avatar
runfrodorun
Gremlin
Posts: 154
Joined: Fri Jun 24, 2011 3:50 am
Location: 192.168.0.1
x 3

Re: FileStreamDataStream crashes on close

Post by runfrodorun »

haha; It sounds crazy, but the way I was taught to code years ago is that if it can be more explicit it should be. When a function takes an std::string for example, I'll pass in:

Code: Select all

const char* str = "example";
func( std::string( str ) );
Reasoning: I've had more then one job where the compiler we were given was pathetic and didn't do anything for you. I did a bunch of work with windriver vxworks a few years back, and that compiler did not do jack if you didn't baby it through everything. Example:

Code: Select all

void func( const int& rc_data );
One of the guys I worked with made the mistake of invoking the function like this:

Code: Select all

func( 5 );
Now while that does usually pop up a warning, with most modern compilers that implies allocating an integer on the stack and then passing the reference. Not with this one. It just gave 4 bytes at memory location 5. Terrible. This compiler wouldn't even pop an error if you forgot to implement functions etc. It was a nightmare to work with, so ever since then I don't leave little things up to the compiler. Call me paranoid, but I don't trust it and I think that it's always better to not leave too many things implied. There's my story.

'Cool story bro' :P :(
Sass not tolerated
LAY DOWN THE LAW!
When I don't understand a problem, I punch it in the face and try again.
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: FileStreamDataStream crashes on close

Post by bstone »

Yeah, those are some funny snippets. But don't be that paranoid - saves a lot of typing. :D
loath
Platinum Sponsor
Platinum Sponsor
Posts: 290
Joined: Tue Jan 17, 2012 5:18 am
x 67

Re: FileStreamDataStream crashes on close

Post by loath »

if you don't want FileStreamDataStream to own the std::fstream instance you can pass 'false' into the constructor. at least in ogre 1.8.

ps. if you're just reading, you might want to remove the fstream::out.

Code: Select all

int _tmain(int argc, _TCHAR* argv[])
{
   std::fstream fstream(testInputFile.c_str(),fstream::in);
   FileStreamDataStream stream(&fstream, [b][u]false[/u][/b]);

   cout<<stream.getAsString();
   stream.close();
   return 0;
}
Post Reply