Bug 1672

Summary: Problem with ns3::SimpleRefCount
Product: ns-3 Reporter: Andreas <inuse123>
Component: coreAssignee: Mathieu Lacage <mathieu.lacage>
Status: RESOLVED INVALID    
Severity: major CC: ns-bugs
Priority: P5    
Version: ns-3.15   
Hardware: PC   
OS: Linux   
Attachments: staticsim

Description Andreas 2013-05-05 17:31:59 UTC
I'm creating a set of Nodes in a NodeContainer and then writing the Ptr<Node> from node.Get(x) in a struct for further processing in functions.

Example code is:

uint16_t numchain = 2;
uint16_t numasr = 1;
uint16_t numdrg = 10;

struct Nodes {
	uint16_t chainID;
	uint32_t nodeID;
	Ptr<Node> cNode;
} *stbNodes;

int main (int argc, char *argv[])
{
	
NodeContainer nstb;
nstb.Create (numchain);

stbNodes = (struct Nodes*)malloc(sizeof(struct Nodes) * numchain * numasr * numdrg);

uint16_t offset = 0;
for (uint16_t chaincnt = 1; chaincnt <= numchain; chaincnt++)
{	
  for (uint16_t stbcnt = offset; stbcnt < chaincnt * numasr * numdrg; stbcnt++)
  {
    stbNodes[stbcnt].chainID = chaincnt;
    stbNodes[stbcnt].nodeID = stbcnt;
    stbNodes[stbcnt].cNode = nstb.Get(chaincnt-1);
  }
  offset = chaincnt * numasr * numdrg;
}
...

If i start the simulation with the values numchain=1, numasr=1, numdrg=10 its all fine. But if i run with e.g. 1,1,20 or 1,5,24 it crashes most of the time with always the same error:

Program received signal SIGSEGV, Segmentation fault.
0x0806d835 in ns3::SimpleRefCount<ns3::Object, ns3::ObjectBase, ns3::ObjectDeleter>::Unref (this=0xffffffff) at ./ns3/simple-ref-count.h:95
95          m_count--;
(gdb) bt
#0  0x0806d835 in ns3::SimpleRefCount<ns3::Object, ns3::ObjectBase, ns3::ObjectDeleter>::Unref (this=0xffffffff) at ./ns3/simple-ref-count.h:95
#1  0x0806d193 in ns3::Ptr<ns3::Node>::operator= (this=0x80cf068, o=...) at ./ns3/ptr.h:470
#2  0x08063b84 in main (argc=2, argv=0xbfffeea4) at ../scratch/staticgen.cc:961

This output is from gdb

So why is this not happening with 10 or 11 or 9 Nodes but with 20 Nodes? And why is this not happening all the time. I've got it running with 1,5,24 sometimes...
If there is something wrong with the handling in my code it should happen everytime or not?

I tried this also with the stable of ns-3.16 but still the same problem.

I hope you can help me with this
Thx in advance
Andreas
Comment 1 Tommaso Pecorella 2013-05-05 17:43:32 UTC
Please post the full code so we can take a look at it. 
With a simple snippet we can't say where the error is thrown, when and why. So no debugging is possible.

T.
Comment 2 Mathieu Lacage 2013-05-06 04:21:56 UTC
(In reply to comment #0)

> stbNodes = (struct Nodes*)malloc(sizeof(struct Nodes) * numchain * numasr *
> numdrg);

This is your bug. Use new instead to make sure that the constructor for Ptr<Node> gets invoked.

Mathieu
Comment 3 Andreas 2013-05-06 15:50:02 UTC
Created attachment 1593 [details]
staticsim
Comment 4 Andreas 2013-05-06 15:52:17 UTC
Thank you Mathieu,

that did the trick. works like a charm now :)
I attached the complete code just for reference