-
June 22nd, 2007, 08:33 AM
#1
Problem copying linked list (memory error)
Hi all,
I seem to be having a problem where I get memory errors upon calling the line below (#*#*#) in this function:
First-chance exception at 0x00569f26 in ScrollingPlot.exe: 0xC0000005: Access violation reading location 0x1502b60c.
First-chance exception at 0x00569f26 in ScrollingPlot.exe: 0xC0000005: Access violation reading location 0x1502b60c.
First-chance exception at 0x7c812a5b in ScrollingPlot.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
First-chance exception at 0x7c812a5b in ScrollingPlot.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
Unhandled exception at 0x00569f26 in ScrollingPlot.exe: 0xC0000005: Access violation reading location 0x1502b60c.
Code:
SingleList<double> * arrayList;
...
arrayList = new SingleList<double>[numwindows];
Code:
void ScrollingPlotFrame::sampleRate(GraphWin *child, int index, int rate)
{
SingleList<double> *list;
SingleList<double> *tempList;
tempList = new SingleList<double>;
SingleNode<double> * current;
list = &arrayList[index];
if(list!=NULL)
{
current = list->head(); // *#*#* crashing here when creating new window #*#*#
bool atleastone=false; // only pass back list if there is at least one value
int ratecount = 0;
int count=0;
if(current != NULL)
{
double avg = current->xretrieve();
while(current != NULL)
{
if( ratecount == 0) //retrieve data
avg = current->xretrieve();
if(ratecount == rate-1) //average values and add to list
{
ratecount = 0;
double val = avg/double(rate);
tempList->push_back(val,0);
atleastone=true;
//timer_.Stop();
}
else //keep running total
{
avg = avg + current ->next() ->xretrieve();
ratecount++;
}
current = current->next();
count++;
}
if(atleastone)
{
child->setList(*tempList,index); //end of list, so send averaged data list to child window
}
}
child->PlotIt(false); //replot
}
else
{
wxString newtext;
newtext = wxT(" NULL LIST");
wxMessageBox(newtext,
_T("Modeless dialog"),
wxOK | wxICON_INFORMATION, this);
}
for(int c=0;c<=tempList->size()-1;c++)
{
tempList->pop_front();
}
delete tempList;
list=NULL;
delete[]list;
delete current;
}
What this function is doing is taking data (linked list) which was read from a file and averaging every 'rate' number of them together. It then adds the average into a new linked list which it passes into the child window to plot on the screen.
Everything runs perfectly fine when I don't include the lines after #*#*#, and I access arrayList in other locations, and it is built/constructed fine since everything else works perfectly.
I almost think it is because I am assigning one instance of the array of linked lists to a single linked list or something, but I don't know whats wrong or how to fix it. Any ideas?
Thanks!!
-
June 22nd, 2007, 08:39 AM
#2
Re: Problem copying linked list (memory error)
First guess would be index is an invalid value. What clues does your debugger provide?
-
June 22nd, 2007, 08:49 AM
#3
Re: Problem copying linked list (memory error)
-
June 22nd, 2007, 08:49 AM
#4
Re: Problem copying linked list (memory error)
Debugger gives no clues other than those memory errors I posted above.
It is not an index problem, because I actually pass in the index from the child window it is associated with, and the same index number is used in other functions with no problems.
-
June 22nd, 2007, 08:58 AM
#5
Re: Problem copying linked list (memory error)
Originally Posted by Lucky75
Debugger gives no clues other than those memory errors I posted above.
With all due respect, that's very likely not correct. You've got some memory you're trying to access somewhere that doesn't belong to you. By examining your pointers, locals and call stack, you should be able to get a pretty good idea what's wrong.
-
June 22nd, 2007, 09:03 AM
#6
Re: Problem copying linked list (memory error)
Here is what I have in my output:
Code:
'ScrollingPlot.exe': Loaded 'C:\Documents and Settings\<user>\My Documents\Visual Studio 2005\Projects\ScrollingPlot\ScrollingPlot\Debug\ScrollingPlot.exe', Symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\version.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2982_x-ww_ac3f9c03\comctl32.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\shlwapi.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\comdlg32.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\shell32.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\ole32.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\oleaut32.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\uxtheme.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\nview.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\psapi.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\olepro32.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\ddraw.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\dciman32.dll', No symbols loaded.
10:01:48: ..\..\src\msw\mdi.cpp(1394): 'SendMessage(WM_MDISETMENU)' failed with error 0x00000000 (the operation completed successfully.).
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\msftedit.dll', No symbols loaded.
'ScrollingPlot.exe': Loaded 'C:\WINDOWS\system32\nviewimg.dll', Binary was not built with debug information.
'ScrollingPlot.exe': Unloaded 'C:\WINDOWS\system32\nviewimg.dll'
First-chance exception at 0x00567d26 in ScrollingPlot.exe: 0xC0000005: Access violation reading location 0x14f363cc.
First-chance exception at 0x00568096 in ScrollingPlot.exe: 0xC0000005: Access violation reading location 0x00000010.
First-chance exception at 0x00568096 in ScrollingPlot.exe: 0xC0000005: Access violation reading location 0x00000010.
First-chance exception at 0x00568096 in ScrollingPlot.exe: 0xC0000005: Access violation reading location 0x00000010.
First-chance exception at 0x00568096 in ScrollingPlot.exe: 0xC0000005: Access violation reading location 0x00000010.
First-chance exception at 0x00568096 in ScrollingPlot.exe: 0xC0000005: Access violation reading location 0x00000010.
First-chance exception at 0x00567d26 in ScrollingPlot.exe: 0xC0000005: Access violation reading location 0x14f363cc.
First-chance exception at 0x7c812a5b in ScrollingPlot.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
First-chance exception at 0x7c812a5b in ScrollingPlot.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000..
Unhandled exception at 0x00567d26 in ScrollingPlot.exe: 0xC0000005: Access violation reading location 0x14f363cc.
The program '[2284] ScrollingPlot.exe: Native' has exited with code 0 (0x0).
The problem that I am having is that its a runtime error.....so hard to debug.
Everything is fine, no memory errors or anything until I include the code below the line I indicated. I have accessed arrayList[index] many times as well, so I just don't see how it can be anything else but the assignment. My operator overloading in my linked list class works well at least for when I am assigning one linked list to another, so the fact that it is an array of linked lists is the only thing I can think of that is different.
Last edited by Lucky75; June 22nd, 2007 at 09:07 AM.
-
June 22nd, 2007, 09:07 AM
#7
Re: Problem copying linked list (memory error)
If you run in the debugger it will stop on the line that crashes. Then you can look around at the state of your application to determine what went wrong.
-
June 22nd, 2007, 09:16 AM
#8
Re: Problem copying linked list (memory error)
That is running it in the debugger.
It crashes at the head() function of my linked list class saying there was an access violation, which is why i was pointing you to the "current = list->head();" line of code
Code:
template <typename Object>
class SingleList {
private:
int count;
SingleNode<Object> * list_head,
* list_tail,
* current,
* returnNode;
void Init();
public:
SingleList();
SingleList( const SingleList & list );
~SingleList();
SingleList & operator = ( const SingleList & rhs );
// Accessors
int size() const;
bool empty() const;
SingleNode<Object> * head() const;
SingleNode<Object> * tail() const;
// Mutators
void push_front( const Object & objx, const Object & objy );
void push_back( const Object & objx, const Object & objy );
SingleNode<Object> * pop_front();
};
template <typename Object>
SingleList<Object> & SingleList<Object>::operator = ( const SingleList<Object> & rhs )
// empty the current object (destructor)
// make a copy of the list rhs (copy constructor)
{
while(!empty()) // delete nodes
this->pop_front();
current = rhs.list_head; // copy nodes
for(int x=0; x<=rhs.count-1; x++)
{
push_back(current -> xelement, current -> yelement);
current = current -> next_node;
}
return *this;
}
//returns head of list
template <typename Object>
SingleNode<Object> * SingleList<Object>::head() const {
return list_head;
}
Last edited by Lucky75; June 22nd, 2007 at 09:29 AM.
-
June 22nd, 2007, 09:29 AM
#9
Re: Problem copying linked list (memory error)
Put a breakpoint in your code and step line by line examining each one as you go.
-
June 22nd, 2007, 09:37 AM
#10
Re: Problem copying linked list (memory error)
Ive tried that, but it's hard to do so because my program is running on a timer, and the breakpoint will also be tripped by each of the windows/graphs that are open. It seems there is a memory error when first opening a window/graph, and then it is okay. The ones that are set to open automatically (Config file) work and just throw the memory error, but if I try to open any more windows/graphs, it crashes completely.
-
June 22nd, 2007, 10:04 AM
#11
Re: Problem copying linked list (memory error)
Since you can't use your work environment to debug the problem, you need
to move your problem to a test environment.
Create a small program that uses the array but does so with a button press
instead of a timer. Your test program will need to be written so that it has
the same problem that you see in the work program.
Now you can step through the test program withoutinterference from the
repetitive timer ticks.
Zip the test program (no obj or other compiler files) and attach to this thread.
-
June 22nd, 2007, 10:11 AM
#12
Re: Problem copying linked list (memory error)
I'lll give it a try, but you won't be able to run it unless you have wxWidgets linked.
-
June 22nd, 2007, 10:11 AM
#13
Re: Problem copying linked list (memory error)
You set list to arrayList in your function
Code:
void ScrollingPlotFrame::sampleRate(GraphWin *child, int index, int rate)
{
...
list = &arrayList[index];
You don't describe how/where arrayList is created and destroyed. Is it
global? Are you sure it's still valid?
You have the error at the first time you access list. Something may be wrong
with how arrayList is managed.
-
June 22nd, 2007, 10:16 AM
#14
Re: Problem copying linked list (memory error)
So what if I can't compile and run it.
You will be able to compile it and run it - in the debugger.
Now you will be able to debug it properly.
-
June 22nd, 2007, 10:39 AM
#15
Re: Problem copying linked list (memory error)
Haha! I got it!!! Thanks everyone, turns out you were all right :P
cvogt61457, great idea -> really helped me to solve the problem.
StlDude, that i did
gcdef, you were right. The index was out of range.
What happened was that I have one function in the child that calls a parent function, and the parent function then calls the child function after to replot itself. I had added a flag and made an overloaded function call so that I don't end up in an infinite loop, but in another function which called the child function to refresh the screen I had forgotten to include the flag. Therefore, the child function went off and called the parent function before my linked list was set and it didn't have the value of the index yet.
Thanks all
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
|