object-lifetime

Is it safe to pass arguments by reference into a std::thread function?

蹲街弑〆低调 提交于 2021-02-18 09:58:09
问题 #include <thread> #include <string> #include <vector> #include <chrono> using namespace std; void f(const vector<string>& coll) { this_thread::sleep_for(1h); // // Is coll guaranteed to be valid before exiting this function? // } int main() { { vector<string> coll(1024 * 1024 * 100); thread(f, coll).detach(); } // // I know std::thread will copy arguments into itself by default, // but I don't know whether these copied objects are still valid // after the std::thread object has been destroyed

Delphi FreeAndNil: Looking for an alternate implementation

偶尔善良 提交于 2020-12-05 12:24:53
问题 NOTE: Bear with me, I feel a little "flame grilled" due to some discussions over here and here and some issues I reported here and here. Some background Ye olde (pre 10.4) FreeAndNil looked like this: FreeAndNil(var SomeObject) The new and fresh FreeAndNil looks like this: FreeAndNil(const [ref] SomeObject: TObject); IMO both have their downsides: The old one doesn't do any type checking, so calling FreeAndNil on pointers, records and interfaces compiles just fine, but produces interesting

Delphi FreeAndNil: Looking for an alternate implementation

微笑、不失礼 提交于 2020-12-05 12:23:11
问题 NOTE: Bear with me, I feel a little "flame grilled" due to some discussions over here and here and some issues I reported here and here. Some background Ye olde (pre 10.4) FreeAndNil looked like this: FreeAndNil(var SomeObject) The new and fresh FreeAndNil looks like this: FreeAndNil(const [ref] SomeObject: TObject); IMO both have their downsides: The old one doesn't do any type checking, so calling FreeAndNil on pointers, records and interfaces compiles just fine, but produces interesting

Placement new base subobject of derived in C++

断了今生、忘了曾经 提交于 2020-07-09 08:26:04
问题 Is it defined behavior to placement-new a trivially destructible base object of a derived? struct base { int& ref; }; struct derived : public base { complicated_object complicated; derived(int& r, complicated_arg arg) : base {r}, complicated(arg) {} }; unique_ptr<derived> rebind_ref(unique_ptr<derived>&& ptr, int& ref) { // Change where the `ref` in the `base` subobject of // derived refers. return unique_ptr<derived>(static_cast<derived*>( ::new (static_cast<base*>(ptr.release()) base{ref}))

Placement new base subobject of derived in C++

橙三吉。 提交于 2020-07-09 08:23:05
问题 Is it defined behavior to placement-new a trivially destructible base object of a derived? struct base { int& ref; }; struct derived : public base { complicated_object complicated; derived(int& r, complicated_arg arg) : base {r}, complicated(arg) {} }; unique_ptr<derived> rebind_ref(unique_ptr<derived>&& ptr, int& ref) { // Change where the `ref` in the `base` subobject of // derived refers. return unique_ptr<derived>(static_cast<derived*>( ::new (static_cast<base*>(ptr.release()) base{ref}))

System.AccessViolationException error when stored callback is executed

孤街浪徒 提交于 2020-05-17 06:49:13
问题 I have passed as callback a C++ member function to a C# project through a C++/CLI wrapper (this works fine). The C# project is going to call this delegate when receiving data from another .exe process: an event will be raised and a method will call this callback. So, I needed to "save" this Action delegate using an static instance of a C# class already created. I got the following code: // C++ unmanaged function WRAPPER_API void dispatchEvent(std::function<void(int)> processEvent) { Iface:

System.AccessViolationException error when stored callback is executed

你说的曾经没有我的故事 提交于 2020-05-17 06:49:05
问题 I have passed as callback a C++ member function to a C# project through a C++/CLI wrapper (this works fine). The C# project is going to call this delegate when receiving data from another .exe process: an event will be raised and a method will call this callback. So, I needed to "save" this Action delegate using an static instance of a C# class already created. I got the following code: // C++ unmanaged function WRAPPER_API void dispatchEvent(std::function<void(int)> processEvent) { Iface:

Variable lifetime

做~自己de王妃 提交于 2020-03-21 20:27:09
问题 What happends to variable when line of execution goes outside of code block? For example: 1 public void myMethod() 2 { 3 int number; 4 number = 5; 5 } so, we declare and set variable. When it goes outside of code block (line 5) what happends to variable number? Here is another example with creating instance of class: 7 public void myMethod() 8 { 9 Customer myClient; 10 myClient = new Customer(); 11 } When it goes outside of code block (line 11) what happends to object reference myClient? I