Developer forums (C::B DEVELOPMENT STRICTLY!) > Development
issue on NullLoader class
blueshake:
hello ,devs:
the codes in Parser::Parse in parser file:
if(!opts.loader) //this should always be true (memory will leak if a loader has already been initialized before this point)
opts.loader=Manager::Get()->GetFileManager()->Load(bufferOrFilename, true);
if I change the function Load() args from false(before) to true(the black one above). then the cc can not parse the codes correctly.
I think it is something relatived to these codes:
--- Code: ---NullLoader *nl = new NullLoader(file, (char*) s.c_str(), s.length() * sizeof(wxChar));
--- End code ---
blueshake:
Seem not cause any attention.
I have a question here.
here are the class LoaderBase's implementation.
--- Code: ---// ***** class: LoaderBase *****
class LoaderBase : public AbstractJob
{
wxSemaphore sem;
bool wait;
void WaitReady()
{
if(wait)
{
wait = false;
sem.Wait();
}
};
protected:
wxString fileName;
char *data;
size_t len;
void Ready()
{
sem.Post();
};
public:
LoaderBase() : wait(true), data(0), len(0) {};
~LoaderBase();
void operator()() {};
wxString FileName() const { return fileName; };
bool Sync();
char* GetData();
size_t GetLength();
};
--- End code ---
here the member variable data's type is char* ,why not wxChar*
when we do this
--- Code: ---NullLoader *nl = new NullLoader(file, (char*) s.c_str(), s.length() * sizeof(wxChar));
--- End code ---
though the s.c_str() is convert to char*, it don't work here.
and in the body of bool Tokenizer::ReadFile() in tokenizer.cpp
these codes:
--- Code: ---if (m_pLoader)
{
fileName = m_pLoader->FileName();
char* data = m_pLoader->GetData();
m_BufferLen = m_pLoader->GetLength();
// the following code is faster than DetectEncodingAndConvert()
// DetectEncodingAndConvert(data, m_Buffer);
// same code as in cbC2U() but with the addition of the string length (3rd param in unicode version)
// and the fallback encoding conversion
#if wxUSE_UNICODE
m_Buffer = wxString(data, wxConvUTF8, m_BufferLen + 1); // + 1 => sentinel
if (m_Buffer.Length() == 0)
{
// could not read as utf-8 encoding, try iso8859-1
m_Buffer = wxString(data, wxConvISO8859_1, m_BufferLen + 1); // + 1 => sentinel
}
#else
m_Buffer = wxString(data, m_BufferLen + 1); // + 1 => sentinel
#endif
success = (data != 0);
}
--- End code ---
char* data = m_pLoader->GetData(); some error happened and make the cc cannot parse the codes correctly.
Note:
if want to reproduce the issue,just change the false to true in the funciton opts.loader=Manager::Get()->GetFileManager()->Load(bufferOrFilename, true);
blueshake:
And I find that if the codes contain non-Englis letters, it will be not recognized by the parser.
MortenMacFly:
--- Quote from: blueshake on December 02, 2009, 11:05:53 am ---here the member variable data's type is char* ,why not wxChar*
--- End quote ---
Not sure why, but if wxChar works I see no reason not to use it. Did you try? It should run out-of-the-box.
blueshake:
and these codes may cause some problem.
the variable data point to the args buffer directly.but what will happened if the buffer is destroyed?
--- Code: ---class NullLoader : public LoaderBase
{
public:
NullLoader(const wxString& name, char* buffer, size_t size) { fileName = name; data = buffer; len = size; Ready(); };
void operator()(){};
};
--- End code ---
and then check out these codes.
--- Code: --- if(reuseEditors)
{
EditorManager* em = Manager::Get()->GetEditorManager();
if(em)
{
wxFileName fileName(file);
for(int i = 0; i < em->GetEditorsCount(); ++i)
{
cbEditor* ed = em->GetBuiltinEditor(em->GetEditor(i));
if(ed && fileName == ed->GetFilename())
{
wxString s(ed->GetControl()->GetText());
#if wxCHECK_VERSION(2, 9, 0)
NullLoader *nl = new NullLoader(file, (char*) s.wx_str(), s.length() * sizeof(wxChar));
#else
NullLoader *nl = new NullLoader(file, (char*) s.c_str(), s.length() * sizeof(wxChar));
#endif
return nl;
}
}
}
}
--- End code ---
the wxString type varialbe s will be destroyed after s leave the if scope.and at this time,the data in class NullLoader will point to something which had been destroyed.
Navigation
[0] Message Index
[#] Next page
Go to full version