Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
I have the following functions :
 
void each_user_specifications :: algorithm_dequeue_stage (single_connection_per_user_queue *q, int tot_queues)
{
	packet_with_attributes *temp1, *temp2;
	unsigned int temp_tot_size = 0;
	int counter = 0, queue_counter = 0;
	int pkt_size = 0, temp_pkt_size = 0;
	static int run_count = 0;
	int ready_to_dequeue_flag = 0, pkt_count = 0, complete_pkt_sent_flag = 0;
 
	run_count ++;
 
	for (int i=0; i < tot_queues; i++)
	{
		cout <<(q+i)->tokens_generated<<" are generated for this queue\n";
		temp1 = (q+i)->head;
 
		while (temp1 != NULL)
		{
			temp_tot_size += temp1->new_packet_size;
			temp1 = temp1->new_packet;
		}
 
		temp1 = (q+i)->head;
 
		while (temp1 != NULL)
		{
			pkt_size = temp1->new_packet_size;
			temp_pkt_size = pkt_size;
 
			while (pkt_size != 0)
			{
				if (temp_tot_size <= (q+i)->tokens_generated)
				{
					//temp2 = temp1;
					ready_to_dequeue_flag = 1;
					break;
				}
 
				pkt_size --;
				temp_tot_size --;
			}
 
			if (ready_to_dequeue_flag == 1)
			{
				(q+i)->tokens_generated = (q+i)->tokens_generated - temp_tot_size;
				(q+i)->current_size -= temp_tot_size;
				(q+i)->sum_tokens += temp_tot_size;
				(q+i)->avg_tokens_lead_or_lagged = (q+i)->sum_tokens/run_count;
 
				if (pkt_size == temp_pkt_size)
				{
					complete_pkt_sent_flag = 1;
					pkt_count ++;
				}
 
				while (temp1->new_packet != NULL)
				{
					temp1 = temp1->new_packet;
					pkt_count ++;
				}
 
				temp2 = temp1;
				ready_to_dequeue_flag = 0;
				break;
			}
 
			temp1 = temp1->new_packet;
		}
 
		temp_tot_size = 0;
 
		while (pkt_count > 0)
		{
			(q+i)->head = (q+i)->dequeue_the_packet ((q+i)->head);
 
			if ((q+i)->head == NULL)
				break;
 
			pkt_count --;
		}
 
		/*temp1 = (q+i)->head;
 
		while (temp1 != NULL)
		{
			temp2 = temp1;
 
			if (temp1->new_packet == NULL)
				break;
 
			temp1 = temp1->new_packet;
		}
 
		temp2 = temp1;*/
 
		if (complete_pkt_sent_flag == 0)
			temp2->new_packet_size = temp_pkt_size - pkt_size;
 
		temp1 = (q+i)->head;
 
		pkt_count = 0;
 
		queue_counter ++;
 
		cout <<"Dequeuing done for queue number "<<queue_counter<<" having class "<<(q+i)->cos<<"\n";
 
		while (temp1 != NULL)
		{
			cout <<temp1->new_packet_size<<"'"<<temp1->new_packet_arrival_time<<"\n";
			temp1 = temp1->new_packet;
		}
 
		system ("PAUSE");
	}
}
 

 

packet_with_attributes* single_connection_per_user_queue :: dequeue_the_packet (packet_with_attributes* temp_head)
{
	packet_with_attributes *old_temp_head1, *old_temp_head2;
	old_temp_head1 = new packet_with_attributes;
	old_temp_head2 = new packet_with_attributes;
 
	old_temp_head1 = temp_head;
	
	while (old_temp_head1->new_packet != NULL)
	{
		old_temp_head2 = old_temp_head1;
		old_temp_head1 = old_temp_head1->new_packet;
	}
 
	old_temp_head2->new_packet = NULL;
 
	if (temp_head == NULL)
		return temp_head;
 
	else 
	{
		delete old_temp_head1;
		return temp_head;
	}
}
 
I am getting error in the last while loop of the first function, although sometimes it's not kicking an error and running freely and dequeuing the packets. But sometimes, it hits the breakpoint and kicks an error of access voilation reading location.
 
I don't know, what I am doing wrong ? I have tried everything posted in answers, please help.
Posted 21-Sep-12 2:13am
Edited 2-Oct-12 21:52pm
DaveAuld171.7K
v6
Comments
Jochen Arndt at 21-Sep-12 8:37am
   
How is init_satq allocated by the initialization function? And what is the value of tot_queues (assuming that it is passed by reference to the initialization function and set their)?
wirelesscomm2k12 at 21-Sep-12 9:29am
   
The function prototype of initialization function is:
 
single_connection_per_user_queue *each_user_specifications :: algorithm_each_user_parameters_initialization_stage (each_user_specifications u[], int obj_array_size, double sched_cycle_system_bandwidth, unsigned int &token_bank_capacity, int &tot_queues)
 
and this function is returning an array of objects belonging to the same class - i.e. single_connection_per_user_queue.
Jochen Arndt at 21-Sep-12 10:04am
   
Because the exception does not occur when called from inside the init function, possible error sources are the involved variables: the returned array pointer and the dimension value tot_queues. To exclude these error sources, you should show some of the code of the init function (or check it yourself). If you use something like *init_satq = new single_connection_per_user_queue[tot_queues] their and did not change tot_queues later, it should be OK.
 
You may add code to your question using the 'Improve question' link rather than trying to post it in comments.
wirelesscomm2k12 at 21-Sep-12 9:30am
   
And yes, you are right, the variable tot_queues is passed by reference.
pasztorpisti at 21-Sep-12 9:31am
   
Providing more code might help if the other parts are not top secret.
wirelesscomm2k12 at 21-Sep-12 9:36am
   
I don't know, whats going on, their server is not responding. So, it's not allowing me to post text in large quantities. But, I will keep trying and you can read my above comments, if it helps you. In the mean time, I am trying to post the whole code of function.
Richard MacCutchan at 21-Sep-12 11:29am
   
Are you sure that obj_array_size is always 45 or less. Since you do not check it, it is always possible that your index value is going out of bounds.
wirelesscomm2k12 at 21-Sep-12 12:09pm
   
Yes, because this is working when I am calling algorithm_enqueue_stage () from algorithm_parametters_initialization_stage () instead of returning satq from algorithm_parameters_initialization_stage () and again calling algorithm_enqueue_stage using one of the arguments as satq.
Richard MacCutchan at 21-Sep-12 12:57pm
   
That does not help. You need to use your debugger to check the values of your parameters and try to find the exact point where the access violation occurs.
wirelesscomm2k12 at 21-Sep-12 12:11pm
   
Do I have to allocate heap space for every pointer and freeing at proper point in my program or I can only declare them ?
Richard MacCutchan at 21-Sep-12 12:58pm
   
You don't allocate space for pointers, you allocate space for objects or arrays. Only you can determine whether you need to allocate space as it depends what your code is trying to do.
lewax00 at 21-Sep-12 13:02pm
   
Not sure if it's related to your problem of not, but I'd like to point out in these lines:
 
u[k].q[i].token_bucket_min_depth = u[k].q[i].MRTR * 1000 * 5/8;
u[k].q[i].token_bucket_avg_depth = u[k].q[i].STR * 1000 * 5/8;
u[k].q[i].token_bucket_max_depth = u[k].q[i].MSTR * 1000 * 5/8;
 
5/8 will evaluate to 0 as it's written. So all three of them become 0 (0 * 1000 * other value = 0). You should change it to "5./8.", "5.0/8.0", ".625", replace "1000 * 5/8" with "625", or something along those lines if you actually want something other than 0.
wirelesscomm2k12 at 2-Oct-12 23:13pm
   
I have tried everything, which has been suggested on this question, but I am again getting the same problem now.
Richard MacCutchan at 3-Oct-12 4:21am
   
Your function dequeue_the_packet looks suspiciously incorrect to me, although I am not sure that I understand what it's trying to do. You allocate two new packet_with_attributes at the beginning and then immediately overwrite their pointers thus leaking that memory.
wirelesscomm2k12 at 3-Oct-12 6:20am
   
I am trying to dequeue already queued packets in different queues using dequeue_the_packet function.
Richard MacCutchan at 3-Oct-12 7:31am
   
OK, I figured it out. I think you need to capture the break and check the values of all your pointers at the point of failure. I'm also not sure exactly where the failure occurs, please edit your question and add some comments on the exact line that causes the crash.
wirelesscomm2k12 at 3-Oct-12 20:54pm
   
I am using Microsoft Visual Studio 2010, and in the first function, it always kicks some kind of error. It was hitting break point in the last while loop so I commented it, but again, it hit the break point. And, it's always hitting the break point, whenever it encounters statement like:
 
temp1 = temp1->new_packet;
 
And, it's always revolving arround only 2 kinds of errors:
 
1. Unhandled exception access voilation reading location error.
2. Heap corruption error and hitting the break point in a file called malloc.c.
 
Richard MacCutchan at 4-Oct-12 3:29am
   
As I said before, you need to capture all the values of your variables to try and figure out which ones are not correct and causing the problem. It's impossible to figure it out just from looking at the code above.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I am suspicous of the function dequeue_the_packet.
 
You do this...
 
old_temp_head1 = temp_head;

while (old_temp_head1->new_packet != NULL)
 
and later..
 
if (temp_head == NULL)
return temp_head;
 
Now you should already have had an access violation if temp_head == NULL when you executed old_temp_head1->new_packet
 
So maybe this function has a typo in it or an other bug, and that is leading to your data corruption.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I can see a few problems here. Please make these corrections and re-run:
 
1. Make packet_with_attributes* single_connection_per_user_queue :: dequeue_the_packet (packet_with_attributes* temp_head) static function; (I will explain the reason for this below)
 
2. As suggested before, get rid of these two lines:
	old_temp_head1 = new packet_with_attributes;
	old_temp_head2 = new packet_with_attributes;
and this line:
delete old_temp_head1;
within dequeue_the_packet() function.
Basically, forget about this. The function body should look like this (if I understand it correctly):
{
	while (temp_head && temp_head->new_packet != NULL)
	{
		temp_head = temp_head->new_packet;
	}
 
	if(temp_head) temp_head->new_packet = NULL;
 
	return temp_head;
}
 
3. Now, in the algorithm_dequeue_stage() function you need to change this line:
(q+i)->head = (q+i)->dequeue_the_packet ((q+i)->head);
to:
(q+i)->head = packet_with_attributes::dequeue_the_packet ((q+i)->head);
 
Now, let me explain the reason for making the function static. See, how you fiddling with the temp_head object you are passing to the function. At some stage the object may become NULL. This means that it will be invalid and if the function is called within this object there will be a crash.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Thanks for taking your time and posting solutions in such a brief manner. Although, I haven't tried any one of the two solutions. But, I am going to try these, and I am pretty sure that they are going to solve this problem because they look completely different. I mean, this time, the way to solve this problem looks completely different.
 
Once again, thanks a lot for posting such a brief solutions.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,631
1 Sergey Alexandrovich Kryukov 6,345
2 CPallini 5,230
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 11 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100