Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

OleKaiwalker

Memory errors

This topic is 5305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, How do you handle memory errors?? Where memory arn''t allocated or maybe a container is exceeding its bounds? ex...
char &Alloc::GetPartOfChunk(unsigned int index){

	//if (index >= chunkSize)

		// Throw an exception here


	return *(memoryPool + (chunkIndex + index));
}

Share this post


Link to post
Share on other sites
Advertisement
I usually do it this way:


someData& getSomething(unsigned int index)
{
#ifdef ENABLE_THROW_OUTOFRANGE
if(index >= size) throw OutOfRangeException("some message")
#endif
return *(data+index);
}



Share this post


Link to post
Share on other sites
class OutOfRangeException : public MyExceptionBase
{
public:
OutOfRange(std::string str=std::str("")):msg(str){}
std::string msg;
};
Note for pedants: if you want msg to be private and write accessors, be my guest.

Angry forgot a semi-colon at the end of his throw.

[edited by - flangazor on April 13, 2004 12:46:19 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by OleKaiwalker
Thx...

But shouldn''t such an error close the the application?


If I use your library and it closes down my application I won''t be happy.

Share this post


Link to post
Share on other sites
Not if you handle the exception yourself. You could always do something like:


try
{
Game-Init();
while (Game->IsRunning())
Game->Main();
Game->Shutdown();
}
catch (MyExceptionBase)
{
// Exception handle code

}
catch (...)
{
MessageBox(NULL, "Unhandled exception was thrown of unknown type. Application will be terminated", "My Error Code", MB_OK | MB_ICONSTOP);
ExitApp();
}


Toolmaker



My site
/* -Earth is 98% full. Please delete anybody you can.*/

Share this post


Link to post
Share on other sites
quote:

class OutOfRangeException : public MyExceptionBase{public:OutofRange(std::string str=std::str("")):msg(str){}std::string msg;};



Never use strings! Use const char arrays(const char*), strings can cause exceptions by themself, and we dont want an exception on an exception..

quote:

Angry forgot a semi-colon at the end of his throw.


since we're being picky, you named your constructor wrong

[edited by - angry on April 13, 2004 12:27:29 PM]

Share this post


Link to post
Share on other sites
What exceptions do you think a std::string can throw and what would cause those?

From what I gather, Herb Sutter is saying on pages 59-60 of Exceptional C++ that you are wrong. Maybe I am misreading him.

FWIW, I should have passed by const &.

[edited by - flangazor on April 13, 2004 12:56:14 PM]

Share this post


Link to post
Share on other sites
I think it can throw an out of resource exception, i.e memory is out. Or maybe it doesn''t allocate memory for it''s strings?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!