Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Crash in WindowImpl() when overloading global new/delete g++  (Read 2648 times)

0 Members and 1 Guest are viewing this topic.

BrianDFS

  • Newbie
  • *
  • Posts: 4
    • View Profile
I've recently been trying to migrate from Visual Studio over to MinGW w/g++.  Now that I've got everything compiling and linking again, I'm getting a crash in the constructor for WindowImpl().  It appears to have something to do with the stl:deque class (I'm assuming it's something to do with the myEvents member variable).  It looks like it's either attempting to delete a bad pointer or, it's trying to delete something that it allocated with a version of new other than my overloaded new.  I've been trying to debug this with gdb, but I'm a gdb noob.

Here's the call stack:
Code: [Select]
#0  0x70624be8 in dlfree (mem=0x344b28) at Native\DF_Common\dlmalloc\malloc.c:4739
#1  0x00465767 in operator delete (Memory=0x344b28) at Native/DF_Common/DF_Memory.h:46
#2  0x0041f381 in __gnu_cxx::new_allocator<sf::Event>::deallocate(sf::Event*, unsigned int) ()
#3  0x0044c108 in std::_Deque_base<sf::Event, std::allocator<sf::Event> >::_M_deallocate_node(sf::Event*) ()
#4  0x0044bf1a in std::_Deque_base<sf::Event, std::allocator<sf::Event> >::_M_destroy_nodes(sf::Event**, sf::Event**) ()
#5  0x0044c1d6 in std::_Deque_base<sf::Event, std::allocator<sf::Event> >::~_Deque_base() ()
#6  0x004582ce in std::deque<sf::Event, std::allocator<sf::Event> >::~deque() ()
#7  0x00402fcf in sf::priv::WindowImpl::WindowImpl() ()
#8  0x00403a9b in sf::priv::WindowImplWin32::WindowImplWin32(sf::VideoMode, std::string const&, unsigned long) ()
#9  0x00402f13 in sf::priv::WindowImpl::New(sf::VideoMode, std::string const&, unsigned long) ()
#10 0x004022e7 in sf::Window::Create(sf::VideoMode, std::string const&, unsigned long, sf::ContextSettings const&) ()
#11 0x004020f6 in sf::Window::Window(sf::VideoMode, std::string const&, unsigned long, sf::ContextSettings const&) ()
#12 0x00401457 in Init (hInstance=0x400000) at Native\DF_Game\Main.cpp:160

This does not happen with Visual Studio and its version of STL.  Nor does it happen with MinGW/g++ if I don't overload global new/delete.

Any ideas and/or suggestions?[/code]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Crash in WindowImpl() when overloading global new/delete g++
« Reply #1 on: July 24, 2010, 11:48:50 pm »
Why do you overload global new and delete, and how do you do it?
Laurent Gomila - SFML developer

BrianDFS

  • Newbie
  • *
  • Posts: 4
    • View Profile
Crash in WindowImpl() when overloading global new/delete g++
« Reply #2 on: July 25, 2010, 12:13:41 am »
Quote from: "Laurent"
Why do you overload global new and delete, and how do you do it?

Typically you'll overload new and delete for explicit control over allocations and deallocations at the lowest level.  Usually the motivation behind doing it is either for better allocation/deallocation run-time (speed) performance and/or to help fight against fragmentation.  Fragmentation isn't so much of an issue with PCs, but it can be a huge issue on consoles.

You can also do it to help track memory stats as well.

Here is some example code on how to do it.  This code simply calls malloc/free from new/delete.  However, that's purely for demonstration purposes.  When done for real, new/delete will call some custom allocator/deallocator.

Code: [Select]
#include <new>

void *operator new(size_t size) throw(std::bad_alloc)
{
void *p = malloc(size);

if (!p)
{
throw std::bad_alloc();
}

return p;
}

void operator delete(void *p) throw()
{
free(p);
}

void *operator new(size_t size, const std::nothrow_t &) throw()
{
return malloc(size);
}

void operator delete(void *p, const std::nothrow_t &) throw()
{
free(p);
}

void *operator new[](size_t size) throw(std::bad_alloc)
{
void *p = malloc(size);

if (!p)
{
throw std::bad_alloc();
}

return p;
}

void operator delete[](void *p) throw()
{
free(p);
}

void *operator new[](size_t size, const std::nothrow_t &) throw()
{
return malloc(size);
}

void operator delete[](void *p, const std::nothrow_t &) throw()
{
free(p);
}


You would place this code in something like MyMemory.h and then include that before including anything else.

BrianDFS

  • Newbie
  • *
  • Posts: 4
    • View Profile
Crash in WindowImpl() when overloading global new/delete g++
« Reply #3 on: July 25, 2010, 12:29:02 am »
So I just tried with the sample code I posted above as the overloaded new/delete operators, and it doesn't crash.  That's strange.

I'm going to look at it some more.  It's possible that the standard library is not checking returned (freed) memory as thoroughly as dlmalloc is, although that's probably a stretch.

BTW, I'm using dlmalloc as my allocator.  It's a very popular (and mature) allocator that's been around for looong time (i.e its origins date back to ~1987).

BrianDFS

  • Newbie
  • *
  • Posts: 4
    • View Profile
Crash in WindowImpl() when overloading global new/delete g++
« Reply #4 on: July 25, 2010, 12:41:57 am »
Okay, so disregard, false alarm, my bad.

After working forward with the example overloads above, it looks like I had something wrong with my original implementation.  

The world is now good again.  :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Crash in WindowImpl() when overloading global new/delete g++
« Reply #5 on: July 25, 2010, 10:15:11 am »
Great.

By the way, I wasn't asking for explanations about how to implement global new/delete overloads, I just wanted to know why and how you did it in your code so that I can try to figure out what's wrong ;)
Laurent Gomila - SFML developer