|
-
February 17th, 2008, 12:56 PM
#1
Ghost variable
I have a problem with declaring a variable in a constructor. The problem I get is that, although the code compiles and runs (with no exceptions raised or anything like that), when I pause the code with my debugger, the variable tagsize is non-existent. In fact, none of the local variables declared within the constructor are there!
I tried moving tagsize and renaming it (it was originally called 'size') to no avail. (I chose tagsize as the one to investigate as it was the one causing the code to malfunction and, until writing this post, I hadn't thought about my other local variables).
My code for the constructor is as follows:
Code:
ID3v2::ID3v2(char* file) {
//Open File
//Check file exists
if (!FileExists(file)) {
//File does not exist - exit
return;
}
FILE* f;
f = fopen (file, "rb");
unsigned char* readin;
int tagsize = 0;
while (feof(f) == 0) {
//Find "ID3" ($49 44 33)
//While (feof(f) == 0) used because loop is controlled by
//continue and break statements. The feof control ensures
//the loop exits if the end of file is reached
while (feof(f) == 0) {
readin = new unsigned char[10];
fread (readin, 1, 10, f);
int pos49 = 0;//inArray(0x49, readin);
//Not present
if (pos49 < 0) {
//Continue searching
continue;
}
//Present, but rest of "ID3" is beyond end of array
if (pos49 > 7) {
//Seek to start of potential "ID3" and continue searching
//(Will pick up on 0x49 next iteration)
fseek(f, pos49 - 10, SEEK_CUR);
continue;
}
//Found "ID3"
if (readin[pos49 + 1] == 0x44 && readin[pos49 + 2] == 0x33) {
//Seek to start and break loop
fseek(f, pos49 - 10, SEEK_CUR);
break;
}
}
//Check for end of file
if (feof(f) != 0) {
break;
}
//Position is now at the start of the tag
try {
processTagHeader(f, tagsize);
while (tagsize > 0) {
try {
processFrame(f, tagsize);
} catch (eInvalidFrameHeader e) {
//Error processing frame header, skip rest of tag
cout << e.what() << endl;
break;
}
}
} catch (eInvalidTagHeader e) {
//Error processing tag header - skip entire tag
//(achieved by position of this catch)
cout << e.what() << endl;
}
}
}
Anyone see any reason why this shouldn't work?
-
February 17th, 2008, 01:26 PM
#2
Re: Ghost variable
 Originally Posted by fatlardo
I have a problem with declaring a variable in a constructor. The problem I get is that, although the code compiles and runs (with no exceptions raised or anything like that),
It is not correct -- it has a memory leak:
Code:
readin = new unsigned char[10];
There is no need to call new here. All you need to do is create a character array:
In addition, your constructor does too much work. What if something fails doing all of those things? How will the person using the class know that your object is a "zombie" (dead, but it's alive)? For cases like this, throw an exception, or move this stuff out of the constructor into another method that must be called.
when I pause the code with my debugger, the variable tagsize is non-existent. In fact, none of the local variables declared within the constructor are there!
So the problem is with your debugger? First, you didn't mention what debugger, what compiler, etc. There are many debuggers and compilers out there.
Second, this is a debugger issue, not an issue with your code. Maybe you didn't compile with debugging information, maybe you're debugging a release build, maybe your debugger is a pile of junk, we don't know. If this were a programming problem, then you would get a definitive answer, but you stated that your program runs correctly.
Regards,
Paul McKenzie
-
February 18th, 2008, 05:13 AM
#3
Re: Ghost variable
I am using the gcc compiler (the latest stable version, i'm not at my computer at the moment so I cant find out exactly which).
Again, with the no computer problem! I don't know which debugger I'm using. I'm using Code::Blocks as my IDE. I know that it's just a structure for plugins, but I dont know if it prefers any particular debugger (I'm running Ubuntu Gutsy Gibbon and haven't installed any additional debuggers).
I declare readin like that because I have previously declared it as .
I have never had a problem with my debugger. I will try it on something I do know works though.
I never said my program runs correctly. It compiles, and doesn't throw up any errors, but the tagsize variable seems to be set to <= 0 when a previous method sets it higher than this (I have traced through to check this).
-
February 18th, 2008, 05:31 AM
#4
Re: Ghost variable
How about just inserting a couple lines of
Code:
std::cerr << tagsize << std::endl
into your code? That way you will definitely see, if the problem is with your code or with your debugger.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
February 18th, 2008, 02:04 PM
#5
Re: Ghost variable
I realised that when I got back to my computer. Turns out another routine was altering the tagsize variable making it 2^32 + 2 :S Anyway fixed that and my code works fine now.
Still no joy with the debugger, but now I definately know it was the debugger. I might try a few different ones to see if I can find a better one.
Thanks for your help
-
February 18th, 2008, 02:17 PM
#6
Re: Ghost variable
Out of curiosity, but which debugger (including version) did you use?
-
February 18th, 2008, 02:24 PM
#7
Re: Ghost variable
 Originally Posted by fatlardo
Still no joy with the debugger, but now I definately know it was the debugger. I might try a few different ones to see if I can find a better one.
More likely than that the debugger is broken, there is something wrong with your project settings. Either you do not generate debug information, or you have optimization turned on (which will often eliminate a lot of local variables). Check with what command line your code is actually built, if it includes -O, -O2 or -O3 then the problem is probably there.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|