Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

Strange memory leak in C++[Solved]

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
10 posts • Page 1 of 1
Author
Message
MdaG
l33t
l33t
User avatar
Posts: 945
Joined: Tue Nov 09, 2004 7:58 pm
Location: Stockholm, Sverige

Strange memory leak in C++[Solved]

  • Quote

Post by MdaG » Fri Mar 10, 2006 11:08 am

I'm probably doing a real n00b error here, but here goes.

At the beginning of my code I query the user if he/she wants to import data from file, but the code never gets past the cin-part. I have to manually terminate the process from there. The printf-statement never executes and when I look at my system monitor in Gnome it shows a huge memory leakage (from 120Mb to ~730Mb). My DRAM is 768 Mb on my machine. What could be the problem, anyone?

Code:

Code: Select all

.
.
.
int main() {
	float Q[N_STATES][N_ACTIONS];				// State-action pair table
	char	ans;										// Query for save and load data.
	
	/* Seed the random generator */
	srand48(time(0));								
	
	/* Initialize the Q-table */
	cout << "Read data from file? (y/n) : "; <----- I know it get's here
	cin >> ans;
	printf("bajs");     <----- Never gets here
	if (ans == 'y')
		loadData(Q);
	else 
		init(Q);

	/* Do Q-learning algorithm */
	oneStepQ(Q);
	
	/* Store "experience" */
	cout << "Store data on file? (y/n) : ";
	cin >> ans;
	if (ans == 'y')
		saveData(Q);

	return 0;
}
.
.
.
This is what I get when I try to run the code:

Code: Select all

$ ./detach_left
Read data from file? (y/n) : n
afterwards everything is just painfully slow and I see the memory leakage...
Last edited by MdaG on Fri Mar 10, 2006 12:31 pm, edited 2 times in total.
Top
GordSki
Guru
Guru
User avatar
Posts: 329
Joined: Mon Oct 18, 2004 7:48 pm
Contact:
Contact GordSki
Website

  • Quote

Post by GordSki » Fri Mar 10, 2006 11:35 am

Could the problem be in init()? One thing to note is that printfs don't always make it to the screen when they are called. It can take time for stdout to get flushed and the text to hit the screen, especially if you go into some memory hungry busy loop......

G.
Top
MdaG
l33t
l33t
User avatar
Posts: 945
Joined: Tue Nov 09, 2004 7:58 pm
Location: Stockholm, Sverige

  • Quote

Post by MdaG » Fri Mar 10, 2006 11:41 am

My init() looks like this:

Code: Select all

void init(float Q[N_STATES][N_ACTIONS]) {
	int i, j;
	for (i = 0; i < N_STATES; i++)
		for (j = 0; j < N_ACTIONS; j++)
			Q[i][j] = 0.0;
}
Top
GordSki
Guru
Guru
User avatar
Posts: 329
Joined: Mon Oct 18, 2004 7:48 pm
Contact:
Contact GordSki
Website

  • Quote

Post by GordSki » Fri Mar 10, 2006 11:55 am

How big are N_STATES and N_ACTIONS?

G.
Top
MdaG
l33t
l33t
User avatar
Posts: 945
Joined: Tue Nov 09, 2004 7:58 pm
Location: Stockholm, Sverige

  • Quote

Post by MdaG » Fri Mar 10, 2006 11:56 am

constants.h

Code: Select all

#define N_PHI 181								// Number of different angles (degrees)
#define N_DIST 51								// Number of different distances (cm)
#define N_STATES N_DIST*N_PHI				// Number of states equal to nCM * nDeg
#define N_ACTIONS 4							// Number of different actions
#define PRECISION 6							// Floating point precision
#define GAMMA 0.9								// Discount factor for critic
#define ALPHA 0.1								// Learning rate scaling factor
#define LAMBDA 0.9							// Trace decay parameter
#define EPSILON 0.1							// Greedy factor
#define MAX_EPISODES 1000					// Maximum runs before quitting
#define MAX_STEPS 20000						// Maximum steps before failure
Top
swingman
Tux's lil' helper
Tux's lil' helper
Posts: 88
Joined: Wed Mar 19, 2003 7:13 am
Location: Sweden

  • Quote

Post by swingman » Fri Mar 10, 2006 11:57 am

Hmmm... output buffering... mixing <iostream> and <stdio.h>

I suggest changing the printf to a cout << msg << endl; or as a minimum add fflush(stdout); after printf.
_
/Bjorn
Top
GordSki
Guru
Guru
User avatar
Posts: 329
Joined: Mon Oct 18, 2004 7:48 pm
Contact:
Contact GordSki
Website

  • Quote

Post by GordSki » Fri Mar 10, 2006 12:01 pm

init() looks ok too. The other thing you could do is try stepping through the program with gdb and see what's going on.

G.
Top
MdaG
l33t
l33t
User avatar
Posts: 945
Joined: Tue Nov 09, 2004 7:58 pm
Location: Stockholm, Sverige

  • Quote

Post by MdaG » Fri Mar 10, 2006 12:02 pm

swingman wrote:Hmmm... output buffering... mixing <iostream> and <stdio.h>

I suggest changing the printf to a cout << msg << endl; or as a minimum add fflush(stdout); after printf.
_
/Bjorn
Thanks. Using printf() together with cout did some strange things. I know now that the memory leak isn't due to cin. :)

*edit*
I have cornered the problem to a line where I set:

Code: Select all

lastState = thisState;
where lastState and thisState are instances of State. I have overloaded the operator '=' with the following code:

Code: Select all

State State::operator =(const State& s) const {
	return State(s.getDistance(), s.getAngle());
}
Top
swingman
Tux's lil' helper
Tux's lil' helper
Posts: 88
Joined: Wed Mar 19, 2003 7:13 am
Location: Sweden

  • Quote

Post by swingman » Fri Mar 10, 2006 12:10 pm

GordSki wrote:init() looks ok too. The other thing you could do is try stepping through the program with gdb and see what's going on.

G.
Or just attach gdb to the currently running process. Just make sure you compile with symbol info, so you can see where the program is stuck.
_
/Bjorn
Top
MdaG
l33t
l33t
User avatar
Posts: 945
Joined: Tue Nov 09, 2004 7:58 pm
Location: Stockholm, Sverige

  • Quote

Post by MdaG » Fri Mar 10, 2006 12:12 pm

swingman wrote:
GordSki wrote:init() looks ok too. The other thing you could do is try stepping through the program with gdb and see what's going on.

G.
Or just attach gdb to the currently running process. Just make sure you compile with symbol info, so you can see where the program is stuck.
_
/Bjorn
I've never used gdb, what is it? *googling*

*edit*
Nevermind, I think I've figured out how to use gdm. Thanks.

*edit*
I found the error. I had a faulty destructor in the class code of State.
Top
Post Reply

10 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic