Skip to content
This repository has been archived by the owner on Mar 10, 2021. It is now read-only.

Attempt to use destroyed contact #24

Open
codecat opened this issue Jul 21, 2019 · 3 comments
Open

Attempt to use destroyed contact #24

codecat opened this issue Jul 21, 2019 · 3 comments

Comments

@codecat
Copy link

codecat commented Jul 21, 2019

I keep getting "Attempt to use destroyed contact" sometimes whenever I use the contact in the :exit or :stay event functions.

Am I using them wrong? What's the correct way of using them? Or should I just switch to using actual event callbacks?

@codecat
Copy link
Author

codecat commented Jul 23, 2019

As a workaround, I modified windfield a bit to - instead of passing the contact from Love2D directly - create a new table with the information I need:

local function copyContact(contact)
    local ret = { normal = {} }
    ret.normal.x, ret.normal.y = contact:getNormal()
    return ret
end

@idbrii
Copy link

idbrii commented Jun 18, 2020

I had the same problem. I worked around it by building my own list of contacts in game code:

if self.collider:enter(wall) then
    local collision_data = self.collider:getEnterCollisionData(wall)

    -- Using .contact causes 'Attempt to use destroyed contact.'
    -- respondToCollision(collision_data, collision_data.contact:getPositions())

    -- check our existing contacts instead.
    local contact_list = collision_data.collider:getContacts()
    for i,contact in ipairs(contact_list) do
        local contact_points = {contact:getPositions()}
        if #contact_points > 0 then
            respondToCollision(collision_data, unpack(contact_points))
            break
        end
    end
end

I assume this is due to a change in love? Maybe windfield worked on an older version, but newer love is more aggressive with reclaiming allocated contacts?

@codecat
Copy link
Author

codecat commented Jun 18, 2020

I assume this is due to a change in love? Maybe windfield worked on an older version, but newer love is more aggressive with reclaiming allocated contacts?

When I was looking into this previously I believe that is indeed the case.

idbrii added a commit to idbrii/love-windfield that referenced this issue Jul 3, 2021
Fix a327ex#24: error "Attempt to use destroyed contact."

love doesn't guarantee Contacts survive beyond this callback (they may
be destroyed in the same frame as creation). Make a copy so users can
access later in the frame.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants