1

So I was trying to mess around with the Pygame module, and I used pygame.mixer and pygame.key. However, when I run the following block of code, it generates an error.

Code:

import pygame, sys
pygame.mixer.init()

# Assume the sound files exist and are found
kick = pygame.mixer.Sound("kick.wav")
clap = pygame.mixer.Sound("clap.wav")

while True:
    keyPressed = pygame.key.get_pressed()
    if keyPressed[pygame.K_a]:
       pygame.mixer.Sound.play(kick)
    if keyPressed[pygame.K_d]:
       pygame.mixer.Sound.play(clap)

Error message:

*** error for object 0x101008fd0: pointer being freed was not allocated

Any help would be great!

Jerrybibo
  • 1,315
  • 1
  • 21
  • 27
  • 2
    I think you should use `pygame.init()` to init all modules. – furas Dec 01 '15 at 23:49
  • Probably operation system will not send (key) events to program without window. – furas Dec 01 '15 at 23:51
  • @furas It still didn't work - even with the window implemented. – Jerrybibo Dec 02 '15 at 00:20
  • Do you still have the same error? Window is needed by `get_pressed()` but it may not have anything to do with error. By the way: `get_pressed()` will not work without `pygame.event.get()`. – furas Dec 02 '15 at 00:37

3 Answers3

2

There are a number of reasons why your code doesn't work, see mine below.

import pygame, sys

pygame.init()

window = pygame.display.set_mode((600,400))

kick = pygame.mixer.Sound("kick.wav")
clap = pygame.mixer.Sound("clap.wav")

while True:
   for event in pygame.event.get():
      if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_a:
            kick.play()
         if event.key == pygame.K_d:
            clap.play()
      if event.type == pygame.QUIT:
         pygame.quit()
         quit()

First, you must create a display window for pygame to run.

window = pygame.display.set_mode((600,400))

Second, notice that you are assign a Sound object to the kick and clap variables. These are Sound objects that have a play() method that can be referenced with the dot operator. This isn't an error, just a bit unnecessary. Read documentation to see Sound and play() parameters. You can simply do:

kick.play()

Lastly, a more conventional way of doing the event handling.

   for event in pygame.event.get():
       if event.type == pygame.KEYDOWN:
           if event.key == pygame.K_a:
Kevin Welch
  • 1,488
  • 1
  • 9
  • 18
0

I tried your code with modifications and it works - Linux Mint, Python 2.7.10

import pygame

pygame.init() # init all modules

window = pygame.display.set_mode((600,400)) # required by event

kick = pygame.mixer.Sound("kick.wav")
clap = pygame.mixer.Sound("clap.wav")

while True:
    pygame.event.get() # required by get_pressed()

    keyPressed = pygame.key.get_pressed()
    if keyPressed[pygame.K_a]:
        print "A"
        pygame.mixer.Sound.play(kick)
    if keyPressed[pygame.K_d]:
        print "D"
        pygame.mixer.Sound.play(clap)

But you can have different problem and I can't help you.

furas
  • 134,197
  • 12
  • 106
  • 148
0

This is a malloc "double free" error.

Multiple people have seen this error and after looking over lots of sites, they basically said the same thing:

You'll find out what the object is when you break in the debugger. Just look up the call stack and you will find where you free it. That will tell you which object it is.

The easiest way to set the breakpoint is to:

  1. Go to Run -> Show -> Breakpoints (ALT-Command-B)
  2. Scroll to the bottom of the list and add the symbol malloc_error_break

The above was the accepted answer from the link linked.

Community
  • 1
  • 1
Anthony Pham
  • 3,096
  • 5
  • 29
  • 38