0

I have an object that gets constructed with another object passed by reference. The objects which is passed to the constructor looks like this:

 HTTPServerResponse::HTTPServerResponse(Poco::Net::HTTPServerResponse &response)

My wrapper class (HTTPServerResponse) destroys response at some point but it doesn't also destroy itself. Actually there are multiple (external) reasons for which response might get destroyed, even outside my class.

What I want is prior to calling any other methods on response check its existence.

I tried:

if(!response) {...

Which produced error C2675: unary '!' : 'Poco::Net::HTTPServerResponse' does not define this operator or a conversion to a type acceptable to the predefined operator; Naturally, I also tried:

if(response) {...

Which also failed with error C2451: conditional expression of type 'Poco::Net::HTTPServerResponse' is illegal; No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

How do I fix this mess?

Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72
  • 3
    You cannot know if an object was deleted/released. Consider making a better design. – Daniel May 28 '12 at 16:19
  • @Pubby can you please provide an example as answer? – Silviu-Marian May 28 '12 at 16:19
  • With a pointer he won't get NULL if the object has been deleted. I agree with @Dani – Adriano Repetti May 28 '12 at 16:20
  • 2
    You can't call methods on a deleted object (and you'll get undefined behavior if you try), but if it's your own code that does the deletion, you can set a flag (or set the corresponding pointer to NULL) to indicate to yourself that you deleted it. Your code can then check the state of that flag/pointer before trying to access the object. If the object can be deleted without your knowledge, that's a real problem and you'll need to redesign things to avoid it. Auto-managing the object's lifetime with a shared_ptr instead of manually deleting it is a good approach. – Jeremy Friesner May 28 '12 at 16:22
  • 2
    This sounds like a use case for `shared_ptr` and `weak_ptr`. – reima May 28 '12 at 16:22
  • @Dani not even checking that memory reference or something? A `hasMethod`, anything? Sorry, ultimate noob here. – Silviu-Marian May 28 '12 at 16:23
  • It's entirely possible for the pointer/reference you have could refer to something that's been deleted, or a new one created in its place, or even next doors dog. You have no way of know ing an arbitrary pointer is valid with just the pointer. – Deanna May 28 '12 at 16:58

3 Answers3

2

You can never know if the reference or pointer you are pointing at is invalid (see Why don't I need to check if references are invalid/null?).

You just have to be careful (and clear) about who own the reference/pointer and who should delete it. To help with ownership you can use std::unique_ptr or std::shared_ptr (in C++0x, use boost versions otherwise).

Community
  • 1
  • 1
Julien Lebot
  • 3,092
  • 20
  • 32
  • If I attempt to reference it inside a `try{}catch(){}` statement, can you please also explain why my application still crashes? – Silviu-Marian May 28 '12 at 16:48
  • 1
    There is no general/automatic way to detect if a reference still points to a valid object. Neither the language runtime nor you can do that. So who should throw the exception you are trying to catch? – reima May 28 '12 at 17:24
  • Because accessing a dangling pointer/reference is undefined behavior, and nothing throws an exception. See http://stackoverflow.com/questions/1823721/how-to-catch-the-null-pointer-exception – Julien Lebot May 28 '12 at 17:24
0

Do you really have to know if object exists? Read about Sink Design pattern. It is common used in the Boost Asio. If you are using pointers you have to remember to null pointers after delete etc... Try with smart_ptr's.

For me it is hard to image why you need check if pointer still exists. There is a design problem in your code.

CyberGuy
  • 2,783
  • 1
  • 21
  • 31
0

I ended up keeping the whole thread busy, preventing the object from being deleted until I finished with it. It's redneck, I know, but it saved me all the trouble of having to redneck my way through the entire code.

Silviu-Marian
  • 10,565
  • 6
  • 50
  • 72