dynamic_cast howto?

void myEdtor::setValues()
{
	for (int x=0; x<getNumChildComponents(); x++)
	{
		Component		*c			= getChildComponent(x);
		Slider			*slider		= dynamic_cast <Slider*> (c);
		ToggleButton	*btn		= dynamic_cast <ToggleButton*> (c);

		if (slider != 0)
		{
			slider->setValue (3);
			continue;
		}

		if (btn != 0)
		{
			btn->setToggleState (1, false);
			continue;
		}
	}
}

i’d like to set all values for certain type of components (children of myEdtor component), if i use a method like that one (i took the dynamic_cast thinggy from juce’s Component source), i get memory leaks:

Object dump complete.
Detected memory leaks!
Dumping objects ->
{21211} normal block at 0x04956DB8, 8 bytes long.
 Data: <        > 03 CD CD CD 88 AE 95 04 
{21210} normal block at 0x0495D5A0, 8 bytes long.
 Data: <        > 03 CD CD CD D8 D8 7F 04 
{21209} normal block at 0x047FC6C8, 8 bytes long.
 Data: <    H   > 03 CD CD CD 48 C4 7F 04 
{21208} normal block at 0x047FB2A8, 8 bytes long.
 Data: <    (   > 03 CD CD CD 28 B0 7F 04 
{21207} normal block at 0x047F2C80, 8 bytes long.
 Data: <    h   > 03 CD CD CD 68 A1 7F 04 
{21206} normal block at 0x047F13E0, 8 bytes long.
 Data: <     #  > 03 CD CD CD A0 23 7F 04 
{21205} normal block at 0x047F9A00, 8 bytes long.
 Data: <        > 03 CD CD CD D8 08 7F 04 
{21204} normal block at 0x047EA100, 8 bytes long.
 Data: <    @ ~ > 03 CD CD CD 40 B2 7E 04 
{21203} normal block at 0x047E38C8, 8 bytes long.
 Data: <      ~ > 03 CD CD CD F8 95 7E 04 
{21202} normal block at 0x047F4200, 8 bytes long.
 Data: <    @S  > 03 CD CD CD 40 53 7F 04 
{21201} normal block at 0x047E4C98, 8 bytes long.
 Data: <     6  > 03 CD CD CD F8 36 7F 04 
{21200} normal block at 0x047E3180, 8 bytes long.
 Data: <    P   > 03 CD CD CD 50 87 7F 04 
{21199} normal block at 0x0495D220, 8 bytes long.
 Data: <      ~ > 03 CD CD CD 08 EE 7E 04 
{21198} normal block at 0x04800290, 8 bytes long.
 Data: <    P ~ > 03 CD CD CD 50 D2 7E 04 
{21197} normal block at 0x047E67A0, 8 bytes long.
 Data: <      ~ > 03 CD CD CD 80 7F 7E 04 
{21196} normal block at 0x04800388, 8 bytes long.
 Data: <     H~ > 03 CD CD CD B8 48 7E 04 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {20787} normal block at 0x0495AE88, 36 bytes long.
 Data: <        j u n o > 01 00 00 00 0C 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {20217} normal block at 0x047FD8D8, 32 bytes long.
 Data: <        j u n o > 01 00 00 00 0A 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {19849} normal block at 0x047FC448, 40 bytes long.
 Data: <        j u n o > 01 00 00 00 0E 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {19482} normal block at 0x047FB028, 40 bytes long.
 Data: <        j u n o > 01 00 00 00 0E 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {19115} normal block at 0x047FA168, 36 bytes long.
 Data: <        j u n o > 01 00 00 00 0C 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {18684} normal block at 0x047F23A0, 38 bytes long.
 Data: <        j u n o > 01 00 00 00 0D 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {18349} normal block at 0x047F08D8, 32 bytes long.
 Data: <        j u n o > 01 00 00 00 0A 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {17951} normal block at 0x047EB240, 32 bytes long.
 Data: <        j u n o > 01 00 00 00 0A 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {17554} normal block at 0x047E95F8, 32 bytes long.
 Data: <        j u n o > 01 00 00 00 0A 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {17155} normal block at 0x047F5340, 32 bytes long.
 Data: <        j u n o > 01 00 00 00 0A 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {16758} normal block at 0x047F36F8, 34 bytes long.
 Data: <        j u n o > 01 00 00 00 0B 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {16360} normal block at 0x047F8750, 34 bytes long.
 Data: <        j u n o > 01 00 00 00 0B 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {16025} normal block at 0x047EEE08, 36 bytes long.
 Data: <        j u n o > 01 00 00 00 0C 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {15689} normal block at 0x047ED250, 32 bytes long.
 Data: <        j u n o > 01 00 00 00 0A 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {15354} normal block at 0x047E7F80, 32 bytes long.
 Data: <        j u n o > 01 00 00 00 0A 00 00 00 6A 00 75 00 6E 00 6F 00 
c:\devel\juce\src\juce_core\text\juce_string.cpp(76) : {15018} normal block at 0x047E48B8, 36 bytes long.
 Data: <        j u n o > 01 00 00 00 0C 00 00 00 6A 00 75 00 6E 00 6F 00 
Object dump complete.

if i comment out the setValue(), setToggleState() calls no memory leaks occur. This is a editor for a vst plugin and it’s initialization. What’s wrong here, and/or is there a better way to do this.

There’s absolutely nothing wrong with your casting - the leaks must be coming from somewhere else. Since it’s leaking a string that starts “juno”, maybe the culprit is some debugging code, perhaps getting called by a listener callback when you change those values?

…and make sure that when you change those values, it doesn’t alter the list of child components that you’re in the middle of iterating.

I’ve had a few bugs like that in my time…

It seems that you are using Visual Studio.
There is a wonderful addin that helps finding memory leaks :
http://www.codeproject.com/KB/applications/visualleakdetector.aspx

oh yeah :slight_smile: i forgot about that, for now i’m trying to keep my code no to spit any of the jules included dumps, i guess if that will be clean VLD should be also, i checked what happpened and it was the setValue() for the components called the listener methods and that caused trouble, i added false as the second parameter and it’s all good now.