FIFE forums

Please login or register.

Login with username, password and session length
Advanced search  

News:

FIFE 0.4.0 has been released on 15th of January, 2017!

Author Topic: Segfault while trying to add an instance dynamically [Solved]  (Read 2589 times)

TeamZorgon

  • Newbie
  • Posts: 3
    • View Profile

So, it's likely I'm going about this the wrong way, but I'm trying to add a little indicator badge to instances on our map.  I made a new layer ("badges"), and I'm trying to add a badge object to that layer.

I originally tried to use the location of the instance object, but then rewrote it to follow the code in the map editor tool, but I must still be doing something wrong.

Any advice would be greatly appreciated.

This is the code:
Code: [Select]
200     def addBadge(self, obj):
201         """
202         adds a badge for the instance
203         """
204         # all of this code is based on code in the map editor
205         # scripts/mapcontroller.py file, specifically the functions to select
206         # cells and add objects I don't know why it segfaults...
207
208         badgelayer = self.world.map.getLayer("badges")
209
210         fifeinstance = self.world.getInstance(obj) #get the fife instance for our game object
211         instanceloc=fifeinstance.getLocation()
212         x=self.world.gui.menuController.contextMenu.clickpoint.x
213         y=self.world.gui.menuController.contextMenu.clickpoint.y
214         print "(x,y)=(%d,%d)" % (x,y)
215         #mapcoords = instanceloc.getMapCoordinates()
216         mapcoords = self.world.gui.cameraController.cam.toMapCoordinates(fife.ScreenPoint(x, y), False)
217         pos = badgelayer.getCellGrid().toLayerCoordinates(mapcoords)
218
219         loc = fife.Location(badgelayer)
220         loc.setLayerCoordinates(pos)
221
222         badge = fife.Object("badge", "Zorgon xml")
223
224         print badge
225         print badgelayer
226         print loc
227         print "loc valid: %s" % loc.isValid()
228
229         inst = badgelayer.createInstance(badge, loc.getLayerCoordinates())
230         fife.InstanceVisual.create(inst)

Here is the relevant console output:
Code: [Select]
(x,y)=(450,329)
<fife.fife.Object; proxy of <Swig Object of type 'FIFE::Object *' at 0x117f94b70> >
<fife.fife.Layer; proxy of <Swig Object of type 'FIFE::Layer *' at 0x117f94c60> >
<fife.fife.Location; proxy of <Swig Object of type 'FIFE::Location *' at 0x117f94c90> >
loc valid: True
zsh: segmentation fault  ./run.py
(There's a delay of a few seconds before the segfault)

Here's the Problem Report:
Code: [Select]
Process:         Python [15118]
Path:            /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Identifier:      org.python.python
Version:         2.7.1 (2.7.1)
Code Type:       X86-64 (Native)
Parent Process:  zsh [15095]

Date/Time:       2011-04-03 13:18:27.110 -0700
OS Version:      Mac OS X 10.6.5 (10H574)
Report Version:  6

Interval Since Last Report:          4265097 sec
Crashes Since Last Report:           83
Per-App Interval Since Last Report:  33880 sec
Per-App Crashes Since Last Report:   23
Anonymous UUID:                      E62BEDF1-94C0-493B-B3EB-873CF7D829EA

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: 0x000000000000000d, 0x0000000000000000
Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Thread 0 Crashed:  Dispatch queue: com.apple.main-thread
0   _fife.so                       0x000000010118ca9c std::_Rb_tree<unsigned int, std::pair<unsigned int const, int>, std::_Select1st<std::pair<unsigned int const, int> >, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, int> > >::upper_bound(unsigned int const&) const + 44
1   _fife.so                       0x000000010118c961 FIFE::getIndexByAngle(int, std::map<unsigned int, int, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, int> > > const&, int&) + 113
2   _fife.so                       0x00000001011e47b0 FIFE::ObjectVisual::getStaticImageIndexByAngle(int) + 32
3   _fife.so                       0x00000001011e456a FIFE::RenderItem::getStaticImageIndexByAngle(unsigned int, FIFE::Instance*) + 90
4   _fife.so                       0x00000001011cedff FIFE::LayerCache::updateEntry(FIFE::LayerCache::Entry&) + 831
5   _fife.so                       0x00000001011ceee0 FIFE::LayerCache::fullUpdate() + 80
6   _fife.so                       0x00000001011cfb27 FIFE::LayerCache::update(FIFE::Camera::Transform, std::vector<FIFE::RenderItem*, std::allocator<FIFE::RenderItem*> >&) + 1351
7   _fife.so                       0x00000001011cb664 FIFE::Camera::render() + 628
8   _fife.so                       0x000000010118474f FIFE::Map::update() + 255
9   _fife.so                       0x0000000101177f6c FIFE::Model::update() + 60
10  _fife.so                       0x000000010114d7d1 FIFE::Engine::pump() + 65
11  _fife.so                       0x000000010125f5d8 _wrap_Engine_pump + 104
12  org.python.python             0x00000001000c124d PyEval_EvalFrameEx + 22397
13  org.python.python             0x00000001000c20de PyEval_EvalFrameEx + 26126
14  org.python.python             0x00000001000c20de PyEval_EvalFrameEx + 26126
15  org.python.python             0x00000001000c20de PyEval_EvalFrameEx + 26126
16  org.python.python             0x00000001000c20de PyEval_EvalFrameEx + 26126
17  org.python.python             0x00000001000c2f49 PyEval_EvalCodeEx + 2137
18  org.python.python             0x00000001000c3066 PyEval_EvalCode + 54
19  org.python.python             0x00000001000e7b2e PyRun_FileExFlags + 174
20  org.python.python             0x00000001000e7de9 PyRun_SimpleFileExFlags + 489
21  org.python.python             0x00000001000fe75c Py_Main + 2940
22  org.python.python             0x0000000100000f14 0x100000000 + 3860

Thread 1:  Dispatch queue: com.apple.libdispatch-manager
0   libSystem.B.dylib             0x00007fff80ed916a kevent + 10
1   libSystem.B.dylib             0x00007fff80edb03d _dispatch_mgr_invoke + 154
2   libSystem.B.dylib             0x00007fff80edad14 _dispatch_queue_invoke + 185
3   libSystem.B.dylib             0x00007fff80eda83e _dispatch_worker_thread2 + 252
4   libSystem.B.dylib             0x00007fff80eda168 _pthread_wqthread + 353
5   libSystem.B.dylib             0x00007fff80eda005 start_wqthread + 13

Thread 2:
0   libSystem.B.dylib             0x00007fff80ed9f8a __workq_kernreturn + 10
1   libSystem.B.dylib             0x00007fff80eda39c _pthread_wqthread + 917
2   libSystem.B.dylib             0x00007fff80eda005 start_wqthread + 13

Thread 3:
0   libSystem.B.dylib             0x00007fff80efafca __semwait_signal + 10
1   libSystem.B.dylib             0x00007fff80efae59 nanosleep + 148
2   libSDL-1.2.0.dylib             0x0000000101712dc6 SDL_Delay + 102
3   libSDL-1.2.0.dylib             0x0000000101712e0a RunTimer + 42
4   libSDL-1.2.0.dylib             0x00000001016cb317 SDL_RunThread + 71
5   libSDL-1.2.0.dylib             0x000000010170e299 RunThread + 9
6   libSystem.B.dylib             0x00007fff80ef9536 _pthread_start + 331
7   libSystem.B.dylib             0x00007fff80ef93e9 thread_start + 13

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000200000017  rbx: 0x00000001033f3d60  rcx: 0x000000000000002d  rdx: 0x0002c80000001900
  rdi: 0x000000020000000f  rsi: 0x00007fff5fbfdf4c  rbp: 0x00007fff5fbfdf30  rsp: 0x00007fff5fbfdf30
   r8: 0x000000010023d400   r9: 0x0000000000000003  r10: 0x000000001033079f  r11: 0x00007fff80ec6a19
  r12: 0x000000020000000f  r13: 0x00000000ffffffff  r14: 0x000000000000002d  r15: 0x00007fff5fbfdf9c
  rip: 0x000000010118ca9c  rfl: 0x0000000000210206  cr2: 0x00000001033fa000

Binary Images:
<snip to stay under char limit>

Model: MacBookPro4,1, BootROM MBP41.00C1.B03, 2 processors, Intel Core 2 Duo, 2.5 GHz, 4 GB, SMC 1.27f3
Graphics: NVIDIA GeForce 8600M GT, GeForce 8600M GT, PCIe, 512 MB
Memory Module: global_name
AirPort: spairport_wireless_card_type_airport_extreme (0x14E4, 0x8C), Broadcom BCM43xx 1.0 (5.10.131.36.1)
Bluetooth: Version 2.3.8f7, 2 service, 12 devices, 1 incoming serial ports
Network Service: Ethernet, Ethernet, en0
Network Service: AirPort, AirPort, en1
Serial ATA Device: Hitachi HTS542525K9SA00, 232.89 GB
Parallel ATA Device: HL-DT-ST DVDRW  GSA-S10N
USB Device: USB2.0 Hub, 0x05e3  (Genesys Logic, Inc.), 0x0608, 0xfd100000
USB Device: Microsoft Optical Mouse with Tilt Wheel, 0x045e  (Microsoft Corporation), 0x00d1, 0xfd140000
USB Device: Keyboard Hub, 0x05ac  (Apple Inc.), 0x1006, 0xfd130000
USB Device: Apple Keyboard, 0x05ac  (Apple Inc.), 0x0220, 0xfd132000
USB Device: Built-in iSight, 0x05ac  (Apple Inc.), 0x8502, 0xfd400000
USB Device: Apple Internal Keyboard / Trackpad, 0x05ac  (Apple Inc.), 0x0230, 0x5d200000
USB Device: IR Receiver, 0x05ac  (Apple Inc.), 0x8242, 0x5d100000
USB Device: BRCM2046 Hub, 0x0a5c  (Broadcom Corp.), 0x4500, 0x1a100000
USB Device: Bluetooth USB Host Controller, 0x05ac  (Apple Inc.), 0x820f, 0x1a110000


edit: added the rest of the code which apparently didn't copy the first time.
« Last Edit: April 04, 2011, 04:40:59 pm by TeamZorgon »
Logged

vtchill

  • Developer
  • Full Member
  • *
  • Posts: 206
    • View Profile
Re: Segfault while trying to add an instance dynamically
« Reply #1 on: April 04, 2011, 10:42:05 am »

Hey Zorgon nice to see you guys making progress.

I looked over the code quickly and I am not sure exactly why the call stack shows upper_bound as the last function call.

I did however notice that you are creating an object in python using the Object constructor in this line:
Code: [Select]
badge = fife.Object("badge", "Zorgon xml")
When using the constructor for Object like this swig will set the thisown property of badge to 1
which means the python interpreter will garbage collect that variable which is not what we want
since fife is storing that variable as well.

To fix this try setting the thisown explicitly to 0 to tell swig not to allow garbage collection on this variable b/c fife will clean it up

The code should now look like this:
Code: [Select]
badge = fife.Object("badge", "Zorgon xml")
badge.thisown = 0

Let me know if this fixes your problem. Also even if this does not fix your problem it needs to be done and may prevent future hard to track down bugs.
Logged

prock

  • Developer
  • Full Member
  • *
  • Posts: 236
    • View Profile
Re: Segfault while trying to add an instance dynamically
« Reply #2 on: April 04, 2011, 12:14:45 pm »

You may also try to get a reference to the object using something like:

Code: [Select]
badge = self.engine.getModel().getObject("badge", "Zorgon xml")
This of course assumes you have self.engine defined and is a reference to the fife engine.   Oh and the badge object must be already defined.

If it is not loaded you have to define/load it yourself...  You can load the xml file using the XMLObjectLoader extension.  Something like this:

Code: [Select]
from fife.extensions.serializers.xmlobject import XMLObjectLoader
.
.
.
imagepool = engine.getImagePool()
animationpool = engine.getAnimationPool()
model = engine.getModel()
vfs = engine.getVFS()

loader = XMLObjectLoader(imagepool, animationpool, model, vfs)
loadImportFile(loader, "badge.xml", engine)

You can see a good example of this in the RPG demo in scene.py.  Check out the "loadObject" function.
 
 
Logged

TeamZorgon

  • Newbie
  • Posts: 3
    • View Profile
Re: Segfault while trying to add an instance dynamically
« Reply #3 on: April 04, 2011, 04:40:41 pm »

Sweet, thanks for the replies!

I have the object imported in the map file, so I tried getting a reference to it through the engine and it worked perfectly.

Thanks again :)
Logged