dimanche 2 avril 2017

Freeing Protobuf generated class causes a a segment fault

Hi I am utilizing Protobuf for my personal project about neural networks.

Here is my Protobuf definitions:

syntax = "proto3";

package NGNET;

message InputLayer {
    string name = 1;
    uint32 size = 2;
}

message ComputeLayer {
    string name = 1;
    uint32 size = 2;
    repeated LayerLink inputs = 3;
}

message LayerLink {
    InputLayer il_input = 1;
    ComputeLayer cl_input = 2;
    uint32 output_size = 3;
    repeated float weights = 4;
}

message NNET {
    string name = 1;
    repeated ComputeLayer outputs = 3;
}

The network is created like this:

ComputeLayer output1 = ComputeLayer(10, "output1");
ComputeLayer output2 = ComputeLayer(10, "output2");
ComputeLayer hidden = ComputeLayer(100, "hidden");
InputLayer input1 = InputLayer(784, "input1");
InputLayer input2 = InputLayer(784, "input2");

output1.link(&hidden);
output2.link(&hidden);
hidden.link(&input1);
hidden.link(&input2);
hidden.link(&extra);

The link functions are defined as:

void ComputeLayer::link(ComputeLayer* to_link) {
  NGNET::LayerLink* link = new NGNET::LayerLink();
  link->set_output_size(internal->size());
  link->set_allocated_cl_input(to_link->getInternal());
  internal->mutable_inputs()->AddAllocated(link);
}

void ComputeLayer::link(InputLayer* to_link) {
  NGNET::LayerLink* link = new NGNET::LayerLink();
  link->set_output_size(internal->size());
  link->set_allocated_il_input(to_link->getInternal());
  internal->mutable_inputs()->AddAllocated(link);
}

Note: The getInternal() function returns a NGNET::ComputeLayer or NGNET::InputLayer

Then the outputs are liked to a NNET with:

nnet->mutable_outputs()->AddAllocated(output1->getInternal());
nnet->mutable_outputs()->AddAllocated(output2->getInternal());

When nnet is deleted the program crashes with a segment fault.

I believe this is due to the hidden layer gets deleted twice. Is there any way I can safely free the memory that was allocated ?

Thanks.

Aucun commentaire:

Enregistrer un commentaire