Arduino keep restarting

Hello,

Im having a trouble and i don’t know how to fix it…
I have 3 classes, 2 of them reference each other

Socket and SocketRules. Both need each other to work.

Heres the constructors:

Socket::Socket(unsigned short index, const char *name)
{
	this->index = index;
	this->SetName(name);
	this->autostart = true;
	
	this->current = 0;
	this->ampers = 0;
	this->voltampers = 0;

	this->rules = new SocketRules(this); // Problematic line
	pinMode(index, OUTPUT);
}
SocketRules::SocketRules(Socket *socket)
{
	this->socket = socket;
	for (size_t i = 0; i < sizeof(this->dependsOn); i++)
	{
		this->dependsOn[i] = NULL;
	}
}

When i comment this line: this->rules = new SocketRules(this); all work well! if i uncomment arduino keep restarting and trigger setup again.
I have tried to comment SocketRules constructor and keep it blank, but same happen…

heres the full header:

#pragma once

class Socket;

class SocketRules
{
public:
	SocketRules(Socket *socket);
	~SocketRules();

	OFFSTATE CanRun();

	short GetFreeDependencyIndex();
	bool HaveDependency(Socket *socket);
	bool HaveDependency(const unsigned short index);
	bool AddDependency(Socket *socket);
	Socket *RemoveDependency(Socket *socket);
	Socket *RemoveDependency(const unsigned short index);
	unsigned short GetDependencyCount();

private:
	Socket *socket;
	Socket *dependsOn[SOCKETS_NUMBER];
	
	unsigned long runTime;
	String runHours[100];
	float maxCurrent;
};

Do you see any problem taht can cause this?
Thanks

sn4k3: this->rules = new SocketRules(this); // Problematic line

Just think about this. As soon as you create a SocketRules instance, the constructor is going to run. This constructor is going to create another incidence of SocketRules (which has to run the constructor), this then creates another incidence of SocketRules etc.....

It's going to be SocketRules construction from here to eternity.

KenF:

sn4k3:

this->rules = new SocketRules(this); // Problematic line

Just think about this. As soon as you create a SocketRules instance, the constructor is going to run. This constructor is going to create another incidence of SocketRules (which has to run the constructor), this then creates another incidence of SocketRules etc…

It’s going to be SocketRules construction from here to eternity.

Thanks for the reply,

Ihave re read my constructors but it seens ok to me, im not creating anything inside SocketRules, just assigning and as i said if i leave SocketRules constructor empty same happens…

sn4k3:

SocketRules::SocketRules(Socket *socket)

{
this->socket = socket;
for (size_t i = 0; i < sizeof(this->dependsOn); i++)
{
this->dependsOn[i] = NULL;
}
}

Within the constructor, dependsOn is a pointer that hasn’t yet been set to point at anything. So you’re poking the value of null into unknown address(es).
Maybe you could just use dependsOn=NULL;.

KenF:

sn4k3:

SocketRules::SocketRules(Socket *socket)

{
this->socket = socket;
for (size_t i = 0; i < sizeof(this->dependsOn); i++)
{
this->dependsOn[i] = NULL;
}
}

Within the constructor, dependsOn is a pointer that hasn’t yet been set to point at anything. So you’re poking the value of null into unknown address(es).
Maybe you could just use dependsOn=NULL;.

Thats not true… an array dont need to be initalizarted, each element otherwise needs.
But as i said in last 2 posts i will repeat: if i clean constructor it happens in same way!!! arduino crash!

SocketRules::SocketRules(Socket *socket)
{
        // NO CODE HERE
	/*this->socket = socket;
	for (size_t i = 0; i < sizeof(this->dependsOn); i++)
	{
		this->dependsOn[i] = NULL;
	}*/
}

If i do that same will happen, so passing NULL or not is not the problem.

Until a pointer has been given some value, it's not pointing at available space. It's pointing to some random memory address (maybe zero but certainly not any allocated space)

Can you post code that compiles that demonstrates this problem? Not snippets.

http://snippets-r-us.com/

How to use this forum

    String runHours[100];

What are you going to use 100 String objects for? And why name them runHours?

  Serial.println (sizeof (String));  // --> 6

So that is 600 bytes gone straight away. And that's before you actually put any data into them.

[quote author=Nick Gammon link=topic=273383.msg1926640#msg1926640 date=1413686203] Can you post code that compiles that demonstrates this problem? Not snippets.

http://snippets-r-us.com/

How to use this forum

[/quote]

Sure, full code: https://www.dropbox.com/s/jh07prpkb5ejq8n/Intelipower.zip?dl=0

Sure, full code

Wrong. That is NOT what you were told to do. Use the Additional Options link to attach your code HERE!

for (size_t i = 0; i < sizeof(this->dependsOn); i++)
	{
		this->dependsOn[i] = NULL;
	}

sizeof(this->dependsOn) will be 8.

	Socket *dependsOn[4];

Only four elements. This is running off the end of the array.

    this->index = index;
    this->SetName(name);
    this->autostart = true;

    this->current = 0;
    this->ampers = 0;
    this->voltampers = 0;

Is "this" really necessary?

Is “this” really necessary?

In the first line, it is, because the member name and the argument name are (foolishly) the same.

The pinMode() statement that follows that overuse of this-> does NOT belong in the constructor.

sizeof(this->dependsOn) will be 8. - Yes
Socket *dependsOn[4]; - How can you say that if i had Socket *dependsOn[SOCKETS_NUMBER]; and SOCKETS_NUMBER = 8?

[quote author=Nick Gammon link=topic=273383.msg1926649#msg1926649 date=1413686904]

    this->index = index;
    this->SetName(name);
    this->autostart = true;

    this->current = 0;
    this->ampers = 0;
    this->voltampers = 0;

Is "this" really necessary? [/quote]

you mean "this" keyword? well i guess no but im sure thats not the bug causing arduino crash...

sn4k3:
sizeof(this->dependsOn) will be 8. - Yes
Socket *dependsOn[4]; - How can you say that if i had Socket *dependsOn[SOCKETS_NUMBER]; and SOCKETS_NUMBER = 8?

I don’t understand that.

well you said Socket *dependsOn[4]; by other words size of array is 4, right?
and im telling you that you wrong, the size of this array is 8 because i use same define SOCKETS_NUMBER which is 8.
That is declared on SocketsRules.h

private:
	Socket *socket;
	[b]Socket *dependsOn[SOCKETS_NUMBER];[/b]
    outlet = new Outlet();

That should be:

    outlet = new Outlet;

// Get the sockets count
unsigned short Outlet::Count()
{
    return sizeof(this->sockets);
}

You seem to be confusing sizeof (which is the number of bytes in size) with the number of array elements. In this case the counter will be double.

Try this if you want to know how many items are in an array:

// number of items in an array
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

sn4k3:
and im telling you that you wrong, the size of this array is 8 because i use same define SOCKETS_NUMBER which is 8.

No, I’m not wrong.

const unsigned short SOCKETS_NUMBER = 8;

...
	Socket *dependsOn[SOCKETS_NUMBER];

...

SocketRules::SocketRules(Socket *socket)
{
	this->socket = socket;
	for (size_t i = 0; i < sizeof(this->dependsOn); i++)
	{
		this->dependsOn[i] = NULL;
	}
}

The problem hasn’t gone away.

Try this:

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();

  int * foo [8];   // 8 pointers
  
  Serial.println (sizeof (foo));
  }  // end of setup

void loop () { }

That prints 16, not 8.

Now:

	for (size_t i = 0; i < sizeof(foo); i++)
	{
		foo[i] = NULL;
	}

Probable crash. You are setting 16 elements of the array to NULL when you only have 8 of them.

You know how many you have, so use that number:

SocketRules::SocketRules(Socket *socket)
{
	this->socket = socket;
	for (size_t i = 0; i < SOCKETS_NUMBER; i++)
	{
		this->dependsOn[i] = NULL;
	}
}