Question about RAII


#1

Hi jules,

I’m hitting your assertion in LeakCounter,

[code]/** If you hit this, then you’ve leaked one or more objects of the type specified by
the ‘OwnerClass’ template parameter - the name should have been printed by the line above.

				If you're leaking, it's probably because you're using old-fashioned, non-RAII techniques for
				your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
				ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
			*/[/code]

1)Should I understand your words like this:
I should put my object directly in my JUCE classes, but not theirs pointers(which I need to new and deleteAllChildren()).

2)my project is half JUCE(for UI and plugin framework) and half pure C++(my audio engine), so I should also avoid using pointer new and delete in my pure C++ part ? In other words, it’s always a good habit to directly use the object instead of pointers?

thx


#2

As far as I understand, you shouldn’t have to delete pointers manually. So If you need to dynamically allocate objects, use something like a ScopedPointer or OwnedArray which takes care of deleting the object as soon as it goes out of scope.
If you use ScopedPointers for your child components you don’t need deleteAllChildren (using it would delete the objects twice resulting in an exception).

I also use Juce’s ScopedPointers etc. for stuff not directly related to the framework (dsp-stuff). Of course this is only possible if this isn’t meant to work without Juce (e.g. is also used in a non-Juce project).

Chris


#3

[quote=“ckk”]As far as I understand, you shouldn’t have to delete pointers manually. So If you need to dynamically allocate objects, use something like a ScopedPointer or OwnedArray which takes care of deleting the object as soon as it goes out of scope.
If you use ScopedPointers for your child components you don’t need deleteAllChildren (using it would delete the objects twice resulting in an exception).

I also use Juce’s ScopedPointers etc. for stuff not directly related to the framework (dsp-stuff). Of course this is only possible if this isn’t meant to work without Juce (e.g. is also used in a non-Juce project).

Chris[/quote]

thx.

yes i can’t put all my codes upon JUCE library. and sometimes a pointer might be more convenient, i guess?


#4

Of course you can’t use a ScopedPointer if the pointer isn’t owned in the scope, e.g. if you pass a pointer to functions.

Chris


#5

Yes, very much so. If you don’t have juce, you could also use std::auto_ptr too. Never use new/delete if you can avoid it.