Register | Login | |||||
Main
| Memberlist
| Active users
| Calendar
| Chat
| Online users Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album |
| |
0 users currently in Programming. |
Acmlm's Board - I3 Archive - Programming - Realloc() fails for some reason | New poll | | |
Add to favorites | Next newer thread | Next older thread |
User | Post | ||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6431 days Last view: 6431 days |
| ||
One of those fun bugs that makes no sense whatsoever.
NumTags++;
You can probably see that what this is supposed to do is allocate enough memory to hold NumTags XMLTag structures. However, for some reason it's failing: Init
It's only trying to allocate 156 bytes, but it fails. However, I added this above: BYTE* blah = NULL;
This works just fine, so obviously there's at least 2K available. |
|||
Cellar Dweller + Red Koopa Since: 11-18-05 From: Arkansas Last post: 6440 days Last view: 6431 days |
| ||
Exactly how sure are you that your program is not writing outside of allocated memory? If it is writing outside of allocated memory, it could be corrupting the heap and causing memory allocation functions to fail. | |||
MathOnNapkins 1100 In SPC700 HELL Since: 11-18-05 Last post: 6430 days Last view: 6430 days |
| ||
I've used realloc() a lot over the past few days.
First of all, make sure you pass it a pointer that was previously allocated by malloc( ) or calloc( ), etc. Next, realloc can fail sometimes b/c of the way memory is allocated on your system. Is it windows 98? I know I had to put: _set_sbh_threshold(0); at the beginning of one of my programs to keep realloc from acting up. Thirdly, I think you may be confused as to the proper size of your Tag pointer. An actual XMLTag structure might be pretty large, potentially. Am I right? Because I don't know the actual size of XMLTag structures. I think you are allocating too much memory by using sizeof(XMLTag) in your third line. I think it should be sizeof(XMLTag*) instead. That would mean you are dynamically allocating a list of pointers to (NumTags) many different XMLTags. Am I right about that? If that was your goal then make that modification and see how it runs. Alternately, you could be allocating all your XMLTag structures together in a linear buffer maybe? But that is sort of a counter intuitive way of going about it, imo. It would also require more hassle in coding such a buffer. If none of these ideas work I'm not sure what else to say except look up documentation on realloc and look at the number of reasons it can fail. There are probably a lot.. |
|||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6431 days Last view: 6431 days |
| ||
It's XP, which has never given me any memory problems before (except with video memory ). I think you're probably right about using XMLTag* there, now that I look at it again. *tinkers a bit*
Yeah, I think it's writing outside of the array. Tsk tsk. BTW, you can pass realloc() a NULL pointer; it'll just act like malloc(). [edit] Yep. A completely different part of the code was writing wrong (Tag[T].Attrib[ So yeah, yay me or something. (edited by HyperHacker on 12-12-05 11:15 PM) (edited by HyperHacker on 12-12-05 11:16 PM) |
|||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6431 days Last view: 6431 days |
| ||
OK, here's another fun one. fp is a FILE* and a private member of a class. The class has an Open() method which opens a file (using fp = fopen()). It also has a Close() method, guess what it does? I create one instance of this class by declaring it in the header file of my program. I call Open(), do some stuff with it, la dee da, then I want to open a different file in another function, so I call Close()... it crashes at fclose(fp). fp is still the same value that it was after fopen(), and the file is still open (at least, I never closed it), but as soon as it tries to close the file the entire program just quits with no error message. Is there any possible reason besides stack/memory corruption for this to happen? I already debugged the hell out of all the allocating routines.
Code follows: class XMLReader
*thwaps board* With the weird line break handling, I get to choose either having tabs in my < (edited by HyperHacker on 12-15-05 03:18 AM) |
|||
sloat Since: 11-18-05 From: Delaware, US Last post: 6534 days Last view: 6534 days |
| ||
don't you have to use "this" with everything...or am i stuck in php mode?
also, what exception is it giving you? |
|||
neotransotaku Sledge Brother Liberated from school...until MLK day Since: 11-17-05 From: In Hearst Field Annex... Last post: 6433 days Last view: 6430 days |
| ||
Is XML a global variable? Right now, the only thing I can think of is you aren't passing what you are think are passing to the function that calls XML.Reset() | |||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6431 days Last view: 6431 days |
| ||
'this' is just a pointer to the class itself. I imagine you could use it, but you don't need to. XML is global. I don't think it's a problem with the function that calls Reset(), because the last output is "Close file...", right before the fclose() call (and the output right after fclose() doesn't occurr - removing it doesn't help either, so it's not the problem).
The annoying part is it doesn't give any exception. The program just closes and returns some random number. |
|||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6431 days Last view: 6431 days |
| ||
Pardon the bump but I did some more digging. First here's a disassembly of the problem function:
004026EE push ebp
When run in the debugger, it does display an exception: Unhandled exception in sdl3dtest.exe (NTDLL.DLL): 0xC0000005: Access Violation. Finally, a stack dump: ntdll.dll!wcsncpy+0x354
I tested a bit, and the class is able to reset just fine in a small test program, so I imagine there's some sort of memory/stack corruption going on. The file pointer hasn't been changed. Don't hesitate to ask if you need more info. [edit] I did some testing, and found interesting results... if you just pass some arbitrary pointer to fclose(), it crashes just like this does (access violation and all), but it only gets to msvcrt.dll!fclose+0x37 instead of ntdll.dll!wcsncpy+0x354, and actually shows the 'fatal error, close or debug' message. More interesting is that if you open a file, then try to close it twice, nothing bad happens. I don't think this really means anything, though, because I'm positive the pointer is correct... I even looked at the memory in the debugger and made sure of it. I wonder if some small problem in my code could have triggered some large problem in Windows'? [edit 2] Hah! Figured out why it wasn't showing any error... I forgot SDL has a built-in exception handler, which apparently decided to just kill the program. With that off, I do get an error: The instruction at "0x7c910de3" referenced memory at "0x7473654c". The memory could not be "read". (edited by HyperHacker on 12-18-05 07:05 PM) (edited by HyperHacker on 12-18-05 10:40 PM) |
|||
sloat Since: 11-18-05 From: Delaware, US Last post: 6534 days Last view: 6534 days |
| ||
Originally posted by HyperHacker It looks like 0x7473654C is a string that's getting dereferenced too many times, or in this case, probably overwriting your file pointer. If you convert it to ascii you get "tseL". But remember that an x86 processor always reads addresses from low to high, so the actual string that it thinks is a pointer is "Lest". I dunno if that has any significance to you, but to me it would be a starting point. |
|||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6431 days Last view: 6431 days |
| ||
Yeah, I checked that out, but no such strings were anywhere to be found. It actually turned out I'd declared something as an array of pointers to structs when it should have been an array of structs.
Also, I managed to cause a BSOD with an IRQL_NOT_LESS_OR_EQUAL error by adding some Beep()s around another area that was segfaulting (trying to read from address 0). BTW, if you have any idea how, write your own exception handler (look at SetUnhandledExceptionFilter). It makes things so freaking much easier. (edited by HyperHacker on 12-19-05 05:22 AM) |
|||
MisterJones Tooky Since: 12-08-05 From: Mexico Last post: 6520 days Last view: 6456 days |
| ||
Closing a file twice with fclose() should not be a problem:
Originally posted by "Standard Library" Tried to eval result? Have you already tried using a breakpoint there and go step by step checking? Would be a little stupid from me to say to use stream classes rather than FILE pointers? try/catch already? (edited by MisterJones on 12-19-05 10:56 AM) |
|||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6431 days Last view: 6431 days |
| ||
Yeah, I fixed it already.
Originally posted by HyperHacker Memory corruption causes really weird problems. |
Add to favorites | Next newer thread | Next older thread |
Acmlm's Board - I3 Archive - Programming - Realloc() fails for some reason | | |