• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By oranssi
      This is an adult psychedelic fighting game with spirits of the natural elements. 
      Choose your character to represent an anthropomorphic version of the different elements. 
      Earth, fire, air, water and more. There are 28 Archelemental spirits to unlock. 
      Take on challenges to earn more goodies such as sexy content, story modes and even make your own Archelemental to play and to share it with others.
      Download here :
      https://oranssi.itch.io/archelementals-of-eiss

      Trailer :
      https://www.youtube.com/watch?v=SmBNu_dPN_Y

    • By oranssi
      This is an adult psychedelic fighting game with spirits of the natural elements. 
      Choose your character to represent an anthropomorphic version of the different elements. 
      Earth, fire, air, water and more. There are 28 Archelemental spirits to unlock. 
      Take on challenges to earn more goodies such as sexy content, story modes and even make your own Archelemental to play and to share it with others.
      Download here :
      https://oranssi.itch.io/archelementals-of-eiss

      Trailer :
      https://www.youtube.com/watch?v=SmBNu_dPN_Y

    • By Ricardo3Ddev
      Hi guys!
      This is a independent game being produced by me and my brother. We’ve been working on it for about 6 months and we’ve already done a good part of the game. We hope to finalize and make it available on Steam by the end of this year.
      We are using Blender 3D and Gimp software for production.
       
      About the Game: Dongo Adventure will be a 3D platform style game, where the main character (Dongo) is a mouse that ventures through various scenarios (sewers, culverts, streets, electric grid, etc.) and faces several enemies along the way (cockroaches, mosquitoes, spiders, toxic gases, electrical wires, etc.). He carries a basket / backpack with cheeses that he uses to throw and defend himself from enemies, as well as being able to push objects that helps him to overcome obstacles. The ultimate goal will be a surprise!
       
      Now we are developing new scenarios and enemies. We hope to publish news soon...
      Game page on Steam: http://store.steampowered.com/app/811450/Dongo_Adventure/ - (Teaser UPDATED)
      Dongo Adventure - Indie Game Project (First Teaser) https://www.youtube.com/watch?v=X2nmxtkE0xk
       
      Thanks for following the project!

    • By Ty Typhoon
      Before read everything i am honest:
      Payment after release you get your percentage lifetime for that project.
       
      Second:
      i dont need your inspirations, ideas, music or designs.
      My head is full with that.
      I need workers who i can trust.
       
       
       
      Please let us talk in discord.
      I got a lot of stuff planned, there is much work to do.
       
      But first my team and me try to start with a small mini game and we need maybe exactly you.
      Planned for more than pc, like ps4, xbox one and mobile - so its very important to us to hopefully welcome a programmer.
       
      The mini game will be part of the planned big game. There will be never before seen guns and gameplay, you will get deeper info if youre a safe part of the team.
       
      I need:
      Programmers
      Animators
      Zbrush pros
       
      Join here please:
      https://discord.gg/YtjE3sV
       
      You find me here:
      Joerg Federmann Composing#2898
       
       
    • By Ty Typhoon
      So me and my co try to do a game.
      It should be in unity couse my co do everything in this engine.
       
      We got the rpg package from evila for inventor, but it only runs on pc right now.
       
      I like to make a online store for guns in the game and a multiplayer open world that runs on pc, android, mobile, ps4, xbox one. 
      Somebody told me that you "only" need to program it like so and that its possible in every engine...
       
      [recruitment wording deleted by moderator]
       
      And yes, this is no recruitment post.
      [recruitment wording deleted by moderator]]
       
      Also discord:
      Joerg Federmann Composing#2898
  • Advertisement
  • Advertisement
Sign in to follow this  

Tool for object Lifetime = unique_ptr; Tool for Memory Lifetime = ?

Recommended Posts

As I know, tools for managing object lifetime are :- 

  • std::unique_ptr
  • std::move
  • move constructor
  • move assignment
  • delete copy constructor
  • delete copy assigment

With those tools, it is very obvious to determine owner of any certain object - the owner is the one who stores std::unique_ptr as a field.

However, it is obscure which class actually controls memory allocation.

There are only few tools to help :-

  • the second template parameter of std::unique_ptr
  • the second template parameter of std::vector and other datastructure

None of them indicate which instance of allocator is the ownership of such address.

In a scope of class, it is alarming that underlying memory of a std::unique_ptr (that a class owns) can be deallocated any times. (e.g. from pop operation of scope allocators).

std::unique_ptr<SomeThing> a=scopeAllocator->makeUnique<SomeThing>();
scopeAllocator->pop();
//memory of "a" is invalid!

Are there any tools/techniques to help/prevent it, or at least make thing more obvious for coder?

 

 

 

Share this post


Link to post
Share on other sites
Advertisement
These tools are designed to work best when the object lifetime and allocation lifetime match exactly.

If the object was allocated from a scope allocator, then the scope allocator itself IS the owner. It's not a good idea to use a unique_ptr as well...

Share this post


Link to post
Share on other sites
These tools are designed to work best when the object lifetime and allocation lifetime match exactly.

Thank a lot Hodgman!  It sounds very nice.  

How to make an allocator own the objects?   

  • Should I cache them by void* and cache the correct deleter (function pointer) for each one?   
  • If I understand correctly, any one-frame allocator should also return weak pointer. (?)

i.e. are there any good practice to follow?    

What if the return result is a bit obscure?

template<class T>class CustomVector{
     T* t;//
};
class AISystem{
    public: static CustomVector<NPC_ID> reportAllNearNPC(){
        //use "scopeAllocator" to allocate "CustomVector::t"
    }
};
CustomVector<NPC_ID> a=aiSystem->reportAllNearNPC();
scopeAllocator->pop();
//"a.t" become dangling pointer

Your comment is very valuable - it is better than the chapter "allocator" in every C++ books I have read, thank!

I desire to know more.  If it is not too much to ask, may you recommend name of some good books :D please?

Edited by hyyou

Share this post


Link to post
Share on other sites
When you allocate objects (not PODs) from a scope allocator, then the scope allocator is supposed to call destructors (and release memory). That's the entire reason for using it over a simpler linear allocator. Does it even make sense to wrap objects in unique_ptr then? You'd call destructors twice, and in the strictest sense you are telling lies because now two owners own the object although you say only one does (and only one should). That's really bad.

On the other hand, the calling of destructors and (conceptual) release of memory usually happens at the end of a scope, not when you manually call a pop() function (why would I even want to pop the allocator at an inopportune time?). End of scope may of course mean "end of frame" or "end of level", and it may "manually" call a pop function. But that point in time is exactly well-defined, it's not happening randomly when the objects are still being accessed afterwards. Which means it basically does the same thing unique_ptr would do as well, at the same time, only for a whole bunch of objects.

Share this post


Link to post
Share on other sites
the strictest sense you are telling lies

It is totally reasonable and also enlightens me, thank!   

What if the result value has some pointer inside?

For example, it is a data-structure that may or may not the owner of the underlying data,  should I create 2 classes like this? :-   

//old version
template<class T>class CustomArray{ //type of return result
    unique_pointer<void*> database; //underlying data
};

//new version
template<class T>class WeakArray{
    T* database;
};
template<class T>class StrongArray{
    unique_pointer_toArray<Bullet*> database;//just example
};
//inside a function of some system
WeakArray<Bullet> arr;
arr.t = scopeAllocator->makeWeakArray<Bullet>(5);//similar as new Bullet[5];
return arr;
Edited by hyyou

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Advertisement