Analysis port and write function

Hi
I have an analysis port which sends packets to the corresponding write function in the subscriber:


      foreach(exp_dma[dma_id]) begin //exp_dma is associative array
	     //Using this field to pass the DMA number to comparator.
	     xgmac_pkt.avb_gateway_info=dma_id;
             exp_pcie_rx_pkt_ap.write(xgmac_pkt);
///	     rd_bk_ap.write(dma_id);
     
	  end


function void write_EXP_``P(T pkt);\
ethernet_transaction exp_pkt;\
//load_mc_bc(pkt);\
//exp_``P``_q[pkt]=1'b1;\
exp_tmp_q.push_back(pkt);\
$display("vem avb_gateway_info =%0d",pkt.avb_gateway_info);\
	  foreach(exp_tmp_q[i])begin \
	     exp_pkt = exp_tmp_q[i]; \
		 $display("vem1 i=%0d dma_id=%0d",i,exp_pkt.avb_gateway_info);\
	end\
`uvm_info("base_comparator",$psprintf("EXP pkt received from the ref_model module for path.size=%0d  \n %s",exp_tmp_q.size(),pkt.sprint),UVM_LOW);\
endfunction



output of the above code is :

vem avb_gateway_info =0
vem1 i=0 dma_id=0
.
.
.

vem avb_gateway_info =2
vem1 i=0 dma_id=2
vem1 i=1 dma_id=2

===========================
When the “write” function got executed the first time, pkt with avb_gateway_info=0 was sent and it was pushed into the exp_tmp_q.
When the second pkt was written, it was also pushed into the exp_tmp_q with avb_gateway_info==2. But I’m not able to understand why the first pkt’s avb_gateway_info changed from 0 to 2. It should have remained 0.
Can anyone please help me understand where is the error in this code.

In reply to kartavya:

The answer is related to basic Class Concepts .

When you pass objects as argument to a method the handle to the object is passed to the method
So any change in the object properties made inside the method will be reflected back from where
the call to the method was made

During the 2nd Iteration of foreach loop above the 2nd Index i.e dma_id would be 2 .
Hence when you pass the same Object to the subscriber it sees the updated value of 2 .

So the Queue holds the same handle twice !!

To Observe different dma_id pass a clone of the object while calling write

In reply to ABD_91:

I modified my code as below and I got the output as expected


    foreach(exp_dma[dma_id]) begin
	  svt_ethernet_transaction xgmac_pkt_clone;
	     //Using this field to pass the DMA number to comparator.
		 xgmac_pkt.avb_gateway_info=dma_id;
		 $cast(xgmac_pkt_clone,xgmac_pkt.clone()); //Added this line

         exp_pcie_rx_pkt_ap.write(xgmac_pkt_clone);
///	     rd_bk_ap.write(dma_id);
         `uvm_info("get_exp_pcie_rx_pkt",$psprintf("DMAs selected =d'%0d",dma_id),UVM_LOW);
	  end


Ouput:

vem avb_gateway_info =0
vem1 i=0 dma_id=0
.
.
.

vem avb_gateway_info =2
vem1 i=0 dma_id=0
vem1 i=1 dma_id=2

=======================