tags:

views:

149

answers:

3
  • Platform : Win32
  • Language : C++

I get an error if I leave the program running for a while (~10 min).

Unhandled exception at 0x10003fe2 in ImportTest.exe: 0xC0000005: Access violation reading location 0x003b1000.

I think it could be a memory leak but I don't know how to find that out.

Im also unable to 'free()' memory because it always causes (maybe i shouldn't be using free() on variables) :

Unhandled exception at 0x76e81f70 in ImportTest.exe: 0xC0000005: Access violation reading location 0x0fffffff.

at that stage the program isn't doing anything and it is just waiting for user input

dllHandle = LoadLibrary(L"miniFMOD.dll");

playSongPtr = (playSongT)GetProcAddress(dllHandle,"SongPlay");
loadSongPtr = (loadSongT)GetProcAddress(dllHandle,"SongLoadFromFile");

int songHandle = loadSongPtr("FILE_PATH");

// ... {just output , couldn't cause errors}

playSongPtr(songHandle);
getch(); // that is where it causes an error if i leave it running for a while

Edit 2:

playSongPtr();

causes the problem. but i don't know how to fix it

+1  A: 

The error tells you that memory is accessed which you have not allocated at the moment. It could be a pointer error like dereferencing NULL. Another possibility is that you use memory after you freed it.

The first step would be to check your code for NULL reference checks, i.e. make sure you have a valid pointer before you use it, and to check the lifecycle of all allocated and freed resources. Writing NULL's over references you just freed might help find the problem spot.

rsp
+1  A: 

I think it's pretty clear that your program has a bug. If you don't know where to start looking, a useful technique is "divide and conquer".

Start with your program in a state where you can cause the exception to happen. Eliminate half your code, and try again. If the exception still happens, then you've got half as much code to look through. If the exception doesn't happen, then it might have been related to the code you just removed.

Repeat the above until you isolate the problem.

Update: You say "at that stage the program isn't doing anything" but clearly it is doing something (otherwise it wouldn't crash). Is your program a console mode program? If so, what function are you using to wait for user input? If not, then is it a GUI mode program? Have you opened a dialog box and are waiting for something to happen? Have you got any Windows timers running? Any threads?

Update 2: In light of the small snippet of code you posted, I'm pretty sure that if you try to remove the call to the playSongPtr(songHandle) function, then your problem is likely to go away. You will have to investigate what the requirements are for "miniFMOD.dll". For example, that DLL might assume that it's running in a GUI environment instead of a console program, and may do things that don't necessarily work in console mode. Also, in order to do anything in the background (including playing a song), that DLL probably needs to create a thread to periodically load the next bit of the song and queue it in the play buffer. You can check the number of threads being created by your program in Task Manager (or better, Process Explorer). If it's more than one, then there are other things going on that you aren't directly controlling.

Greg Hewgill
it is a console application and i'm using getch(); from conio.h to pause the program. i'm waiting for user to press a key so the program can carry on running. and no , i didn't use any threads in this program.
Nick Brooks
Well, by itself that's not quite enough information for us to guess what might be happening. Could you post some code that demonstrates this problem?
Greg Hewgill
updated main post
Nick Brooks
Any chance you start a new thread in playSongPtr method? I suspect it's the culprit.
Raymond
but the program crashes long after the song finished playing
Nick Brooks
That doesn't mean anything. Did you try removing the call to `playSongPtr()`?
Greg Hewgill
trying it right now , but it takes a while for it to crash so i'll have to wait and see
Nick Brooks
seems to be working , but how do i make it so i can use playsongptr ?
Nick Brooks
A: 

I doubt this particular problem is a memory leak; the problem is dereferencing a pointer that does not point to something useful. To check for a memory leak, watch your process in your operating system's process list tool (task manager, ps, whatever) and see if the "used memory" value keeps growing.

On calling free: You should call free() once and only once on the non-null values returned from malloc(), calloc() or strdup(). Calling free() less than once will lead to a memory leak. Calling free() more than once will lead to memory corruption.

You should get a stack trace to see what is going on when the process crashes. Based on my reading of the addresses involved you probably have a stack overflow or have an incorrect pointer calculation using a stack address (in C/C++ terms: an "auto" variable.) A stack trace will tell you how you got to the point where it crashed.

janm