wxArrayString and simple crash. Need some help! 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.
Post Reply
cavernah
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Jan 19, 2009 3:32 am
Location: Brazil

wxArrayString and simple crash. Need some help!

Post by cavernah » Wed Feb 04, 2009 9:50 pm

Hi! Im a newbie in C++ and wxWidgets. I got a crash (memory leak?) in a simple code with wxArrayString. I want compare two wxArrayString and save the result(get same numbers) in an another wxArrayString and then save this last wxArrayString in a file.
PS.: I use wxArrayString with numbers here because the numbers are between letters and I organize then from it in the original code. So, all the string is important, but I want only to keep the strings with the same numbers in the 2 wxArrayString in a third wxArrayString.

Here the exemple code with numbers (only):
-The real problem is in the last line!

Code: Select all

    wxString buffer;
    wxString buffer2;
    wxString buffer3;
	wxFFile file(wxT("arraytemp.txt"),"w");
	wxFFile file2(wxT("arraytemp2.txt"),"w");
	wxFFile file3(wxT("buffer.txt"),"w");
	wxFFile file4(wxT("arraytemp3.txt"),"w");
    wxArrayString arraytemp;
    wxArrayString arraytemp2;
    wxArrayString arraytemp3;

//fill arraytemp with 0,1,1,1,2
    int x = 0;
    buffer.Printf("%d",x);
    arraytemp.Add(buffer);
    file.Write(arraytemp.Item(0)+"\n");
    x = 1;
    buffer.Printf("%d",x);
    arraytemp.Add(buffer);
    file.Write(arraytemp.Item(1)+"\n");
    x = 1;
    buffer.Printf("%d",x);
    arraytemp.Add(buffer);
    file.Write(arraytemp.Item(2)+"\n");
    x = 1;
    buffer.Printf("%d",x);
    arraytemp.Add(buffer);
    file.Write(arraytemp.Item(3)+"\n");
    x = 2;
    buffer.Printf("%d",x);
    arraytemp.Add(buffer);
    file.Write(arraytemp.Item(4)+"\n");

//fill arraytemp2 with 1,1,1,2,3
    x = 1;
    buffer.Printf("%d",x);
    arraytemp2.Add(buffer);
    file2.Write(arraytemp2.Item(0)+"\n");
    x = 1;
    buffer.Printf("%d",x);
    arraytemp2.Add(buffer);
    file2.Write(arraytemp2.Item(1)+"\n");
    x = 1;
    buffer.Printf("%d",x);
    arraytemp2.Add(buffer);
    file2.Write(arraytemp2.Item(2)+"\n");
    x = 2;
    buffer.Printf("%d",x);
    arraytemp2.Add(buffer);
    file2.Write(arraytemp2.Item(3)+"\n");
    x = 3;
    buffer.Printf("%d",x);
    arraytemp2.Add(buffer);
    file2.Write(arraytemp2.Item(4)+"\n");

//compare
    int a;
    int b;
    for (int i = 0; i<5; i++)
    {
        a = wxAtoi(arraytemp[i]);
        b = wxAtoi(arraytemp2[i]);
        if (a==b)
        {
            buffer3.Printf("%d",a);
            file3.Write(buffer3+"\n");  //save "buffer.txt"
            arraytemp3.Add(buffer3);  //fill arraytemp3
            file4.Write(arraytemp3.Item(i)+"\n");   //save "arraytemp3.txt"  What's the problem here in saving arraytemp3???
        }
    }
Thanks for all future help!
Last edited by cavernah on Wed Feb 04, 2009 10:36 pm, edited 3 times in total.

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Wed Feb 04, 2009 10:19 pm

Hi,

How do you determine there is a memory leak?

User avatar
Disch
Experienced Solver
Experienced Solver
Posts: 99
Joined: Wed Oct 17, 2007 2:01 am

Re: wxArrayString and simple memory leak. Need some help!

Post by Disch » Wed Feb 04, 2009 10:26 pm

What's the problem here in saving arraytemp3???
arraytemp3 is of type wxArrayString, not of type wxString. If you look at wxFFile::Write, there isn't a version of the function which takes wxArrayString, only one that takes wxString (and another which takes const void* -- but that's not what you want either).

If you want to write the whole array of strings to the file, you'll need to step through the array in a loop and write each string individually.

You're also potentially stepping out of bounds, here:

Code: Select all

            arraytemp3.Add(buffer3);  //fill arraytemp3
            file4.Write(arraytemp3.Item(i)+"\n");   //save "arraytemp3.txt"  What's the problem here in saving arraytemp3???
        }
    }
You're using 'i' to index arraytemp3 -- but arraytemp3 doesn't necessarily have 'i' items in it! For example say your first match (and thus the first time anything is added to arraytemp3) is when i=2. This would mean arraytemp3 only has 1 item in it (index=0), and you're trying to get the 3rd item in it (index=2) which is nonexistant!

If you're just trying to dump the matching string to the file, write 'buffer3' to the file instead. Or step through the arraytemp3 array later, outside that loop, and don't exceed the array's boundary when you index it (use arraytmp3.GetCount() to know how many items are in the array).


EDIT:

Accesing the array past its bounds is almost certainly the source of your crash. And no, it's not a memory leak. Memory leaks don't crash, they just eat RAM.

cavernah
In need of some credit
In need of some credit
Posts: 6
Joined: Mon Jan 19, 2009 3:32 am
Location: Brazil

Re: wxArrayString and simple memory leak. Need some help!

Post by cavernah » Mon Feb 09, 2009 11:46 pm

Disch wrote:
What's the problem here in saving arraytemp3???
arraytemp3 is of type wxArrayString, not of type wxString. If you look at wxFFile::Write, there isn't a version of the function which takes wxArrayString, only one that takes wxString (and another which takes const void* -- but that's not what you want either).

If you want to write the whole array of strings to the file, you'll need to step through the array in a loop and write each string individually.

You're also potentially stepping out of bounds, here:

Code: Select all

            arraytemp3.Add(buffer3);  //fill arraytemp3
            file4.Write(arraytemp3.Item(i)+"\n");   //save "arraytemp3.txt"  What's the problem here in saving arraytemp3???
        }
    }
You're using 'i' to index arraytemp3 -- but arraytemp3 doesn't necessarily have 'i' items in it! For example say your first match (and thus the first time anything is added to arraytemp3) is when i=2. This would mean arraytemp3 only has 1 item in it (index=0), and you're trying to get the 3rd item in it (index=2) which is nonexistant!

If you're just trying to dump the matching string to the file, write 'buffer3' to the file instead. Or step through the arraytemp3 array later, outside that loop, and don't exceed the array's boundary when you index it (use arraytmp3.GetCount() to know how many items are in the array).


EDIT:

Accesing the array past its bounds is almost certainly the source of your crash. And no, it's not a memory leak. Memory leaks don't crash, they just eat RAM.
Sorry! It's my "memory leak"... :oops: Thanks for the answer!

Post Reply