Skip to content

Commit 868f4b8

Browse files
committed
fix: attempt at fixing dangling pointer problem with themember net::FullyConnectToNet()
1 parent 3d3b454 commit 868f4b8

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

examples/Ex3_CloneAndConnectNets.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ int main(){
1111
vector <double> in{ 0, 2, 3};
1212
vector <double> out{ 1, 0, .5, 0};
1313
net n1(dims, true);//creates fully connected net with weights initialized to random values in the shape of dims
14-
n1.TrainBackpropagation( out, in);//run backpropagation once
1514
cout<<"n1:"<<endl;
1615
for (int i= 0; i<n1.get_size(); i++){
1716
cout<<"layer "<<i<<" = "<<n1.get_size(i)<<endl;
1817
}
1918

19+
2020
net n2 = n1;
2121
cout<<"n2:"<<endl;
2222
for (int i= 0; i<n2.get_size(); i++){
@@ -33,10 +33,17 @@ int main(){
3333
n3.AppendNet(&n2,false);
3434

3535
cout<<"n3:"<<endl;
36-
for (int i= 0; i<n3->get_size(); i++){
37-
cout<<"layer "<<i<<" = "<<n3->get_size(i)<<endl;
36+
for (int i= 0; i<n3.get_size(); i++){
37+
cout<<"layer "<<i<<" = "<<n3.get_size(i)<<endl;
3838
}
39-
39+
40+
n3.TrainBackpropagation(out,in);
41+
cout<<endl<<"n1 output = ";
42+
n1.Output();
43+
cout<<endl<<"n2 output = ";
44+
n2.Output();
45+
cout<<endl<<"n3 output = ";
46+
n3.Output();
4047

4148

4249
return 0;

header/net.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ class net //represents a the vector of vectors of neurons
3939
int col;
4040
bool isnetfunc=false;
4141

42-
43-
4442
union
4543
{
4644
//pointer to function to run
@@ -92,6 +90,7 @@ class net //represents a the vector of vectors of neurons
9290
vector<double> input;
9391
typedef vector <string> group;
9492
vector<group>groups;
93+
bool netPointersCopied=false;
9594
//vector<thread> threads;
9695

9796

src/net.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ net::net(vector <int> &dimensions, bool fullyconnected)
6464
//destructor
6565
net::~net()
6666
{
67+
if(this->netPointersCopied){
68+
cout <<endl<<"netPointersCopied";
69+
return;
70+
}
6771
//deletes all neurons and their inweights
6872
for(row=layers.begin(); row!=layers.end(); row++)
6973
{
@@ -803,11 +807,12 @@ net* net::AppendNet(net* net_from, bool pasteOver)
803807
this->layers.resize(0);
804808
this->layers.insert(this->layers.end(), l.begin(), l.end());
805809

810+
806811
}else{
807812
this->layers.insert(this->layers.end(), net_from->layers.begin(), net_from->layers.end());
808813
}
809814

810-
815+
net_from->netPointersCopied = true;
811816
return this;
812817
}
813818

0 commit comments

Comments
 (0)