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)
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度