Why wxXmlNode GetNext() occur error when I use wxFile Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
cnzui
Earned a small fee
Earned a small fee
Posts: 24
Joined: Thu Nov 03, 2011 9:17 am

Why wxXmlNode GetNext() occur error when I use wxFile

Post by cnzui »

Well, I have a xml document like this:

Code: Select all

<files>
<file src="1.jpg" />
<file src="2.jpg" />
</files>
This is my codes:

Code: Select all

wxStringInputStream sis(msg);
wxXmlDocument doc(sis);
wxXmlNode *root=doc.GetRoot();

wxXmlNode *group = root->GetChildren();
while(group)
{
    wxXmlNode *child = group->GetChildren();
    while(child)
    {
        char fbuf[1024];
        //wxFile code start
        wxString imgsrc=friends->GetAttribute(wxT("src"),wxT(""));			
        wxFile fl;
        fl.Open(imgsrc);			
        fl.Read(fbuf,fl.Length());
        fl.Close();
        //wxFile code end	

        child=child->GetNext();
    }
    group=group->GetNext();
}
When it runs, it will occur error like this:
0xC0000005: Access Violation.
However,if I comment the wxFile codes, it can runs correctly, can somebody tell me how to resolve this problem?
Welcome to my blog:http://www.cnzui.com
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Why wxXmlNode GetNext() occur error when I use wxFile

Post by evstevemd »

You assume th e file is loaded correctly which might not be the case change code to somethining like

Code: Select all

wxXmlDocument doc;
if(!doc.Load(sis)){
wxMessageBox(_("Something went wrong with Loading File"));
return ;
}
// start processing the XML file
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
cnzui
Earned a small fee
Earned a small fee
Posts: 24
Joined: Thu Nov 03, 2011 9:17 am

Re: Why wxXmlNode GetNext() occur error when I use wxFile

Post by cnzui »

evstevemd wrote:You assume th e file is loaded correctly which might not be the case change code to somethining like

Code: Select all

wxXmlDocument doc;
if(!doc.Load(sis)){
wxMessageBox(_("Something went wrong with Loading File"));
return ;
}
// start processing the XML file

I have tried it, but it's not this reason~

The error is found in the "while" loop.
Welcome to my blog:http://www.cnzui.com
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Why wxXmlNode GetNext() occur error when I use wxFile

Post by evstevemd »

cnzui wrote:
evstevemd wrote:You assume th e file is loaded correctly which might not be the case change code to somethining like

Code: Select all

wxXmlDocument doc;
if(!doc.Load(sis)){
wxMessageBox(_("Something went wrong with Loading File"));
return ;
}
// start processing the XML file

I have tried it, but it's not this reason~

The error is found in the "while" loop.
Use debugger and track changes in variables especially pointers. Also posting traceback will help!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
cnzui
Earned a small fee
Earned a small fee
Posts: 24
Joined: Thu Nov 03, 2011 9:17 am

Re: Why wxXmlNode GetNext() occur error when I use wxFile

Post by cnzui »

I debug it and found that after the wxFile code,the object "doc" is no use at all! Why ?
Welcome to my blog:http://www.cnzui.com
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Why wxXmlNode GetNext() occur error when I use wxFile

Post by evstevemd »

cnzui wrote:I debug it and found that after the wxFile code,the object "doc" is no use at all! Why ?
What do you mean by doc is not used at all? It is already used here: wxXmlNode *root=doc.GetRoot();
BTW what is the value of root in debugger!

Code: Select all

wxXmlNode *root=doc.GetRoot();
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
cnzui
Earned a small fee
Earned a small fee
Posts: 24
Joined: Thu Nov 03, 2011 9:17 am

Re: Why wxXmlNode GetNext() occur error when I use wxFile

Post by cnzui »

evstevemd wrote:
cnzui wrote:I debug it and found that after the wxFile code,the object "doc" is no use at all! Why ?
What do you mean by doc is not used at all? It is already used here: wxXmlNode *root=doc.GetRoot();
BTW what is the value of root in debugger!

Code: Select all

wxXmlNode *root=doc.GetRoot();
I mean it seems the doc is overwrited after the wxfile operation.
I'm mad for this problem :oops:
Welcome to my blog:http://www.cnzui.com
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Why wxXmlNode GetNext() occur error when I use wxFile

Post by PB »

I've just skimmed through your code in the first post, so I might have overlooked something, but still: The line

Code: Select all

fl.Read(fbuf,fl.Length());
looks very suspicious to me. Can you 100% guarantee that size of the fl file will never be greater than 1024 bytes you allocate for fbuf? If not, then memory corruption is bound to happen. Either read the data in chunks or better yet, it's often possible to avoid using a fixed size buffer at all.

Of course, there still can be another cause of your bug, but that line should be still reviewed. For now at least try to add

Code: Select all

wxASSERT(sizeof(fbuf) >= fl.Length());
before the line quoted above and run your app under the debugger with conditions that make itapp crash. You should see if the memory past fbuf gets overwritten or not in that specific case.
cnzui
Earned a small fee
Earned a small fee
Posts: 24
Joined: Thu Nov 03, 2011 9:17 am

Re: Why wxXmlNode GetNext() occur error when I use wxFile

Post by cnzui »

PB wrote:I've just skimmed through your code in the first post, so I might have overlooked something, but still: The line

Code: Select all

fl.Read(fbuf,fl.Length());
looks very suspicious to me. Can you 100% guarantee that size of the fl file will never be greater than 1024 bytes you allocate for fbuf? If not, then memory corruption is bound to happen. Either read the data in chunks or better yet, it's often possible to avoid using a fixed size buffer at all.

Of course, there still can be another cause of your bug, but that line should be still reviewed. For now at least try to add

Code: Select all

wxASSERT(sizeof(fbuf) >= fl.Length());
before the line quoted above and run your app under the debugger with conditions that make itapp crash. You should see if the memory past fbuf gets overwritten or not in that specific case.

I'm afraid you are right, very thanks!
Welcome to my blog:http://www.cnzui.com