Jump to content
TheDcoder

[Solved] Error allocating memory for a 6 MB file

Recommended Posts

TheDcoder

Hello! I am having problem with using file read...

f7683dc5a8fab1924e567f1e08b7fed1.png

Here is my code:

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

Local Const $CHUNK = 1024
Local $hFile = FileOpen(@ScriptDir & '\test.bin', $FO_APPEND)
Local $iCurrentPos = 0

Do
    $iCurrentPos += $CHUNK
    $bFileSetPos = FileSetPos($hFile, $iCurrentPos, $FILE_END)
    $vData = FileRead($hFile)
    $vData = BinaryToString($vData)
    MsgBox(0, 0, $vData)
    $iStringPos = StringInStr($vData, "Test", $STR_NOCASESENSEBASIC)
Until Not ($iStringPos = 0) Or $bFileSetPos = False
FileClose($hFile)

MsgBox($MB_OK, "Success", $iStringPos)

Here is the 6 MB file: <Attachment Deleted>

 

Thanks in Advance! TD :D

Edited by TheDcoder
Appended [Solved] to title & deleted attachment

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
water

You have to define the number of bytes to read in FileRead as well. Else you read the whole file every time.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
TheDcoder

@water Huh? :huh:

I remember that FileRead reads from the position set by FileSetPos, my previous version of code just did that.

Even if it did read the whole file, I think it would be easy to store 6 MB of data... This is my test script which I use to read the whole file:

ConsoleWrite(FileRead(@ScriptDir & '\test.bin'))
MsgBox(0,0,0)
1 hour ago, water said:

You have to define the number of bytes to read in FileRead as well

I tried that now, I set the number of bytes to read to $iCurrentPos, Now for some reason the script is looping indefinitely.

 

TD :)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
TheDcoder

@water Huh? :huh:

I remember that FileRead reads from the position set by FileSetPos, my previous version of code just did that.

Even if it did read the whole file, I think it would be easy to store 6 MB of data... This is my test script which I use to read the whole file:

ConsoleWrite(FileRead(@ScriptDir & '\test.bin'))
MsgBox(0,0,0)
1 hour ago, water said:

You have to define the number of bytes to read in FileRead as well

I tried that now, I set the number of bytes to read to $iCurrentPos, Now for some reason the script is looping indefinitely.

 

TD :)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
water

Can you please describe what you try to do?
Now you

  • Open the file in write/append mode - but you only read the content
  • You use FileSetPos from the end of the file - do you really try to read the file from end to start?
  • You increment $iCurrentPos - but my understanding is that this should be a negative number for FileSetPos from the end of the file
  • You always read the file from the set position to the end - the longer you loop the more data you read
  • Error checking is not done when an error could occur (after FileSetPos or FileRead) - so you might process data in an inconsistant state
Edited by water
  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
TheDcoder
3 minutes ago, water said:

Can you please describe what you try to do?

I am trying to read the footer of a file (in this case "Test" indicates where the footer starts from) :)

5 minutes ago, water said:

Open the file in write/append mode - but you only read the content

The code is from a function in my original code, I returned the read data.

5 minutes ago, water said:

You use FileSetPos from the end of the file - do you really try to read the file from end to start?

Yep, it is necessary to read the footer :D

6 minutes ago, water said:

You increment $iCurrentPos - but my understanding is that this should be a negative number to read from the end of the file

Silly me! That fixed the problem :D

 

Thanks water! You solved my problem by asking questions! That is simply great! :thumbsup:


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
TheDcoder

This is the code which worked for me :D:

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

Local Const $CHUNK = 1024
Local $hFile = FileOpen(@ScriptDir & '\test.bin', $FO_APPEND)
Local $iCurrentPos = 0
Local $bFileSetPos = True

Do
    $iCurrentPos -= $CHUNK
    $bFileSetPos = FileSetPos($hFile, $iCurrentPos, $FILE_END)
    $vData = FileRead($hFile, $iCurrentPos)
    $vData = BinaryToString($vData)
    $iStringPos = StringInStr($vData, "Test", $STR_NOCASESENSEBASIC)
Until Not ($iStringPos = 0) Or $bFileSetPos = False
FileClose($hFile)

MsgBox($MB_OK, "Success", $iStringPos)

But I still don't understand why there was a memory allocation error in my faulty code :think:


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
water

Glad the problem could be solved :) 
But I still think it is a lot of overhead to check a 6MB file ;)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
TheDcoder
Just now, water said:

lot of overhead to check a 6MB file

Any suggestions? :)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
water

Read the file in one go.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
TheDcoder

@water :P. I normally process larger files, this was tiny compared to them :)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
water

Ah, new information :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
LarsJ

Share this post


Link to post
Share on other sites
TheDcoder
Just now, LarsJ said:

If you need the footer only, the read the file from the end.

? :huh:


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
LarsJ

My grandmother can code that - while she is knitting at the same time. She can multitask. You have to figure that out yourself young man.

Share this post


Link to post
Share on other sites
TheDcoder
11 minutes ago, LarsJ said:

My grandmother can code that

Your grandma is Augusta Ada!? :shocked:


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
water

No, his grandmother is a woman. And women are able to multitask. At least that's what they try to tell us.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

Guess what I found in the help file :)

"A count value that is too large can lead to AutoIt stopping with a memory allocation failure."


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
TheDcoder
Just now, water said:

No, his grandmother is a woman

https://en.wikipedia.org/wiki/Ada_Lovelace :P


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
TheDcoder

@water Oh :o!


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • ahha
      By ahha
      It took a month of cutting down a big program to find it and here's the code to illustrate "Error allocating memory".
      The first while using the filename in the fileopen, etc. shows the error.  The second while example does not have the problem (so I switched to using it).
      ;shows "Error allocating memory" ;Autoit v3.3.14.5 #AutoIt3Wrapper_run_debug_mode=Y ; use this to debug in console window <--- LOOK #include <FileConstants.au3> ;--- shows error allocating memory - is it Autoit or SciTE ? Try compiling <-- makes no difference so probably not SciTE. Normally error at index 50K-100K $fnINDEX = @ScriptDir & "\__index.txt" $i = 0 ;#cs While 1 $i = $i + 1 FileOpen($fnINDEX, $FO_OVERWRITE) ;overwrite as we just need last one processed FileWriteLine($fnINDEX, $i) FileClose($fnINDEX) If $i/1000 = Int($i/1000) Then MsgBox(0, "count", "$i = " & $i, 1) ;give indication of count, hangs when memory allocation error imminent WEnd ;#ce ;comment out the above loop and this works without an allocation error - looks like the above code leaking memory? While 1 $i = $i + 1 $fh = FileOpen($fnINDEX, $FO_OVERWRITE) ;overwrite as we just need last one processed FileWriteLine($fh, $i) FileClose($fh) If $i/1000 = Int($i/1000) Then MsgBox(0, "count", "$i = " & $i, 1) ;give indication of count WEnd Exit ;program  
×