FAQ Database Discussion Community


How can I increase ownership count of std::shared pointer

c++,smart-pointers,object-lifetime,lifetime-scoping
I have a struct that has a pointer as member: struct MyStruct { char *ptr; } I want to initialize the ptr in a scope and then be able to use it outside of that scope: { // scope 0 { //scope 1 { // scope 2 mystruct.ptr = new...

Before_validation :foo, on: :update not working as expected

ruby-on-rails,activerecord,callback,object-lifetime
I'm using Rails 4.0.0. I have the following setup: class Foo < ApplicationController before_validation :foo, on: :create ... private def bar puts 'bar is called' end end This works - in the console, when I create a foo, I see the message 'bar is called'. If I call valid? on...

can memcpy for std::aligned_storage?

c++,c++14,memcpy,object-lifetime
std::aligned_storage::type is POD type. POD type can memcpy. However, What happens if placement new non-trivially-copyable type to std::aligned_storage? Can it memcpy that std::aligned_storage? non-trivially-copyable type(non-POD type) can NOT memcpy, Behavior is undefined. If std::aligned_storage memcpy non-trivially-copyable type, is it also undefined behavior? #include <new> #include <type_traits> #include <cstring> #include <iostream>...

Strong reference to `self` to keep the object alive (temporarily): evil?

ios,objective-c,automatic-ref-counting,uialertview,object-lifetime
I'm creating a wrapper for UIAlertView (I know about UIAlertController and about several already existing wrappers, it's also for educational purposes). Suppose it looks like this (very shortened version): @interface MYAlertView : NSObject -(void)show; @end @interface MYAlertView()<UIAlertViewDelegate> @end @implementation MYAlertView -(void)show { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Some title" message:@"Some...

Is the lifetime of the temporary string long enough here? [duplicate]

c++,object-lifetime
This question already has an answer here: Guaranteed lifetime of temporary in C++? 5 answers #include <cstdio> #include <string> std::string foo() { return "Hello, World!"; } int main() { printf( "%s\n", foo().c_str() ); } ...

Is HierarchicalLifetimeManager the correct lifetime manager to use for WCF client / ChannelFactory

wcf,dependency-injection,inversion-of-control,unity,object-lifetime
I have a very simple WPF application that is calling There is one container for the application, that is configured when the application starts. The WPF app has a service reference to my WCF service. I have registered my generated proxy to the container like this: container.RegisterType<IAccountService>( new HierarchicalLifetimeManager(), new...

Struct that owns some data and a reference to the data [duplicate]

rust,object-lifetime,borrow-checker
This question already has an answer here: How to initialize struct fields which reference each other 1 answer Construction of an object allocates data needed for lifetime of that object, but also creates another object that needs to keep references to the data: pub fn new() -> Obj {...

How To Implement PerGraph LifeStyle

c#,.net,dependency-injection,simple-injector,object-lifetime
According to Simple Injector documentation, a Per Graph lifestyle exists that can be used to limit the scoping of an object to the current graph. But the documentation doesn't list what actually needs done to implement it, and I can't find any documentation online on how to use it. How...