Gdk::Pixmap is giving me a segmentation fault on create() - c++

I am doing pixel drawing onto a Gtk::DrawingArea. Sometimes an expose event occurs and I need to redraw everything. But drawing every pixel again is slow, so I'm trying to create a Sdk::Pixmap and draw to it, then draw it to the screen when needed.
I'm trying to create a Sdk::Pixmap and draw to it with draw_point(). Here is my code:
In the hpp:
Glib::RefPtr<Gdk::Pixmap>pixmap;
Glib::RefPtr<Gdk::GC>pixGC;
In the cpp:
Glib::RefPtr<Gdk::Drawable> d = Gdk::Drawable::create(); // this has a value of 1
pixmap = Gdk::Pixmap::create(d, width, height, 3); // this call is giving me a seg fault
pixGC = Gdk::GC::create();
And when drawing:
pixmap->draw_point(pixGC,x, y); // don't even know if this works yet because of the seg fault
What's wrong with my code? :(
Also, I haven't written the code to draw to the actual DrawingArea yet.
Edit: Ok I fixed the first seg fault with:
pixmap = Gdk::Pixmap::create(get_window(), width, height, 3);
pixGC = Gdk::GC::create(get_window());
Thanks to this link:
http://marc.info/?l=gtkmm&m=108547746915009
But now I get this:
The program 'programName' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadDrawable (invalid Pixmap or Window parameter)'.
(Details: serial 69525 error_code 9 request_code 64 minor_code 0)
(Note to programmers: normally, X errors are reported asynchronously;
that is, you will receive the error a while after causing it.
To debug your program, run it with the --sync command line
option to change this behavior. You can then get a meaningful
backtrace from your debugger if you break on the gdk_x_error() function.)
Stack trace:
Breakpoint 3, 0x00007ffff72830e0 in _XError ()
from /usr/lib/x86_64-linux-gnu/libX11.so.6
(gdb) bt
#0 0x00007ffff72830e0 in _XError () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#1 0x00007ffff72801d1 in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#2 0x00007ffff7280215 in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#3 0x00007ffff7281050 in _XReply () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#4 0x00007ffff727c99d in XSync () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#5 0x00007ffff727ca2b in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#6 0x00007ffff725f954 in XCreatePixmap ()
from /usr/lib/x86_64-linux-gnu/libX11.so.6
#7 0x00007ffff3e2f48d in ?? ()
from /usr/lib/x86_64-linux-gnu/libgdk-x11-2.0.so.0
#8 0x00007ffff6de048d in Gdk::Pixmap::Pixmap (this=0x7fffd4000930,
drawable=..., width=256, height=256, depth=3, __in_chrg=<optimized out>,
__vtt_parm=<optimized out>) at pixmap.cc:55
#9 0x00007ffff6de0d71 in Gdk::Pixmap::create (drawable=..., width=256,
height=256, depth=<optimized out>) at pixmap.cc:317
#10 0x00000000004408e1 in Viewer::start (this=0x7fffffffea00, width=256,
height=256) at viewer.cpp:23
#11 0x000000000042e380 in AppWindow::rayTraceDrawer (this=0x7fffffffe8e0)
at appwindow.cpp:25
#12 0x0000000000432cd8 in std::_Mem_fn<void (AppWindow::*)()>::operator() (
this=0x767b58, __object=0x7fffffffe8e0)
at /usr/include/c++/4.6/functional:551
#13 0x0000000000432b8b in std::_Bind_result<void, std::_Mem_fn<void (AppWindow::---Type <return> to continue, or q <return> to quit---
*)()> (AppWindow*)>::__call<void, , 0>(std::tuple<>&&, std::_Index_tuple<0>, std::_Bind_result<void, std::_Mem_fn<void (AppWindow::*)()> (AppWindow*)>::__enable_if_void<void>::type) (this=0x767b58, __args=...)
at /usr/include/c++/4.6/functional:1287
#14 0x0000000000432ae3 in std::_Bind_result<void, std::_Mem_fn<void (AppWindow::*)()> (AppWindow*)>::operator()<>() (this=0x767b58)
at /usr/include/c++/4.6/functional:1378
#15 0x0000000000432972 in std::thread::_Impl<std::_Bind_result<void, std::_Mem_fn<void (AppWindow::*)()> (AppWindow*)> >::_M_run() (this=0x767b40)
at /usr/include/c++/4.6/thread:117
#16 0x00007ffff6031c78 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#17 0x00007ffff589de9a in start_thread ()
from /lib/x86_64-linux-gnu/libpthread.so.0
#18 0x00007ffff55ca3fd in clone () from /lib/x86_64-linux-gnu/libc.so.6
#19 0x0000000000000000 in ?? ()
Is this because I'm creating the pixmap on a background thread?
Edit 2:
Nope, moved to UI thread and still crashing...
Edit 3:
Looks like I may have fixed it. The problem was accessing get_window() too early. Moved code to on_realize and it stopped crashing. Not sure if pixmap is valid yet though...will post after I figure out how to draw from pixmap to drawingarea.

And the solution after so many hours is:
In on_realize of your DrawingArea:
pixmap = Gdk::Pixmap::create(get_window(), width, height, get_window()->get_depth());
pixGC = Gdk::GC::create(get_window());
Draw to pixmap:
pixGC->set_foreground(color);
pixmap->draw_point(pixGC,x,y);
Then to draw to DrawingArea:
get_window()->draw_drawable(get_style()->get_fg_gc(get_state()),
pixmap,
0,0,0,0);

Related

Running custom protoc-plugin on linux causes

I am building a custom protoc-compiler that is based on googles c++ libraries for protobuf.
I ran into a strange error when running it on linux, while it runs fine on MacOS
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
After setting up and trying around with my debugger this is the stacktrace:
#1 0x00007f61c097b897 in abort () from /usr/lib/libc.so.6
#2 0x00007f61c0d1381d in __gnu_cxx::__verbose_terminate_handler () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/vterminate.cc:95
#3 0x00007f61c0d204da in __cxxabiv1::__terminate (handler=<optimized out>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:47
#4 0x00007f61c0d20537 in std::terminate () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:57
#5 0x00007f61c0d2078e in __cxxabiv1::__cxa_throw (obj=obj#entry=0x5568aec89df0, tinfo=tinfo#entry=0x7f61c0e5a750 <typeinfo for std::system_error>, dest=dest#entry=0x7f61c0d4cc60 <std::system_error::~system_error()>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_throw.cc:95
#6 0x00007f61c0d167ff in std::__throw_system_error (__i=-1) at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/ext/new_allocator.h:89
#7 0x00007f61c14b5c63 in std::call_once<void (&)(google::protobuf::internal::DescriptorTable const*), google::protobuf::internal::DescriptorTable const*&> (__f=#0x7f61c14c0780: {void (const google::protobuf::internal::DescriptorTable *)} 0x7f61c14c0780 <google::protobuf::(anonymous namespace)::AssignDescriptorsImpl(google::protobuf::internal::DescriptorTable const*)>, __once=...) at /usr/include/c++/7/mutex:698
#8 google::protobuf::internal::AssignDescriptors (table=<optimized out>, table#entry=0x7f61c17dddc0 <descriptor_table_google_2fprotobuf_2fdescriptor_2eproto>) at google/protobuf/generated_message_reflection.cc:2407
#9 0x00007f61c148f440 in google::protobuf::FileDescriptorProto::GetMetadataStatic () at ./google/protobuf/descriptor.pb.h:623
#10 google::protobuf::FileDescriptorProto::GetMetadata (this=<optimized out>) at google/protobuf/descriptor.pb.cc:2281
#11 0x00005568acbb22ed in google::protobuf::Message::GetReflection (this=0x7ffd47e4efc0) at /home/leo/CLionProjects/protoc-gen-java-leo/protoc/include/google/protobuf/message.h:333
#12 0x00005568acbad61f in google::protobuf::compiler::java_leo::(anonymous namespace)::CollectExtensions (message=..., extensions=0x7ffd47e4eef0) at /home/leo/CLionProjects/protoc-gen-java-leo/src/google/protobuf/compiler/java_leo/java_file.cc:84
#13 0x00005568acbad8ca in google::protobuf::compiler::java_leo::(anonymous namespace)::CollectExtensions (file_proto=..., alternate_pool=..., extensions=0x7ffd47e4eef0, file_data="\n\021addressbook.proto\022\btutorial\032\037google/protobuf/timestamp.proto\032\roptions.proto\"\255\002\n\006Person\022\"\n\002id\030\001 \001(\tB\022\222\202\031\016java.util.UUIDR\002id\022\022\n\004name\030\002 \001(\tR\004name\022\020\n\003age\030\003 \001(\005R\003age\022\024\n\005email\030\004 \001(\tR\005email\022\064\n\006phones\030\005 \003(\v2\034.tutorial.Person.PhoneNumberR\006phones\022=\n\flast_updated\030\006 \001(\v2\032.google.protobuf.TimestampR\vlastUpdated\032N\n\vPhoneNumber\022\026\n\006number\030\001 \001(\tR\006number\022'\n\004type\030\002 \001(\016\062\023.tutorial.PhoneTypeR\004type\"7\n\vAddressBook\022(\n\006people\030\001 \003(\v2\020.tutorial.PersonR\006people*+\n\tPhoneType\022\n\n\006MOBILE\020\000\022\b\n\004HOME\020\001\022\b\n\004WORK\020\002B+\n\024com.example.tutorialB\021AddressBookProtosP\001b\006proto3") at /home/leo/CLionProjects/protoc-gen-java-leo/src/google/protobuf/compiler/java_leo/java_file.cc:122
#14 0x00005568acbaf23f in google::protobuf::compiler::java_leo::FileGenerator::GenerateDescriptorInitializationCodeForImmutable (this=0x5568aec7d590, printer=0x7ffd47e4f380) at /home/leo/CLionProjects/protoc-gen-java-leo/src/google/protobuf/compiler/java_leo/java_file.cc:439
#15 0x00005568acbaed2a in google::protobuf::compiler::java_leo::FileGenerator::Generate (this=0x5568aec7d590, printer=0x7ffd47e4f380) at /home/leo/CLionProjects/protoc-gen-java-leo/src/google/protobuf/compiler/java_leo/java_file.cc:351
#16 0x00005568acbb73ea in google::protobuf::compiler::java_leo::JavaGenerator::Generate (this=0x7ffd47e4f788, file=0x5568aec77500, parameter="", context=0x7ffd47e4f5f0, error=0x7ffd47e4f5d0) at /home/leo/CLionProjects/protoc-gen-java-leo/src/google/protobuf/compiler/java_leo/java_generator.cc:158
#17 0x00007f61c0f06fae in google::protobuf::compiler::CodeGenerator::GenerateAll (this=0x7ffd47e4f788, files=std::vector of length 2, capacity 2 = {...}, parameter="", generator_context=0x7ffd47e4f5f0, error=0x7ffd47e4f5d0) at google/protobuf/compiler/code_generator.cc:58
#18 0x00007f61c0f16733 in google::protobuf::compiler::GenerateCode (request=..., generator=..., response=response#entry=0x7ffd47e4f6a0, error_msg=error_msg#entry=0x7ffd47e4f680) at google/protobuf/compiler/plugin.cc:133
#19 0x00007f61c0f16b17 in google::protobuf::compiler::PluginMain (argc=<optimized out>, argv=0x7ffd47e4f8c8, generator=0x7ffd47e4f788) at google/protobuf/compiler/plugin.cc:169
#20 0x00005568acbe9ed5 in main (argc=1, argv=0x7ffd47e4f8c8) at /home/leo/CLionProjects/protoc-gen-java-leo/main.cpp:10
#21 0x00007f61c097d153 in __libc_start_main () from /usr/lib/libc.so.6
#22 0x00005568acb9598e in _start ()
Since I'm a total C++ noob, it took me around 8 hours trying stuff out and googleing around before I found out the reason, so I wanted to share it here for the next person that might run into this problem.
It turns out, that the "pthread"-library is required and it crashes while calling the "call_once"-method if it's not present. A better error-message would have been great, "what(): Unknown error -1" didn't help out a lot :D
All I did was to add this line in my CMakeLists.txt:
target_link_libraries(${CMAKE_PROJECT_NAME} pthread)
Now it runs like a charm :)
Hope this helps someone.

After SDL_Quit(), return interrupt csignal control back to Qt

I'm working on a program that only needs to use SDL to get screen resolutions when needed. I'd like to start the SDL init, get what I need, then close it. But I'm having trouble because when I start the SDL is takes over the single interrupt handle, I can't even set it to SIG_IGN...
I have a main method that creates a QApplication and a mainWidget.
While running, a function in the mainWidget starts an SDL instance using the following code:
bool MainWidget::getSomeSdlInformation()
{
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");
SDL_SetMainReady();
SDL_Init(0);
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
qDebug() << "SDL_Init failed: " << QString::fromUtf8(SDL_GetError());
SDL_Quit();
return false;
}
signal(SIGINT, SIG_DFL);
// Get the needed video information
// Quit SDL
SDL_Quit();
return true;
}
After I "Quit SDL" I'd like to return the csignal interrupt back to QT, such that when a user presses control-c the program closes gracefully.
I'm having some trouble figuring our how to return control of the handle back to QT.
Here is an a crash log from preforming the control-c operation on my existing implementation:
Program received signal SIGSEGV, Segmentation fault.
__GI___pthread_mutex_lock (mutex=0x7fffffffd2f0) at ../nptl/pthread_mutex_lock.c:66
66 ../nptl/pthread_mutex_lock.c: No such file or directory.
(gdb) bt
#0 __GI___pthread_mutex_lock (mutex=0x7fffffffd2f0) at ../nptl/pthread_mutex_lock.c:66
#1 0x00007ffff48070da in XrmQGetResource () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#2 0x00007ffff47e4a42 in XGetDefault () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#3 0x00007ffff43ab0d0 in _XcursorGetDisplayInfo () from /usr/lib/x86_64-linux-gnu/libXcursor.so.1
#4 0x00007ffff43ab1a9 in XcursorGetDefaultSize () from /usr/lib/x86_64-linux-gnu/libXcursor.so.1
#5 0x00007ffff43ad435 in XcursorLibraryLoadCursor () from /usr/lib/x86_64-linux-gnu/libXcursor.so.1
#6 0x00007ffff706ed5c in ?? () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#7 0x00007ffff706f485 in QCursor::handle() const () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#8 0x00007ffff707b364 in ?? () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#9 0x00007ffff7080398 in QWidgetPrivate::create_sys(unsigned long, bool, bool) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#10 0x00007ffff703231d in QWidget::create(unsigned long, bool, bool) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#11 0x00007ffff703a86a in QWidget::setVisible(bool) () from /usr/lib/x86_64-linux-gnu/libQtGui.so.4
#12 0x0000000000421270 in show (this=0x7fffffffda60) at /usr/include/qt4/QtGui/qwidget.h:497
#13 main (argc=1, argv=) at /home/jeff/Dev/openmw/apps/launcher/main.cpp:55

Qt Application stuck waiting for a wake event from the system

I am in a bit of a pickle.
My application has one main window (inherits from QMainWindow). The main window is parent to a QTabWidget, which acts as a sort of dialog that shows up when the user clicks a toolbar button on the main window, and hides when its 'Close' button is clicked. In addition, the main window has another toolbar button, which when clicked, calls exec() on a QDialog. While developing the app, I have noticed that once every 40 times or so, clicking one of the two buttons I have mentioned causes an empty widget to appear, but instead of the intended view being drawn, everything freezes. When I click the 'Close' button on the main window, my OS tells me that the application has stopped responding. There is never any way to recover from crashes like this, and I am usually forced to kill the app and restart.
The vexing thing about all this is that I cannot reproduce the crashes at will. They are impossible to predict, and often catch me off-guard.
I used gdb to attach to a frozen instance, and got the following back trace:
#0 pthread_cond_wait##GLIBC_2.3.2 ()
at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
#1 0x00007f1ba3bf1b03 in QWaitCondition::wait(QMutex*, unsigned long) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
#2 0x00007f1ba3bea277 in QReadWriteLock::lockForRead() ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
#3 0x00007f1b9bf5ee4e in ?? () from /opt/Qt/5.4/gcc_64/plugins/platforms/../../lib/libQt5DBus.so.5
#4 0x00007f1b9bf61d71 in ?? () from /opt/Qt/5.4/gcc_64/plugins/platforms/../../lib/libQt5DBus.so.5
#5 0x00007f1b9bf64e54 in ?? () from /opt/Qt/5.4/gcc_64/plugins/platforms/../../lib/libQt5DBus.so.5
#6 0x00007f1b904159d6 in dbus_connection_dispatch () from /lib/x86_64-linux-gnu/libdbus-1.so.3
#7 0x00007f1b9bf579f5 in ?? () from /opt/Qt/5.4/gcc_64/plugins/platforms/../../lib/libQt5DBus.so.5
#8 0x00007f1b9bfa78f5 in ?? () from /opt/Qt/5.4/gcc_64/plugins/platforms/../../lib/libQt5DBus.so.5
#9 0x00007f1ba3e5a326 in QObject::event(QEvent*) () from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
#10 0x00007f1ba510b8f4 in QApplicationPrivate::notify_helper(QObject*, QEvent*) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#11 0x00007f1ba510f506 in QApplication::notify(QObject*, QEvent*) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#12 0x00007f1ba3e25c84 in QCoreApplication::notifyInternal(QObject*, QEvent*) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
#13 0x00007f1ba3e28868 in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
#14 0x00007f1ba3e80123 in ?? () from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
#15 0x00007f1ba283be04 in g_main_context_dispatch () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#16 0x00007f1ba283c048 in ?? () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#17 0x00007f1ba283c0ec in g_main_context_iteration () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#18 0x00007f1ba3e80554 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>)
() from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
#19 0x00007f1ba3e23eab in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
#20 0x00007f1ba530a2ed in QDialog::exec() () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#21 0x0000000000425fe4 in MainWindow::AddNewDownload() ()
#22 0x00007f1ba3e57e7a in QMetaObject::activate(QObject*, int, int, void**) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
#23 0x00007f1ba50fee52 in QAction::triggered(bool) () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#24 0x00007f1ba5100c77 in QAction::activate(QAction::ActionEvent) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#25 0x00007f1ba520b0b5 in ?? () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#26 0x00007f1ba520b374 in QAbstractButton::mouseReleaseEvent(QMouseEvent*) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#27 0x00007f1ba52cf89a in QToolButton::mouseReleaseEvent(QMouseEvent*) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#28 0x00007f1ba514723c in QWidget::event(QEvent*) () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#29 0x00007f1ba52d0730 in QToolButton::event(QEvent*) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#30 0x00007f1ba510b8f4 in QApplicationPrivate::notify_helper(QObject*, QEvent*) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#31 0x00007f1ba510f071 in QApplication::notify(QObject*, QEvent*) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#32 0x00007f1ba3e25c84 in QCoreApplication::notifyInternal(QObject*, QEvent*) ()
from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
#33 0x00007f1ba510df88 in QApplicationPrivate::sendMouseEvent(QWidget*, QMouseEvent*, QWidget*, QWidget*, QWidget**, QPointer<QWidget>&, bool) () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#34 0x00007f1ba5162387 in ?? () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
#35 0x00007f1ba5164e78 in ?? () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
---Type <return> to continue, or q <return> to quit---
It appears that my app is stuck waiting to be woken up when a certain condition is met. The presence of dbus in the back trace suggests that it was trying to talk to another process, which I am guessing is a GUI-related process. I however do not know why the wait condition is met 98% of the time, but fails 2% of the time. When using strace, I was able to find that the system call that blocked is:
futex(0x7f403c00690c, FUTEX_WAIT_PRIVATE, 1, NULL)
I am not using QThreads or mutexes in my code, so I am guessing this problem is Qt-internal. Does anyone with knowledge of low-level workings of Qt know what is happening?

How can I figure out where and why this segmentation fault occurs?

I'm having trouble figuring out the problem with my code...I'm in the early stages of prototyping a game (my first serious project). It frequently, but not always, crashes with a segmentation fault. Here's the flow of the program...
title screen - press z to start new game
player and enemy on screen...enemy takes a life when collided with, there are 4 lives.
when character's life is 0, game goes back to title screen - press z to start new game
The last step is where the crash occurs...The crash only seems to happen after the player dies and is brought back to the title screen and the player presses z to start another game, but it doesn't always crash. Through gdb, I've determined that it happens when the deconstructor for Title is called...
Here's the debug info and relevant code...I'll provide any other code/info if requested.
*** glibc detected *** /home/rzrscm/code/demogamething/game: free(): invalid pointer: 0x080c6b98 ***
//memory map info omitted
0xb7c54537 in raise () from /lib/libc.so.6
(gdb) where
#0 0xb7c54537 in raise () from /lib/libc.so.6
#1 0xb7c57922 in abort () from /lib/libc.so.6
#2 0xb7c8afbd in ?? () from /lib/libc.so.6
#3 0xb7c950ca in ?? () from /lib/libc.so.6
#4 0xb7c96918 in ?? () from /lib/libc.so.6
#5 0xb7c99a5d in free () from /lib/libc.so.6
#6 0xb7f4e776 in SDL_FreeSurface () from /usr/lib/libSDL-1.2.so.0
#7 0x0804ac7f in ~Title (this=0x80b4250, __in_chrg=<value optimized out>) at title.cpp:13
#8 0x08049d3a in GameState::load (this=0x804e368, loadState=LEVEL) at gamestate.cpp:39
#9 0x08049c5c in GameState::change (this=0x804e368, changeTo=LEVEL) at gamestate.cpp:26
#10 0x08049753 in Player::handleEvent (this=0x804e300) at player.cpp:102
#11 0x080490c8 in main () at main.cpp:27
So, what I know is that the crash occurs when it's changing states from TITLE to LEVEL...Here's the class function that unloads and loads the states...currentState is a private vector in the GameState class...
std::vector<GameState *> currentState;
void GameState::load(STATE loadState) {
if(state == TITLE) {
while(!currentState.empty()) {
delete currentState.back();
currentState.pop_back();
}
currentState.push_back(new Title());
}
else if(state == LEVEL) {
while(!currentState.empty()) {
delete currentState.back();
currentState.pop_back();
}
currentState.push_back(new Level(currentLevel));
}
}
The crash happens when the deconstructor for the Title class is called...It happens whether it's freeing the music or the image...Whichever one is the first function is the one it crashes on.
Title::~Title() {
SDL_FreeSurface(background);
Mix_FreeMusic(music);
background = NULL;
music = NULL;
}
Here's the code for the image loading function...
SDL_Surface *loadImage(std::string imageFile) {
SDL_Surface *loadedImage;
SDL_Surface *newImage;
loadedImage = IMG_Load(imageFile.c_str());
newImage = SDL_DisplayFormatAlpha(loadedImage);
SDL_FreeSurface(loadedImage);
return newImage;
}
[edit] I ran it through the debugger a couple more times without making changes to the code since I still can't find what's wrong...And each time the crash occurred while trying to free the music...
0xb7c54537 in raise () from /lib/libc.so.6
(gdb) backtrace
#0 0xb7c54537 in raise () from /lib/libc.so.6
#1 0xb7c57922 in abort () from /lib/libc.so.6
#2 0xb7c8afbd in ?? () from /lib/libc.so.6
#3 0xb7c950ca in ?? () from /lib/libc.so.6
#4 0xb7c9633e in ?? () from /lib/libc.so.6
#5 0xb79974e2 in ?? () from /usr/lib/libmikmod.so.2
#6 0xb7997640 in Player_Free () from /usr/lib/libmikmod.so.2
#7 0xb7ebb6e3 in Mix_FreeMusic () from /usr/lib/libSDL_mixer-1.2.so.0
#8 0x0804ac8d in ~Title (this=0x80c6bc0, __in_chrg=<value optimized out>) at title.cpp:14
#9 0x08049d3a in GameState::load (this=0x804e368, loadState=LEVEL) at gamestate.cpp:39
#10 0x08049c5c in GameState::change (this=0x804e368, changeTo=LEVEL) at gamestate.cpp:26
#11 0x08049753 in Player::handleEvent (this=0x804e300) at player.cpp:102
#12 0x080490c8 in main () at main.cpp:27
#5 0xb7c99a5d in free () from /lib/libc.so.6
#6 0xb7f4e776 in SDL_FreeSurface () from /usr/lib/libSDL-1.2.so.
You are probably freeing a pointer which is invalid. Now I read
free(): invalid pointer: 0x080c6b98
You might be trying to free an object which was not allocated dynamically. How does background get his value?

QPixmap fromImage results in Segmentation fault

I'm at a complete loss here...
I'm trying to convert a QImage to a QPixmap but my program keeps crashing with the following stack trace. What's weird is it works fine if the QRectF that I specify doesn't have an upper left point of (0, 0). I know QImage has a copy method as well, but that was also failing (with no useful stacktrace), so I figured I'd try to work with Pixmaps instead... Line's 4/5 of the stacktrace are mine, but I'm basically doing the following
QImage _drawing = QImage(1024, 1024);
// fill _drawing with some data
QRect i_work = QRect(1, 1, 10, 10);
QRect i_break = QRect(0, 0, 10, 10);
QPixmap foo = QPixmap::fromImage(_drawing);
// QPixmap good = foo.copy(i_work); // I work
QPixmap broken = foo.copy(i_break); // Segmentation fault...
StackTrace:
Program received signal SIGSEGV, Segmentation fault.
0x01f44024 in QBasicAtomicInt::ref (this=0xabababab)
at ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h:120
120 ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h: No such file
or directory.
in ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h
(gdb) bt
#0 0x01f44024 in QBasicAtomicInt::ref (this=0xabababab)
at ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h:120
#1 0x01964d3e in QImage::QImage (this=0x28cc70, image=...)
at image\qimage.cpp:1125
#2 0x01990682 in QRasterPixmapData::fromImage (this=0xc391a00,
sourceImage=..., flags=...) at image\qpixmap_raster.cpp:151
#3 0x019862ab in QPixmap::fromImage (image=..., flags=...)
at image\qpixmap.cpp:2031
#4 0x00423fdc in TxImage::getSelection (this=0xc3b6cb0, selection_area=...)
at TxImage.cpp:207
#5 0x004421e2 in RomView::mouseReleaseEvent (this=0xc397978, event=0x28d5fc)
at RomView.cpp:261
#6 0x019241a0 in QWidget::event (this=0xc397978, event=0x28d5fc)
at kernel\qwidget.cpp:8187
#7 0x01c9e6bc in QFrame::event (this=0xc397978, e=0x28d5fc)
at widgets\qframe.cpp:557
#8 0x01d2bf0f in QAbstractScrollArea::viewportEvent (this=0xc397978,
e=0x28d5fc) at widgets\qabstractscrollarea.cpp:1043
#9 0x01e9aae6 in QGraphicsView::viewportEvent (this=0xc397978,
event=0x28d5fc) at graphicsview\qgraphicsview.cpp:2862
#10 0x01f86a13 in QAbstractScrollAreaPrivate::viewportEvent (this=0xc3969f8,
event=0x28d5fc) at widgets//qabstractscrollarea_p.h:100
#11 0x01f8506c in QAbstractScrollAreaFilter::eventFilter (this=0xbfa7388, o=
0xbf43978, e=0x28d5fc) at widgets//qabstractscrollarea_p.h:116
#12 0x6a1ffc73 in QCoreApplicationPrivate::sendThroughObjectEventFilters (
this=0xa7a46d8, receiver=0xbf43978, event=0x28d5fc)
at kernel\qcoreapplication.cpp:847
#13 0x018d96e5 in QApplicationPrivate::notify_helper (this=0xa7a46d8,
receiver=0xbf43978, e=0x28d5fc) at kernel\qapplication.cpp:4392
#14 0x018d7909 in QApplication::notify (this=0x28fe34, receiver=0xbf43978,
e=0x28d5fc) at kernel\qapplication.cpp:3959
#15 0x6a1ff9dc in QCoreApplication::notifyInternal (this=0x28fe34,
receiver=0xbf43978, event=0x28d5fc) at kernel\qcoreapplication.cpp:732
#16 0x01f4d53e in QCoreApplication::sendSpontaneousEvent (receiver=0xbf43978,
event=0x28d5fc)
at ../../include/QtCore/../../src/corelib/kernel/qcoreapplication.h:218
#17 0x018d6118 in QApplicationPrivate::sendMouseEvent (receiver=0xbf43978,
event=0x28d5fc, alienWidget=0xc3adb70, nativeWidget=0x28fd60,
buttonDown=0x237941c, lastMouseReceiver=..., spontaneous=true)
at kernel\qapplication.cpp:3056
#18 0x0193fc63 in QETWidget::translateMouseEvent (this=0x28fd60, msg=...)
at kernel\qapplication_win.cpp:3317
#19 0x0193aaa6 in QtWndProc (hwnd=0x130fb8, message=514, wParam=0,
lParam=5373956) at kernel\qapplication_win.cpp:1657
#20 0x762c62fa in USER32!OffsetRect () from C:\Windows\syswow64\user32.dll
#21 0x900010c2 in ?? ()
#22 0x90909090 in ?? ()
#23 0x00df7d80 in operator+ ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb)
I assure you that qatomic_i386.h exists and looking at line 120...
112: inline bool QBasicAtomicInt::ref()
113: {
114: unsigned char ret;
115: asm volatile("lock\n"
116: "incl %0\n"
117: "setne %1"
118: : "=m" (_q_value), "=qm" (ret)
119: : "m" (_q_value)
120: : "memory");
121: return ret != 0;
122: }
It is very difficult to tell what is going on. However QT Doc for QImage constructor says that
Warning: This will create a QImage
with uninitialized data. Call fill()
to fill the image with an appropriate
pixel value before drawing onto it
with QPainter.
Ensure that your QImage is filled properly by calling fill () method. Maybe this is the cause of the problem.
I was successfully able to solve the problem, but I'm not sure of the underlying problem, as the workaround doesn't really make a whole lot of sense to me.
So I was rendering the QImage on a QGraphicsScene/QGraphicsView. I had my QImage at location (0,0) on the scene and another QGraphicsItem also at location (0,0). Removing the other QGraphicsItem from the scene before calling copy on the QImage seemed to fix the problem. I've no idea what's so special about the point (0,0). The only thing I can think of is that isNull on a QPoint(0,0) returns true.