LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2006, 12:36 PM   #1
BradDaBug
LQ Newbie
 
Registered: May 2002
Posts: 29

Rep: Reputation: 15
Memory leak in aio_read()?


I noticed that Valgrind is not happy about what's going on inside aio_read(). Take a look at this program:
Code:
#include <sys/types.h>
#include <aio.h>
#include <fcntl.h>
#include <iostream>
#include <vector>

using namespace std;

const int SIZE_TO_READ = 100;

vector<aiocb*> cbs;

void fillaiocbs(int count, int size, int offset, int file)
{
	for(int i = 0; i < count; i++)
	{
		aiocb* cb = new aiocb;
		memset(cb, 0, sizeof(aiocb));
		cb->aio_nbytes = SIZE_TO_READ;
		cb->aio_fildes = file;
		cb->aio_offset = 0;
		cb->aio_buf = new char[size];
		
		cbs.push_back(cb);
	}
}

void deletecbs()
{
	for (int i = 0; i < cbs.size(); i++)
	{
		delete[] (char*)cbs[i]->aio_buf;
		delete cbs[i];
	}
}

int main()
{
	// open the file
	int file = open("something or other", O_RDONLY, 0);
	
	if (file == -1)
	{
		cout << "Unable to open file!" << endl;
		return 1;
	}
	
	// create the buffer
	//char* buffer = new char[SIZE_TO_READ];
	
	// create the control block structure
	fillaiocbs(1000, SIZE_TO_READ, 0, file);
	
	// read!
	for (int i = 0; i < cbs.size(); i++)
	{
	
		if (aio_read(cbs[i]) == -1)
		{
			cout << "Unable to create request!" << endl;
			close(file);
		}
	}
	
	cout << "Request enqueued!" << endl;
	
	// wait until the request has finished
	for (int i = 0; i < cbs.size(); i++)
	{
		while(aio_error(cbs[i]) == EINPROGRESS)
		{
			cout << "Working..." << endl;
		}
	}
	
	// success?
	/*int numBytes = aio_return(cb);
	
	if (numBytes != -1)
		cout << "Success!" << endl;
	else
		cout << "Error!" << endl;
		*/
	
	// now clean up
	//delete cb;
	//delete[] buffer;
	
	deletecbs();
	close(file);
	
	return 0;
}
The code creates 1000 read requests from a certain file and waits for them to complete. Valgrind reports that almost 30KB is leaked inside aio_read().

I'm using Debian Testing with the 2.6.8-2-386 kernel. Is this a known issue? Does anyone else get similar results? Am I doing something wrong?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory leak?! nyk Programming 10 01-24-2005 07:08 PM
memory leak mdk Mandriva 1 09-17-2004 10:54 AM
possible memory leak?? matt80 Linux - General 2 07-01-2004 11:07 PM
Memory Leak when using memory debugging C program on SuSE SLES8 babalina Linux - Distributions 0 10-06-2003 09:39 AM
Memory Leak? eLinux Linux - General 2 09-20-2003 09:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration