B> I've come up with the following code:
B>
B> #include "MyClass.h"
B> #include "MyApp.h"
B>
B> IMPLEMENT_APP_NO_MAIN(MyApp)
B>
B> int MyClass::ShowImage(wxBitmap* BMP)
B> {
B> wxMyApp* app = new MyApp();
B> wxApp::SetInstance( app );
B>
B> wxGetApp().SetBitmap(BMP); //workaround to load the image
B> pointer into the app
You can't create wxBitmaps before initializing the GUI so I don't know
where do you take wxBitmaps pointer from before wxEntry is called.
B> int argc=1;
B> char** argv=(char**)malloc(sizeof(char*));
B> argv[0]=(char*)malloc(sizeof(char)*strlen("Test"));
B> strcpy(argv[0],"Test");
FWIW you don't need to allocate argv dynamically and if you do, you need
to free it as wx won't do it. And while wx itself doesn't assume that argv
is NULL-terminated, functions called by it (e.g gtk_init()) might so I'd do
this instead
char *argv[] = { "test", NULL };
wxEntry(1, argv);
B> The funny thing is that the above code compiles and runs perfectly when my
B> project is compiled under "release" options of Codeblocks,
Try running it under valgrind (assuming you're under Linux) and you will
see that it doesn't run perfectly.
B> but under the "debug" one it throws a SIGSEGV when it execute the
B> "wxEntry" function.
Have you tried looking at the stack trace under debugger when it crashed?
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
B> int MyClass::ShowImage(RgbMap* Map)
B> {
B> //LRgbMap is our class that stores an RGB image
B> wxMyApp* app = new MyApp();
B> wxApp::SetInstance( app );
B>
B> wxGetApp().SetRgbMap(Map);
B>
B> char *argv[] = { "test", NULL };
B> int argc=1;
B>
B> return wxEntry(argc, argv);
B> }
B>
B> Then, inside MyApp.cpp, I added the following code for OnInit():
B>
B> bool MyApp::OnInit()
B> {
B> //(*AppInitialize
B> bool wxsOK = true;
B> //wxInitAllImageHandlers(); //Is this important? With it,
B> it wouldn't compile under Windows and in Debian
wxInitAllImageHandlers() is needed if you use any bitmap handlers for
loading BMP, PNG, JPEG, TIFF, ... data into wxBitmap. If you don't, then
you don't need it. In any case, it will always compile fine without this
call, it just wouldn't find the handler and so might fail to load the
bitmap when you try to do it.
B> it worked fine without it
B> if ( wxsOK )
B> {
B> this->BMP=new wxBitmap();
B> ConvertRGBMapToWxBitmap(this->RgbMap,this->BMP);
B> wxShowImageFrame *Frame = new wxShowImageFrame(this->BMP);
B> Frame->Show();
B> SetTopWindow(Frame);
B> }
B> //*)
B> return wxsOK;
B> }
B>
B> My test program is this:
B>
B> int main()
B> {
B> Map=new RgbMap;
B> Mapa->ReadBMP("image.bmp");
B> MyClass::ShowRgbMap(Map);
B>
B> Map2=new RgbMap();
B> Map->ReadBMP("image2.bmp");
B> MyClass::ShowRgbMap(Map2);
B>
B> return 0;
B> }
Except for the obvious memory leaks of RgbMap objects this looks fine.
So maybe the problem is that something is not de-/re-initialized properly.
To prevent this from happening you could always create wxTheApp only once
and call wxEntryStart() (and wxEntryCleanup()) once too instead of calling
wxEntry() multiple times. Although doing the latter should work too, of
course...
B> time it was called, but it threw another SIGSEGV when I called the
B> function a second time.
Do you have the output of "bt" from gdb for Linux?
B> Under Windows it showed this:
B>
B> #0 00000000 ??() (??:??)
B> #1 004894A6 wxWindow::MSWWindowProc(this=0x2a34938, message=28,
B> wParam=1, lParam=1468)
This is strange, I don't see where exactly does it crash. We can see
that this happens while processing WM_ACTIVATEAPP but there doesn't seem to
be any pointer dereferences in its handler. Maybe you could try to debug it
a bit by putting a breakpoint on "case WM_ACTIVATEAPP" in
src/msw/window.cpp and checking what happens there?
Regards,
But, to each his own...
[ btw, sorry that the body of the response is not included... It's
terribly inconvenient to extract them in my email client when they are sent
as attachments instead of in the main message body... You understand, it's
a lot like inconsiderate folks who won't wrap their email 'properly' to fit
other group members' email clients... Some people can be so
inconsiderate!... ]
jmmomp:
Dave S.
Development with wxWidgets on MSWindows
http://tech.groups.yahoo.com/group/wxMS_developers/
wxWidgets Code Exchange
http://www.wxcodex.net/
----- Original Message -----
From: "Vadim Zeitlin" <va...@wxwidgets.org>
To: <wx-u...@googlegroups.com>
Sent: Thursday, July 16, 2009 7:21 AM
Subject: Re[2]: Starting wxWidgets from main() function
B> time it was called, but it threw another SIGSEGV when I called the
B> function a second time.
Do you have the output of "bt" from gdb for Linux?
This is strange, I don't see where exactly does it crash. We can see
that this happens while processing WM_ACTIVATEAPP but there doesn't seem to
be any pointer dereferences in its handler. Maybe you could try to debug it
a bit by putting a breakpoint on "case WM_ACTIVATEAPP" in
src/msw/window.cpp and checking what happens there?
B> > B> time it was called, but it threw another SIGSEGV when I called the
B> > B> function a second time.
B> >
B> > Do you have the output of "bt" from gdb for Linux?
B>
B> I didn't understand what you meant by "bt",
The output of gdb "bt" command [at the moment of the crash].
B> so I took the output at the moment of the second crash under Linux.
B> Also, the problem is happening when the function wxTheApp->OnRun() is
B> called in the program, as I will put later:
B>
B> #0 0x7ff01d843de4 _L_unlock_163() (/lib/libpthread.so.0:??)
B> #1 0x7ff01d843de9 _L_unlock_163() (/lib/libpthread.so.0:??)
B> #2 0x7ff01d843b26 __pthread_mutex_unlock_usercnt()
B> (/lib/libpthread.so.0:??)
B> #3 0x7ff01e2e2099 wxMutexInternal::Unlock()
B> (/usr/lib/libwx_baseu-2.8.so.0:??)
B> #4 0x7ff01eb5ee9a ??() (/usr/lib/libwx_gtk2u_core-2.8.so.0:??)
B> #5 0x7ff01eb5eeb4 ??() (/usr/lib/libwx_gtk2u_core-2.8.so.0:??)
You should be using debug version of wx (libwx_gtk2ud-core.so &c) in order
to make anything out of this -- like this it really doesn't have any useful
information.
The output of gdb "bt" command [at the moment of the crash].
B> >
B> > The output of gdb "bt" command [at the moment of the crash].
B>
B> #0 0x7fd4237ac290 __lll_unlock_wake() (/lib/libpthread.so.0:??)
B> #1 0x7fd4237a8de9 _L_unlock_163() (/lib/libpthread.so.0:??)
B> #2 0x7fd4237a8b26 __pthread_mutex_unlock_usercnt()
B> (/lib/libpthread.so.0:??)
B> #3 0x7fd42425f076 wxMutexInternal::Unlock(this=0x6eff50)
B> (../src/unix/threadpsx.cpp:297)
B> #4 0x7fd424b0011a wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:256)
B> #5 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #6 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #7 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #8 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #9 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #10 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #11 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #12 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #13 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #14 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #15 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #16 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #17 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #18 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #19 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #20 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #21 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #22 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #23 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #24 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #25 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #26 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #27 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #28 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B> #29 0x7fd424b00134 wxapp_poll_func(ufds=0x7735d0, nfds=2, timeout=0)
B> (../src/gtk/app.cpp:259)
B>
B> Seems the debug configuration missed the "--debug" on "wx-config". Will it
B> help somehow now?
Well, sorry, no. This is clearly not a valid stack trace. If you mean that
you didn't use debug version of wx, it would explain it. Please build
everything in debug when trying to gather information about a problem.
Alternatively please make a _small_ self-contained example showing your
problem and then maybe I could debug it here.
Well, sorry, no. This is clearly not a valid stack trace. If you mean that
you didn't use debug version of wx, it would explain it. Please build
everything in debug when trying to gather information about a problem.
Alternatively please make a _small_ self-contained example showing your
problem and then maybe I could debug it here.
B> > Well, sorry, no. This is clearly not a valid stack trace. If you mean
B> > that
B> > you didn't use debug version of wx, it would explain it. Please build
B> > everything in debug when trying to gather information about a problem.
B>
B> I compiled using `wx-config --debug --cflags` and `wx-config --debug
B> --libs`, and that was the call stack that was given to me, even when I ran
B> it manually, without the help of Codeblocks
Strange, maybe something corrupting the stack then... have you tried
running it under valgrind?
B> Alternatively please make a small self-contained example showing your
B> > problem and then maybe I could debug it here.
B>
B> I'm sending a small program with the makefile. The problem is with
B> subsequent calls of ShowImage(2), as you'll see in main.cpp
I'll try to have a look at it.
B> I'm sending a small program with the makefile. The problem is with
B> subsequent calls of ShowImage(2), as you'll see in main.cpp
I built your test under Linux and it works just fine for me. I even ran it
under valgrind to be sure I don't miss anything and here is what it says:
% valgrind ./Test
==6920== Memcheck, a memory error detector.
==6920== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==6920== Using LibVEX rev 1854, a library for dynamic binary translation.
==6920== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==6920== Using valgrind-3.3.1-Debian, a dynamic binary instrumentation framework.
==6920== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==6920== For more details, rerun with: -v
==6920==
==6920==
==6920== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 88 from 2)
==6920== malloc/free: in use at exit: 419,871 bytes in 4,736 blocks.
==6920== malloc/free: 28,805 allocs, 24,069 frees, 2,348,266 bytes allocated.
==6920== For counts of detected errors, rerun with: -v
==6920== searching for pointers to 4,736 not-freed blocks.
==6920== checked 4,375,576 bytes.
==6920==
==6920== LEAK SUMMARY:
==6920== definitely lost: 580 bytes in 13 blocks.
==6920== possibly lost: 48,352 bytes in 42 blocks.
==6920== still reachable: 370,939 bytes in 4,681 blocks.
==6920== suppressed: 0 bytes in 0 blocks.
==6920== Rerun with --leak-check=full to see details of leaked memory.
I.e. everything looks fine. I really don't know what's going on in your
case, sorry.
B> Alternatively, I can change the question to: what steps I need to take to
B> start a wxApp in the main function, as many times as I want, without the
B> IMPLEMENT_APP macro?
As I said before, your code looks correctly to me...
B> Maybe it is because of machine configuration? I'm actually running Debian in
B> a remote machine with vncserver, logging in with putty in Windows.
I don't think this can make any difference.
B> The output of `wx-config --debug --cflags` was:
B>
B> -I/usr/lib/wx/include/gtk2-unicode-debug-2.8 -I/usr/include/wx-2.8
B> -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -D__WXDEBUG__ -D__WXGTK__ -pthread
This looks correct. Could you try 2.9.0 (RC6) just to see if you have
better luck with it?