object-lifetime

Is this a proper use of a temporary std::string?

ぐ巨炮叔叔 提交于 2019-12-05 10:25:45
std::string getMyString() { return <make a string>; } ... HANDLE something = OpenSomething(getMyString().c_str(), ...); I've read Guaranteed lifetime of temporary in C++ and I believe that the temporary string will live until the assignment has been evaluated, i.e. plenty long enough to make this work as expected. Having once before run into an std::string lifetime-related bug (can't remember what it was) I'd rather double-check... Yes, this is fine. :-) The string will be destroyed at the end of the statement, at the semi colon. The destructor for the temporary will not be called until after

Xamarin, Autofac, NavigationService and BeginLifetimeScope

两盒软妹~` 提交于 2019-12-05 00:52:56
问题 A beginner question on lifetimescopes with autofac and when to use them, in a xamarin app. As mentioned in this article (https://nblumhardt.com/2011/01/an-autofac-lifetime-primer/) which is referred to by the autofac documentation (http://docs.autofac.org/en/latest/lifetime/) they hammer a lot on not resolving from the root container but using seperate lifetimescopes and thinking in terms of units of work because autofac hold referenences to disposable objects even if they are not used

Safe way in Delphi for a Form to distribute interface objects tied to its lifetime?

大城市里の小女人 提交于 2019-12-04 20:51:41
问题 I have a Delphi Form that provides the functionality behind an interface object that other parts of the code get references too via a property belonging to the Form. I can't delegate the interface functionality to a child object because too much of that functionality is serviced by controls/components on the form. I can't use TAggregatedObject or TContainedObject to link the lifetime of the interfaced objects being passed around to the Form because the TForm class does not inherit from

Who owns wrapped streams (e.g. TextWriter) in .NET?

谁说我不能喝 提交于 2019-12-04 19:18:40
问题 I've recently encountered an error "ObjectDisposedException: Cannot access a closed Stream" [ObjectDisposedException: Cannot access a closed Stream.] System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +10184402 System.Security.Cryptography.CryptoStream.FlushFinalBlock() +114 System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing) +48 when using code following the format: using (var stream = new MemoryStream()) { using (var hashStream = new CryptoStream(stream,

Can I override Dispose to make an entity class that always calls SaveChanges?

你离开我真会死。 提交于 2019-12-04 12:06:02
This is a fairly fine point, and I expect the answer is "it's not a hot idea to begin with" - that said, it has a points that I'm interested in regardless, if someone is kind enough to indulge. Model Code: public partial class MyEntities : ObjectContext { // the idea is if the object is in a using block, this always gets called? protected override void Dispose(bool disposing) { this.SaveChanges(); base.Dispose(disposing); } } Client Code: using(var model = new MyEntities()) { // do something // no worry about calling model.SaveChanges() } The issues I'm uncertain about are: Is Dispose the

std::string c_str() scope after returning from function

梦想的初衷 提交于 2019-12-04 05:31:30
问题 I have below mentioned function in C++/MFC: CString StringFunc() { std::string abc = "Hello"; return abc.c_str(); } int main() { CString Temp = StringFunc(); Use_Temp(Temp); } 1.) What would be the lifetime of abc.c_str() pointer returned by StringFunc(), would it be safely copied to variable 'Temp' after StringFunc() returns ? 2.) CString Temp = StringFunc() is a Shallow copy operation or Deep Copying ? 回答1: What would be the lifetime of abc.c_str() pointer returned by StringFunc(), would it

Lifetime of Qt Objects

点点圈 提交于 2019-12-04 02:09:46
What are the lifetimes of Qt Objects? Such as: QTcpSocket *socket=new QTcpSocket(); When socket will be destroyed? Should I use delete socket; Is there any difference with: QTcpSocket socket; I couldn't find deep infromation about this, any comment or link is welcomed. Qt uses parent-child relationships to manage memory. If you provide the QTcpSocket object with a parent when you create it, the parent will take care of cleaning it up. The parent can be, for example, the GUI window that uses the socket. Once the window dies (i.e. is closed) the socket dies. You can do without the parent but

What wording in the C++ standard allows static_cast<non-void-type*>(malloc(N)); to work?

你说的曾经没有我的故事 提交于 2019-12-03 13:49:02
As far as I understand the wording in 5.2.9 Static cast, the only time the result of a void* -to-object-pointer conversion is allowed is when the void* was a result of the inverse conversion in the first place. Throughout the standard there is a bunch of references to the representation of a pointer, and the representation of a void pointer being the same as that of a char pointer, and so on, but it never seems to explicitly say that casting an arbitrary void pointer yields a pointer to the same location in memory, with a different type, much like type-punning is undefined where not punning

Android - is onDestroy supposed to destroy the activity, its variables and free up memory

风格不统一 提交于 2019-12-03 12:20:29
I have a bug in my code that made me think I don't fully understand the Android Lifecycle. Yes, I have read all the docs and looked at the diagrams, but they seem to talk only about when to save data, when the activity may loose focus or get killed. However, my question is if I don't need to save state, what happens to the variables & their stored values? I expected them to be destroyed to, but a bug in my code seems to indicate otherwise. In my case here is what happened. I have an activity that launches a custom view (no xml, I just draw bitmaps on the screen in my custom view). The only

Is using the result of new char[] or malloc to casted float * is UB (strict aliasing violation)?

孤人 提交于 2019-12-03 06:02:49
问题 Which code of these has UB (specifically, which violates strict aliasing rule)? void a() { std::vector<char> v(sizeof(float)); float *f = reinterpret_cast<float *>(v.data()); *f = 42; } void b() { char *a = new char[sizeof(float)]; float *f = reinterpret_cast<float *>(a); *f = 42; } void c() { char *a = new char[sizeof(float)]; float *f = new(a) float; *f = 42; } void d() { char *a = (char*)malloc(sizeof(float)); float *f = reinterpret_cast<float *>(a); *f = 42; } void e() { char *a = (char*