« first day (167 days earlier)      last day (4785 days later) » 

Xeo
10:05 PM
Yet Another Windows Surprise: tabbing while renaming a file/folder will get you to the next file/folder in the folder
 
@GMan thanks
@Xeo recently Mac surpised me with this: holding the '3' and '2' button while booting starts the kernel in 32-bit mode, holding the '6' and '4' button will cause the system to boot in 64-bit mode
 
@StackedCrooked lol, that's clever
and silly
 
@jalf yeah, when I learned about this it was met with disbelief and laughter by my colleages
 
Xeo
i just can't stop giggling with this textalign centaur...
 
10:20 PM
Concerning exceptions in destructors I want to present this (not entirely) hypothetical situation:I have a C++ library that connects with a server through some RPC protocol. To create a client session the server provides the AddClient() and RemoveClient() methods. Failure results in exception. Now the C++ library exposes this functionality through the Client class. It is implemented using RAII: the constructor calls AddClient() and the destructor calls RemoveClient().
How to deal with the danger of exception escaping a destructor?
 
Xeo
bad exception choices in the library i'd say
 
@Xeo Handling network errors with exceptions seems fine to me.
 
@StackedCrooked Why might RemoveClient fail and what state is the session in if RemoveClient fails?
 
well, if the class really is designed like that, what do you gain by letting exceptions escape from the destructor? What am I supposed to do as a user of the class, to handle the exception?
 
@CharlesBailey it may have already been removed (for some mysterious reason), or in case of a network failure the client RPC library will throw a NetworkException .
 
10:25 PM
catch the exception and.... uh... ignore it? The object is gone, I can't call RemoveClient on it again, even if I wanted to.
 
@jalf suppose the disconnect was triggered by a close button in a GUI. Then the exception should be allowed to propagate until the GUI eventhandler method, which catches the exception and shows (or logs) an error message.
 
@StackedCrooked If it's already been removed, what happens? If a NetworkException is thrown do I need to re-attempt RemoveClient after "fixing the network"?
 
@StackedCrooked and you're ok with this error message just disappearing into the void if the destructor was called (and throws) during stack unwinding for another exception?
 
@CharlesBailey if it's already been removed, then the server will throw an error (because this should not happen). If a NetworkException is thrown then yes, RemoveClient should be called again.
 
If you're going to report errors to the user, do it reliably. Which means calling RemoveClient before the destructor, where you can properly react to the error
 
10:28 PM
@jalf no of course not
@jalf so no RAII then?
 
If you're going to do it in the destructor, it means you're shutting down the object, and you just have to do your best to clean up, but it's really too late to complain if something goes wrong
 
@StackedCrooked It sounds like RemoveClient has a really poor contract. It may succeed or it my fail. If it fails it may need to be re-tried, or it may not. As it stands it's not a suitable function to be used in an RAII-class destructor on its own.
 
@StackedCrooked I didn't say that. But RAII is about releasing resources in the destructor, not about sending/receiving complex network messages that can fail in dozens of ways, and have to be signalled back to teh user
 
RAII is suitable when you can attempt to acquire a resource and, if successful, somehow or other always release that resource.
 
@CharlesBailey I agree, that's why I said earlier that extensive use of RAII may lead to situations where destructors throw.
 
10:31 PM
@StackedCrooked That's me.
 
in case we were wondering
 
It seems to me that wanting both RAII and non-throwing destructors can conflict.
 
@StackedCrooked Unless you really mean "inappropriate use", it shouldn't.
 
@StackedCrooked no, it may lead to situations where destructors may have to swallow an exception to avoid breakign the program flow
@StackedCrooked RAII doesn't mean "every operation should be done in constructor/destructors"
A good RAII class would be one which acquires the resource in the ctor (in your case, establishes the connection), and in the destructor, shuts down, in whatever way possible. If I want to perform operations that may throw (such as removing myself from the network), then I should do it before the destructor, while I am able to handle the fact that the operation can fail.
 
Choosing against RAII in my sample seems like sacrificing design for safety.
 
10:35 PM
And if I don't do so, then the destructor should be a safety net to ensure that at least the socket gets closed
but the destructor, by definition, is called when the resource is released, and so there's no point in it notifying me of errors
It should do its best to release the resource, and then shut up and return, so I can get on with my life. It is shutting down, and if it fails, there's nothing I can do about it, so why bother to notify me of the failure?
@StackedCrooked No one is saying anything about "choosing against RAII". But again, RAII doesn't mean "only ctors and dtors are allowed", as you seem to think.
 
@j
damnit
@jalf it seems like RAII is appropriate here because of the symmetry of the AddClient() and RemoveClient() method.
 
RAII is about managing resources. Which resource are you managing here, specifically? The network connection? Or the more abstract "being registered on the server"?
@StackedCrooked no, RAII isn't just about symmetry
It is about acquiring and releasing resources
 
The "RemoveClient" is a sort of cleanup.
 
About "do something" and "clean up after the 'something'"
It is a cleanup if it doesn't report errors.
 
@StackedCrooked But RemoveClient has a really bad interface so you need to write a fail-safe function around it before it is a suitable "opposite" of AddClient for use in a RAII class.
 
10:39 PM
A cleanup doesn't say "oops, I failed to clean up, do something or I'm going to terminate your entire process"
 
@jalf It just throws an exception, it doesn't terminate the process.
 
it does if you don't catch the exception
or if it throws during stack unwinding
in other words, it terminates unless you do something
 
The C++ runtime does, not my function.
 
what a silly distinction
without your function to throw the exception, it wouldn't terminate
A sensible RAII class might have a constructor which opens the connection and calls AddClient. Then you have an ordinary member function for an explicit "disconnect me, and throw if the operation fails", and a destructor for "oops, I failed to disconnect cleanly. Now do your best to shut down as cleanly as possible"
 
@StackedCrooked What is really clever is that with a 32bit kernel Mac OSX can run 64bit applications... kind of running 32bit apps in a 64bit os, but the other way around
 
10:45 PM
@DavidRodríguezdribeas yep, learned about that recently too. I think it's cool that my C++ apps are default x86_86 arch while I'm running a 32-bit kernel.
@jalf I've been thinking about introducing initialize/finalize methods. The actual client object would be wrapped in a sort of proxy class that takes care of calling the initialize/finalize functions after construction and before destruction respectively.
@jalf I do realize that this replaces the problem to the proxy class, but I think a solution is workable.
 
I found out when I run into a problem with a plugin for safari that was failing and a friend just selected safari to run 32bit mode instead of 64 (that is also clever: same app, installed in the system, "right click->open in 32bit" to make it run 32bits... which was kind of shocking since I was running a 32bit kernel...
 
@CharlesBailey @jalf or perhaps it's an option to catch the exception in the destructor, and store it in a sort of exception storage class. After destruction exception storage class would be checked and if it holds an exception it could be rethrown.
@DavidRodríguezdribeas yep, I freaked a little when I noticed I was running a 32-bit kernel.
 
11:09 PM
Does anyone remember if, after casting to volatile, the read has to behave as an observable effect? That is, normally this would do nothing: int i = 5; (void)i;, but does this: int i = 5; (void)(volatile int&)i; force i to be read? I recall hearing it's actually non-binding, since the original object is non-volatile, but I can't be arsed to look. :)
 
I love how on my final year I am back to doing 2D sprite ¬_¬ I don't like this recent turn
had fun to day with an epic fail at flooding the heap. working with a friends code, he has a simple linked list style node, needed to delete the list. didn't think too much about the number of noes, so did a simple "on delete, delete next node" style dtor. yer... with like 1000 nodes that didn't work to well
 
§1.9/7 Accessing an object designated by a volatile lvalue (3.10), modifying an object, calling a library I/O function, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment.
@GMan My understanding is that (int volatile &)i is an object i designated by a volatile lvalue expression...
 
@DavidRodríguezdribeas I wonder what is meant by "access"? Presumably it means to perform either a read or a write operation on it?
 
§1.9/6 The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions.
The previous paragraph is:
 
You can name an object in an lvalue expression without reading or writing it. Does i access the object denoted by i or do you have to do int j = i; or i = 5; to actually access it?
 
11:18 PM
§1.9/6 The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions.
 
@thecoshman: it resulted in stackoverflow because of too deep recursion?
 
@StackedCrooked indeed :P
 
@thecoshman done that, been there, ... :)
 
A prime example of why to use STL
 
@CharlesBailey My guess is that i should be sufficient, as that is an expression that after evaluation is substituted by its value. I don't really know... but if you had a hardware counter, I find it hard to believe that you would be forced to create a variable and assign it just to trigger the read
 
11:21 PM
@GMan I don't even think that (void)i; would cause i to be read even if it actually were volatile now.
@DavidRodríguezdribeas I don't agree. i = 0 is an expression which I don't believe causes i to be read, so I don't think that i should either.
 
any way, if of for another poor night sleep
 
@CharlesBailey in the case of conversion to (void)i the full expression has no value, so a read (again, just my opinion) would not be required.
 
@CharlesBailey Hm, I thought the evaluation of the expression i was to perform an lvalue-to-rvalue conversion.
But I have no grounds for that.
 
"Any expression can be explicitly converted to type “cv void.” The expression value is discarded. [Note:
however, if the value is in a temporary variable (12.2), the destructor for that variable is not executed until
the usual time, and the value of the variable is preserved for the purpose of executing the destructor. ] The
lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are not
applied to the expression."
From the standard re. static_cast but this section also applies to other C cast syntax casts.
 
Ooooooh, look at that.
 
11:27 PM
Cast to void doesn't cause the lvalue-to-rvalue conversion to be performed so if you believe this conversion would cause a read it wouldn't apply here.
 
Yeah, you're right.
This is a sub-problem, by the way, of "touching" a location in memory to make it hot in the cache.
 
I'm guessing, but pass it to a function by value that doesn't do anything?
 
That is, given: int someArray[4096]; for (std::size_t i = 0; i < 4096; ++i) process_four_elements(someArray + i);, we'd like to prepare those four elements by putting this in process_four_elements: void process_four_elements(int* i) { touch(i + 4); /* ... */ }
How should touch be implemented? It can't just be template <typename T> void touch(T) {}, because an optimizing compiler will see it does nothing and remove it.
I was thinking making it void touch(T x){ (void)(volatile T&)x; } but as you pointed out, that won't work. I'd hate to assign it to some dummy thing.
 
@GMan By the time your in the function x is a copy anyway. Don't you want to touch an address rather than a value?
 
Er, yeah, sorry, make that touch(*(i + 4)).
 
11:35 PM
inline void do_nothing(int) {} void touch(int *x) { do_nothing(*const_cast<volatile int*>(x)); } Eurrgghhh.
 
We do that at work in a non-portable way for gcc: __builtin_prefetch
 
However clever you are, if the compiler thinks it can optimize something out (the underlying object not actually being volatile) you're kinda screwed anyway you slice it.
 
@DavidRodríguezdribeas Yeah, I was exactly wondering if there was a portable way to do that. :)
@CharlesBailey: Yeah, starting to think there isn't a cost-free portable way. If you want to touch it in C++, you have to do something to it (like assign it to something), but if you don't have a cost, the compiler will optimize it away.
 
I just looked up and it seems that the particular intrinsic is not portable (some are, well, not portable but implemented in different compilers
Also note that there is a difference between a cache prefetch and static_cast<int volatile&>(i)... you are actually reading, not just asking the data to be brought to cache, and that might stall the processor waiting until the data can be actually read
actually defeating the whole purpose of prefetching the data into cache
 
Hm, right. Thanks for the info's, guys.
 
11:43 PM
If that access is inside a function, then the compiler is even more limited in that it less free to reorder side effects around
 
yea
 

« first day (167 days earlier)      last day (4785 days later) »