Jump to content
  • Advertisement
Sign in to follow this  
whitde

c++ - when method inside new fails

This topic is 4848 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

In a class constructor I load data from a file and have found that because the constrctors never return values I can't bail out gracefully when the file doesn't exist. iParticle *p = new iParticle("x.psi"); I made the fatal mistake of testing (p == NULL) which never happens even when it fails loading and returns. Q) Is there a way to make it return NULL back to the calling code after cleaning up? Inside the constructor I had code as below :- psi = iResource::CreateFromFile(filename); if (!psi) { return; } I need something before the return to cleanup and make it return NULL to the calling code. After this I will have to sift through all my code and look for these timebombs.

Share this post


Link to post
Share on other sites
Advertisement
You have two options: you could use an empty constructor that just initializes everything to NULL and then provide an Init (or whatever you want to call it) function that returns an error code, or you could throw an exception in your constructor if something fails (the memory will automatically be cleaned up).

Share this post


Link to post
Share on other sites
Personally i think you could design this in a much better way but if you must do it this way then do exception handling and throw an exception.

Share this post


Link to post
Share on other sites
If you searched the forums instead of just asking, you would have found something similar to this:

#include <new>
#define cnew new(std::nothrow)
#define cdelete(x) {delete(std::nothrow,x);x=0;}
#define cdeleten(x) {delete[](std::nothrow,x);x=0;}

Share this post


Link to post
Share on other sites
Quote:
Original post by whitde
Q) Is there a way to make it return NULL back to the calling code after cleaning up?


No clean way. Throw an exception instead.

Gorax - he wants a null pointer if the constructor fails, not if the memory allocation fails. Once the program is in the constructor, it has left operator new, so no code you put in there* will ever affect how the constructor is handled.


* - short of hacking your stack in ASM. As I said "no clean way".

Share this post


Link to post
Share on other sites
Quote:
Original post by Gorax
If you searched the forums instead of just asking, you would have found something similar to this:
*** Source Snippet Removed ***


That has no relevance to his problem, that just uses nothrow versions operators new/delete for when allocation fails not his code in the constructor.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!