doucha7329 2012-08-23 20:58
浏览 75

是什么导致我的HTTP服务器出现“退出状态-1073741819”失败?

As an exercise I created a small HTTP server that generates random game mechanics, similar to this one. I wrote it on a Windows 7 (32-bit) system and it works flawlessly. However, when I run it on my home machine, Windows 7 (64-bit), it always fails with the same message: exit status -1073741819. I haven't managed to find anything on the web which references that status code, so I don't know how important it is.

Here's code for the server, with redundancy abridged:

package main

import (
    "fmt"
    "math/rand"
    "time"
    "net/http"
    "html/template"
)

// Info about a game mechanic
type MechanicInfo struct { Name, Desc string }

// Print a mechanic as a string
func (m MechanicInfo) String() string {
    return fmt.Sprintf("%s: %s", m.Name, m.Desc)
}

// A possible game mechanic
var (
    UnkillableObjects = &MechanicInfo{"Avoiding Unkillable Objects",
                                      "There are objects that the player cannot touch. These are different from normal enemies because they cannot be destroyed or moved."}
    //...
    Race              = &MechanicInfo{"Race",
                                      "The player must reach a place before the opponent does. Like \"Timed\" except the enemy as a \"timer\" can be slowed down by the player's actions, or there may be multiple enemies being raced against."}
)

// Slice containing all game mechanics
var GameMechanics []*MechanicInfo

// Pseudorandom number generator
var prng *rand.Rand

// Get a random mechanic
func RandMechanic() *MechanicInfo {
    i := prng.Intn(len(GameMechanics))
    return GameMechanics[i]
}


// Initialize the package
func init() {
    prng = rand.New(rand.NewSource(time.Now().Unix()))

    GameMechanics = make([]*MechanicInfo, 34)
    GameMechanics[0] = UnkillableObjects
    //...
    GameMechanics[33] = Race
}

// serving

var index = template.Must(template.ParseFiles(
    "templates/_base.html",
    "templates/index.html",
))

func randMechHandler(w http.ResponseWriter, req *http.Request) {
    mechanics := [3]*MechanicInfo{RandMechanic(), RandMechanic(), RandMechanic()}
    if err := index.Execute(w, mechanics); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

func main() {
    http.HandleFunc("/", randMechHandler)
    if err := http.ListenAndServe(":80", nil); err != nil {
        panic(err)
    }
}

In addition, the unabridged code, the _base.html template, and the index.html template.

What could be causing this issue? Is there a process for debugging a cryptic exit status like this?

  • 写回答

1条回答 默认 最新

  • dqd2800 2012-08-28 15:36
    关注

    When I ran it, I got the following two errors:

    template: content:6: nil pointer evaluating *main.MechanicInfo.Name
    http: multiple response.WriteHeader calls
    

    The former was in the web browser, the latter in the console window where I launched your server.

    The nil pointer problem is because your abridged program leaves GameMechanics[1:32] set to nil.

    The second error is interesting. The only place in your program that any methods on your http.ResponseWriter get called is inside of index.Execute, which is not your code -- meaning maybe there is something wrong happening in html/template. I'm testing this with Go 1.0.2.

    I put _base.html at the top of index.html and then changed index to this:

    var index = template.Must(template.ParseFiles("templates/index.html"))
    

    and the http.WriteHeaders warning went away.

    Not really an answer, but a direction you could explore.

    As a bonus, here's the more "Go way" of writing your program. Note that I simplified the use of the PRNG (you don't need to instantiate unless you want several going in parallel) and simplified the structure initializer:

    package main
    
    import (
        "fmt"
        "html/template"
        "math/rand"
        "net/http"
    )
    
    // Info about a game mechanic
    type MechanicInfo struct{ Name, Desc string }
    
    // Print a mechanic as a string
    func (m MechanicInfo) String() string {
        return fmt.Sprintf("%s: %s", m.Name, m.Desc)
    }
    
    // The game mechanics
    var GameMechanics = [...]*MechanicInfo{
        {"Avoiding Unkillable Objects",
            "There are objects that the player cannot touch. These are different from normal enemies because they cannot be destroyed or moved."},
        {"Race",
            "The player must reach a place before the opponent does. Like \"Timed\" except the enemy as a \"timer\" can be slowed down by the player's actions, or there may be multiple enemies being raced against."},
    }
    
    // Get a random mechanic
    func RandMechanic() *MechanicInfo {
        i := rand.Intn(len(GameMechanics))
        return GameMechanics[i]
    }
    
    var index = template.Must(template.ParseFiles("templates/index.html"))
    
    func randMechHandler(w http.ResponseWriter, req *http.Request) {
        mechanics := [3]*MechanicInfo{RandMechanic(), RandMechanic(), RandMechanic()}
        if err := index.Execute(w, mechanics); err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }
    }
    
    func main() {
        http.HandleFunc("/", randMechHandler)
        if err := http.ListenAndServe(":80", nil); err != nil {
            panic(err)
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题