Hi, it’s shameful that I come to ask some examples of the scoped pointer and reference counted object examples. Using JUCE only for the GUI and audio wrappers, I never really took the time to dig how to deal with counters and scope pointers. In short most of the time I build an object in the constructor and destroy it when the parent object is destructed. That is obviously not viable for larger projects with shared objects, references delayed callbacks etc… so:
- In the ReferenceCountedObject we have the following example:
[code] class MyClass : public ReferenceCountedObject
// This is a neat way of declaring a typedef for a pointer class,
// rather than typing out the full templated name each time…
typedef ReferenceCountedObjectPtr Ptr;
MyClass::Ptr p = new MyClass(); MyClass::Ptr p2 = p; p = nullptr; p2->foo();[/code]
will p be destroyed if I do p2 = nullptr ? ( Yes it does!) If so, I then guess I should put the pointer to nullptr in the destructor, right ? ( If p is on the stack, no… if p is a class variable, destructing the object will release all the variables it does held including p. Writing explicitly “p = nullptr” in the destructor is therefore useless)
- I would like to create an AudioSampleBuffer, should I do the following:
typedef ReferenceCountedObjectPtr<AudioSampleBuffer> Ptr;
AudioSampleBuffer::Ptr ab = new AudioSampleBuffer(2, 1024);
- What about scope pointers ? How to code something like :
and are affectations also like the following ?
When after a function call we do return a scope pointer, will it live until the scope of the function caller ? I guess yes…
Is that correct: ScopedPointers should be used in order to return variables, affect and do some stuff which will end up with the stack at some time but countedObject are preferable for app life time variable such as sharing variables between threads, objects… variables owned by an object on the heap typically ?
Is there some good examples somewhere related to the points above ? Did I missed something else important ?