#Problems with pointer allocation

1 messages · Page 1 of 1 (latest)

plain spoke
#
    const alloc = app.buffer_allocator.allocator();
    const open_file_path = try std.fs.path.join(alloc, &[_][]const u8{
        try std.fs.cwd().realpathAlloc(alloc, "."),
        "src/main.zig",
    });
    const open_file_lines = try buffer.readFile(alloc, open_file_path);
    const editor_view = try alloc.create(EditorView);

I am getting weird pointer errors. Do you know what i could be doing wrong? By the way, buffer_allocator is an arena and I am probably doing something stupid here.

tulip thunder
#

can you share the error you're getting?

plain spoke
#

It seems the issue is inside the read file

#

a second...

#
Loop.zig:140:35: 0x100ceae1f in ttyRun (editor)g/0.13.0/lib/zig/std/posix.zig:837:22: 0x100cc9797 
in read (editor)                                                       const n = try self.tty.read
(buf[read_start..]);        .BADF => return error.NotOpenForReading, // Can be a race condition.
                                                      ^                                           
                   ^
                    /Users/joel/.cache/zig/p/1220de23a3240e503397ea579de4fd85db422f537e10036ef7471
7c50164475813ce/src/posix/Tty.zig:110:5: 0x100cdf28b in read (editor)
                                                                         return posix.read(self.fd
, buf);
#

and the readFile

pub fn readFile(
    allocator: std.mem.Allocator,
    absolute_path: []u8,
) !std.ArrayList([]const u8) {
    const file = try std.fs.openFileAbsolute(absolute_path, .{});
    defer file.close();

    var buf_reader = std.io.bufferedReader(file.reader());
    var in_stream = buf_reader.reader();
    var file_data = std.ArrayList([]const u8).init(allocator);

    // go line by line on the file caching it
    var buf: [max_file_buffer_size]u8 = undefined;
    while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
        const duped = try allocator.dupe(u8, line);
        try file_data.append(duped);
    }

    return file_data;
}
#

i need to perfect my string knowledge for sure. it is all a bit crazy to me without it :p

#

i've set some tests on readFile and it doesn't seem to have issues?!

clever hound
plain spoke
#

already sent...

#

#1343189389605601322 message

#

that is what i get which doesnt give anything

clever hound
#

Sorry I was looking for a compilation error..

#

EBADF fd is not a valid file descriptor or is not open for
reading.

Whatever you are trying to read for some reason is not open for reading

#

Like if it was open for writing only and you are trying to read it or something like that

#

I can't see from a quick glance why you would fail reading src/main.zig

#

Also @plain spoke are you on Windows?

plain spoke
#

no mac

#

but it is weird i used the exact same stuff on the test and it passes

#

i will publish it out so you can look at the code, if you dont mind

#

if i publish the allocation lines for the "editor_view" it works

plain spoke
#

and other weird thing, i think it only happens on a key event

#

if i comment that creation of memory, it is fine

#

and i can only replicate by pressing any key event

clever hound
#

Haven't read the code yet. But maybe some event closes the file before you expect it to be closed?

plain spoke
#

well, in code, i duplicate the [] const u8 so it shouldn't matter?! is my assumption wrong?

#

also i commented all the code

clever hound
#

I think

plain spoke
#

also... why is the keyboard event relevant to this? hm

#

open_file_lines is allocated it shouldnt?!

clever hound
#

It's not, it's a dangling pointer issue

#

Oh my bad

plain spoke
#
    var file_line_data = std.ArrayList([]const u8).init(allocator);
```
clever hound
#

Okay I'll keep reading 😅

plain spoke
#

and it is not being used so even if goes out... who cares

#

could it be a out of memory issue of sorts?

#

it would be weird. a struct shouldnt weight that much

clever hound
#

You said only when you uncomment it happens right?

plain spoke
#

no

clever hound
#

The other way around?

plain spoke
#
    const open_file_lines = try buffer.readFile(alloc, open_file_path);
    _ = open_file_lines;
    const editor_view = try alloc.create(EditorView);
    _ = editor_view;

this happens, the issue. if i comment out the editor_view, it doesnt

clever hound
#

Okay thanks

plain spoke
#

oh wait...

clever hound
#

This is my chain of thought:
The error is telling you there is a bad file descriptor

plain spoke
#

commented out editor_view now and it crashed

clever hound
#

This happens when file is closed, or not a real fd, etc.

#

And unexpected crashes like you are describing is evidence for possible memory issue...

plain spoke
#

ok so the issue must be the readFile. why the tests dont fail? no clue

clever hound
#

Because dangling pointers suck (if that's the problem)

plain spoke
#

yeah... commenting out all the code and see if something else is messing this up

clever hound
#

I can't the issue quickly but I need to get back to work.

#

Have you tried to use a debugger? Set a breakpoint before you are trying to open the file and examine memory

plain spoke
#

go on go on i will try to solve it

clever hound
#

Sorry I couldn't be more helpful

plain spoke
#

i am not being able to run debugger with a cli but maybe i am being stupid

clever hound
#

Just suggesting, maybe the obvious

plain spoke
#

that is fine

#

i know this is a tricky one

#

will get back at it and try to figure