Problem with wxSpinCtrl->GetValue() and STL 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
Bundy
I live to help wx-kind
I live to help wx-kind
Posts: 153
Joined: Fri Apr 29, 2005 9:46 am
Location: Poland

Problem with wxSpinCtrl->GetValue() and STL

Post by Bundy » Wed May 10, 2006 6:27 am

I have simple class to read line from file. It use only STL:

Code: Select all

#include <fstream>

class ReadLine
{
    
public:

    ReadLine(char* path)
    {
        file = new std::ifstream(path);       
    }
    
    char* getLine(int number)
    {
	skipLine(number-1);
	char * linia;
        file->getline(linia,50); 
        return linia;  
    }  
    
    void skipLine(int number)
    {
        while(number>0)
        {
            while( file->get()!='\n' );
            --number;  
        }
    }  
    
    char getNextChar()
    {
        return file->get();   
    }

private:    
    std::ifstream *file;
};

Now in simple wxWidgets application I have memo, button and spinctrl. Idea is to read specific line when user click button and select line number in spinctrl. This is code of function wich react on button event:

Code: Select all

        wxString path = _T("test2.txt");
        
        char * cos;
        cos  = (char*)wxString(path).c_str();
        ReadLine rl = ReadLine( cos  );
        
        wxString zaw;
        int number = WxSpinCtrl1->GetValue();
        zaw = rl.getLine( number );

        WxMemo1->SetValue(zaw)

When I compile and click button program crashes, but when I add one line:

Code: Select all

        wxString path = _T("test2.txt");
        
        char * cos;
        cos  = (char*)wxString(path).c_str();
        ReadLine rl = ReadLine( cos  );
        
        wxString zaw;
        int number = WxSpinCtrl1->GetValue();
        zaw = wxString()<<number; // <--- this is this added line
	zaw = rl.getLine( number );

        WxMemo1->SetValue(zaw);

everything works perfect. Ofcourse when I put number instead read him from spinctrl it works too. So, have You som idea whats is wrong?

Regards
Bundy
"Fate rewards prepared mind"

Quote from movie "Liberator 2"

micros
Super wx Problem Solver
Super wx Problem Solver
Posts: 317
Joined: Sat Mar 18, 2006 10:41 am
Location: Ustek, Bohemia

Re: Problem with wxSpinCtrl->GetValue() and STL

Post by micros » Wed May 10, 2006 7:01 am

Bundy wrote:

Code: Select all

    char* getLine(int number)
    {
	skipLine(number-1);
	char * linia;
        file->getline(linia,50); 
        return linia;  
    }  
In this function, linia is an uninitialized pointer. You haven't allocated memory for it. You also have to append a terminating NUL character. Here's the quickest fix:

Code: Select all

	skipLine(number-1);
	char * linia = new char[51];
        file->getline(linia,50);
        linia[file->gcount()] = '\0';
        return linia;  
But the caller will have to delete[] the returned pointer! A much safer and more comfortable way is to use the std::getline() function. It fills a std::string you supply:

Code: Select all

    void getLine(int number, std::string& buffer)
    {
	skipLine(number-1);
        std::getline(*file, buffer);
    }  
By the way, you should either write a destructor that will delete the ifstream, or have it as a member (I prefer the latter):

Code: Select all

    ReadLine(const char* path)
      : m_file(path)
    {
    }

  private:
    std::ifstream m_file;
I have some more points. Converting a const type to non-const is always suspicious, and in your case unnecessary:

Code: Select all

        char * cos;
        cos  = (char*)wxString(path).c_str();
        // creating a copy of the path string is unnecessary, and cos has wrong type
        // this is enough (though won't compile in unicode build):
        const char* cos = path.c_str();
Finally, you could take a look at wxTextFile, I think it offers the functionality you want. Although it consumes more memory since it loads the whole file, it's much more efficient. (your snippet leaks memory, which is worse than consume :))

Bundy
I live to help wx-kind
I live to help wx-kind
Posts: 153
Joined: Fri Apr 29, 2005 9:46 am
Location: Poland

Post by Bundy » Wed May 10, 2006 7:19 am

Thanks, About uninitializet pointer i know already. Compiler return me error when i buid simple console application. I'm wondering why compiler didn't say me about uninitializet pointer when I build simple application with wxWidgets. Anyway thanks for this and thanks for others tips. I will use them :)

Regards
Bundy
"Fate rewards prepared mind"

Quote from movie "Liberator 2"

Post Reply