Assertion with wxTextFile - but only when inside my project 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
celstark
Knows some wx things
Knows some wx things
Posts: 43
Joined: Fri Aug 26, 2005 7:37 pm

Assertion with wxTextFile - but only when inside my project

Post by celstark »

Pulling my hair out on this one. Consider the following code:

bool MyApp::OnInit(void)
{

Code: Select all

wxTextFile *foo1 = new wxTextFile(_T("foo1.txt"));
if (foo1->Exists()) foo1->Open();
else foo1->Create();

foo1->AddLine(_T("arg"));
foo1->Write();
foo1->Close();

delete foo1;
I've used wxTextFile a good bit and code like this has worked in the past (this or using a wxTextFile rather than a pointer). In fact, if I place this text into something like minimal.cpp, it still works. Yet if it's here in my app - even before my app does much (right here in OnInit - first discovered elsewhere of course), I get a debug assertion on the "delete". It's happening in in wxFile::Close()
if ( IsOpened() ) ...

(Here, m_fd = -3289651 and m_error = true) and the reported error from MSVC is:
Expression: (fh >- 0 && (unsigned)fh < (unsigned)_nhandle)

Now, since this works in minimal.cpp and since I've used this code for ages and since I've tried this in 2.8.11, 2.9.0, and 2.9.1, I don't think it's a wxWidgets bug per se. But what could cause something like this to happen in the init of my project (code's been running and developing for years) and not in something else?

Any ideas where to look? Stepping through the code it seems to be doing reasonable things on the delete there (and, of course, if I don't do this as a pointer when the function closes and deletes I get the same error).

Craig
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

My guess would be that the file is not successfully opened. Do a check with wxTextFile::IsOpened().

If this is false, tracing through the Open or Create call (whichever is used) should reveal why it happened, probably missing access rights, or file already in use.

Code: Select all

wxTextFile *foo1 = new wxTextFile(_T("foo1.txt"));
if (foo1->Exists()) foo1->Open();
else foo1->Create();

if(fool->IsOpened()) {
  foo1->AddLine(_T("arg"));
  foo1->Write();
  foo1->Close();
}

delete foo1;
Use the source, Luke!
celstark
Knows some wx things
Knows some wx things
Posts: 43
Joined: Fri Aug 26, 2005 7:37 pm

Post by celstark »

Thanks - I'd checked that and that wasn't the issue. Reads, writes, etc. were actually going through.

After going back to an earlier version and adding in about a month worth of patches, I finally tracked down the issue. A library I was using had a header file that specified:

#pragma pack(1)

Why they need 1-byte packing, I don't know. But, of course anything that came after this was done with said 1-byte packing or called with the expectation of 1-byte packing of all structs, class members, etc.

So, wxWidgets calls were often being directed to the wrong address. Which one was affected would depend on things like compile order and other crud to make it just that more nice and random for me to track down.

The offending file was several levels of include down and in an unrelated bit of code, making the issue that much tougher to find. But, it's found, it's fixed, and my code is back to being happy.

Craig
Post Reply