From 4e1d606af5d95e007ba90c13fc2dc61c39905e27 Mon Sep 17 00:00:00 2001 From: Gabriel Ferreira Date: Mon, 1 Aug 2022 23:16:40 -0300 Subject: [PATCH] bindings: fix segmentation violation in python scripts And move sample-rng-plot.py code to a main function --- examples/wireless/mixed-wired-wireless.py | 11 ++++- src/core/examples/sample-rng-plot.py | 60 +++++++++++++---------- utils/python-unit-tests.py | 8 +++ 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/examples/wireless/mixed-wired-wireless.py b/examples/wireless/mixed-wired-wireless.py index 76c4b91bb..71b3d452e 100644 --- a/examples/wireless/mixed-wired-wireless.py +++ b/examples/wireless/mixed-wired-wireless.py @@ -235,7 +235,7 @@ def main(argv): # Reset the address base-- all of the 802.11 networks will be in # the "10.0" address space ipAddrs.SetBase(ns.network.Ipv4Address("10.0.0.0"), ns.network.Ipv4Mask("255.255.255.0")) - + tempRef = [] # list of references to be held to prevent garbage collection for i in range(backboneNodes): print ("Configuring wireless network for backbone node ", i) # @@ -279,11 +279,18 @@ def main(argv): # the network mask initialized above # ipAddrs.NewNetwork() + + # This call returns an instance that needs to be stored in the outer scope + # not to be garbage collected when overwritten in the next iteration + subnetAlloc = ns.mobility.ListPositionAllocator() + + # Appending the object to a list is enough to prevent the garbage collection + tempRef.append(subnetAlloc) + # # The new wireless nodes need a mobility model so we aggregate one # to each of the nodes we just finished building. # - subnetAlloc = ns.mobility.ListPositionAllocator() for j in range(infra.GetN()): subnetAlloc.Add(ns.core.Vector(0.0, j, 0.0)) diff --git a/src/core/examples/sample-rng-plot.py b/src/core/examples/sample-rng-plot.py index 2f1d8c8e2..936ec3121 100644 --- a/src/core/examples/sample-rng-plot.py +++ b/src/core/examples/sample-rng-plot.py @@ -29,37 +29,43 @@ import sys import argparse from ns import ns -parser = argparse.ArgumentParser("sample-rng-plot") -parser.add_argument("--not-blocking", - action="store_true", - default=False) -args = parser.parse_args(sys.argv[1:]) -# mu, var = 100, 225 +def main(): + parser = argparse.ArgumentParser("sample-rng-plot") + parser.add_argument("--not-blocking", + action="store_true", + default=False) + args = parser.parse_args(sys.argv[1:]) -## Random number generator. -rng = ns.CreateObject("NormalRandomVariable") -rng.SetAttribute("Mean", ns.DoubleValue(100.0)) -rng.SetAttribute("Variance", ns.DoubleValue(225.0)) + # mu, var = 100, 225 -## Random number samples. -x = [rng.GetValue() for t in range(10000)] + ## Random number generator. + rng = ns.CreateObject("NormalRandomVariable") + rng.SetAttribute("Mean", ns.DoubleValue(100.0)) + rng.SetAttribute("Variance", ns.DoubleValue(225.0)) -# the histogram of the data + ## Random number samples. + x = [rng.GetValue() for t in range(10000)] -## Make a probability density histogram -density = 1 -## Plot color -facecolor='g' -## Plot alpha value (transparency) -alpha=0.75 + # the histogram of the data -# We don't really need the plot results, we're just going to show it later. -# n, bins, patches = plt.hist(x, 50, density=1, facecolor='g', alpha=0.75) -n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) + ## Make a probability density histogram + density = 1 + ## Plot color + facecolor = 'g' + ## Plot alpha value (transparency) + alpha = 0.75 -plt.title('ns-3 histogram') -plt.text(60, .025, r'$\mu=100,\ \sigma=15$') -plt.axis([40, 160, 0, 0.03]) -plt.grid(True) -plt.show(block=not args.not_blocking) + # We don't really need the plot results, we're just going to show it later. + # n, bins, patches = plt.hist(x, 50, density=1, facecolor='g', alpha=0.75) + n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75) + + plt.title('ns-3 histogram') + plt.text(60, .025, r'$\mu=100,\ \sigma=15$') + plt.axis([40, 160, 0, 0.03]) + plt.grid(True) + plt.show(block=not args.not_blocking) + + +if __name__ == "__main__": + main() diff --git a/utils/python-unit-tests.py b/utils/python-unit-tests.py index 1908d3f52..a049db191 100644 --- a/utils/python-unit-tests.py +++ b/utils/python-unit-tests.py @@ -214,6 +214,8 @@ class TestSimulator(unittest.TestCase): self.assertTrue(self._received_packet is not None) self.assertEqual(self._received_packet.GetSize(), 19) + # Delete Ptr<>'s on the python side to let C++ clean them + del internet def testAttributes(self): """! Test attributes function @@ -243,6 +245,9 @@ class TestSimulator(unittest.TestCase): mobility.GetAttribute("PositionAllocator", ptr2) self.assertNotEqual(ptr.GetObject(), ns.core.Ptr["Object"](ns.cppyy.nullptr)) + # Delete Ptr<>'s on the python side to let C++ clean them + del queue, mobility, ptr, ptr2 + def testIdentity(self): """! Test identify @param self this object @@ -257,6 +262,9 @@ class TestSimulator(unittest.TestCase): self.assertEqual(c1, c2) + # Delete Ptr<>'s on the python side to let C++ clean them + del csma, channel + def testTypeId(self): """! Test type ID @param self this object