Discussion:
StrConv(ByteArray, vbUnicode) raising an "Out of memory" error
(too old to reply)
Deanna Earley
2014-10-23 08:35:40 UTC
Permalink
I really hope that something has changed recently and that I'm not going
crazy but...

Does anyone know if the handling of StrConv() to convert an ANSI/local
code page byte array to a string has changed recently?

I have used it for years as a simple conversion:

Data = GetData()
Result = StrConv(Data, vbUnicode)

This works most of the time, but in the last month, I've now seen it
raising error 7, Out of memory when the input byte array is empty (0
length, 0 to -1)
I'd swear on my life this has worked for the last I don't know how many
years...

I've recently had to rebuild my dev machine and is now 64-bit Windows 7
(with VB6 SP6 of course) which could be the cause but another 32-bit
machine does the same thing. Maybe a Windows Update?

Thanks
--
Deanna Earley (***@icode.co.uk)
iCatcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be printed, shredded then fed
to the rats. Please reply to the group.)
Arne Saknussemm
2014-10-23 08:39:47 UTC
Permalink
On Thu, 23 Oct 2014 09:35:40 +0100
"Deanna Earley" wrote in
microsoft.public.vb.general.discussion,comp.lang.basic.visual.misc
Post by Deanna Earley
Data = GetData()
Result = StrConv(Data, vbUnicode)
This works most of the time, but in the last month, I've now seen it
raising error 7, Out of memory when the input byte array is empty (0
length, 0 to -1)
What do you mean with that "0 to -1" ? If that's the upper bound of the
array, I wonder if the strconv considers it "unsigned" so trying to
deal with an array whose upper bound is "ffff" (or something like that)
Deanna Earley
2014-10-23 10:46:16 UTC
Permalink
Post by Arne Saknussemm
On Thu, 23 Oct 2014 09:35:40 +0100
"Deanna Earley" wrote in
microsoft.public.vb.general.discussion,comp.lang.basic.visual.misc
Post by Deanna Earley
Data = GetData()
Result = StrConv(Data, vbUnicode)
This works most of the time, but in the last month, I've now seen it
raising error 7, Out of memory when the input byte array is empty (0
length, 0 to -1)
What do you mean with that "0 to -1" ? If that's the upper bound of the
array, I wonder if the strconv considers it "unsigned" so trying to
deal with an array whose upper bound is "ffff" (or something like that)
A 0 length array has bounds of 0 to -1 (as 0 to 0 is 1 item).
You can easily create a 0 length byte array by assigning an empty string
to it.
That is a possibility though, but begs the question of has this changed?
I still swear this used to work...
--
Deanna Earley (***@icode.co.uk)
iCatcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be printed, shredded then fed
to the rats. Please reply to the group.)
Arne Saknussemm
2014-10-23 13:09:41 UTC
Permalink
:: On Thu, 23 Oct 2014 11:46:16 +0100
:: (microsoft.public.vb.general.discussion,comp.lang.basic.visual.misc)
Post by Deanna Earley
A 0 length array has bounds of 0 to -1 (as 0 to 0 is 1 item).
You can easily create a 0 length byte array by assigning an empty
string to it.
Hey !! Stop biting, I was just trying to make a guess :) !
Post by Deanna Earley
That is a possibility though, but begs the question of has this
changed? I still swear this used to work...
I don't know; but then, why don't you write your own "ConvStr" function
with error trapping and all that and use find/replace to use it in
place of the vanilla one :) ?
--
WARNING: The Law of Large Numbers also applies to assholes. The number
of assholes of any given form is never zero, nor can you make it so.
Deanna Earley
2014-10-23 13:28:37 UTC
Permalink
Post by Arne Saknussemm
:: On Thu, 23 Oct 2014 11:46:16 +0100
:: (microsoft.public.vb.general.discussion,comp.lang.basic.visual.misc)
Post by Deanna Earley
A 0 length array has bounds of 0 to -1 (as 0 to 0 is 1 item).
You can easily create a 0 length byte array by assigning an empty
string to it.
Hey !! Stop biting, I was just trying to make a guess :) !
Post by Deanna Earley
That is a possibility though, but begs the question of has this
changed? I still swear this used to work...
I don't know; but then, why don't you write your own "ConvStr" function
with error trapping and all that and use find/replace to use it in
place of the vanilla one :) ?
I can do, it was more annoyance that it's only recently started
happening, and the same code has been in use for years, and now,
suddenly, I'm getting tens of errors a day.
As far as I can tell, the source data is the same as it's always been...

Oh well :)
--
Deanna Earley (***@icode.co.uk)
iCatcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be printed, shredded then fed
to the rats. Please reply to the group.)
Farnsworth
2014-10-23 12:04:24 UTC
Permalink
I was able to duplicate the error with VB6 with SP5 applied, but with SP6
runtime files on Windows XP+SP2. I haven't applied any OS updates for a long
time. Here is the code I used:

Option Explicit

Private Sub Form_Load()
Dim Data() As Byte
Dim Result As String

Data = ""
Debug.Print LBound(Data), UBound(Data)
Result = StrConv(Data, vbUnicode)

End Sub

Output:

0 -1

If you use it in many places, you can write your own DoStrConv() function
that takes care of that special case.
Farnsworth
2014-10-23 12:35:13 UTC
Permalink
Post by Farnsworth
I was able to duplicate the error with VB6 with SP5 applied, but with SP6
runtime files on Windows XP+SP2. I haven't applied any OS updates for a
Option Explicit
Private Sub Form_Load()
Dim Data() As Byte
Dim Result As String
Data = ""
Debug.Print LBound(Data), UBound(Data)
Result = StrConv(Data, vbUnicode)
End Sub
0 -1
If you use it in many places, you can write your own DoStrConv() function
that takes care of that special case.
If I use "Dim Data As Variant", the error goes away.
Farnsworth
2014-10-23 15:48:14 UTC
Permalink
Post by Deanna Earley
Data = GetData()
Result = StrConv(Data, vbUnicode)
What's the declaration for Data and GetData?
Deanna Earley
2014-10-24 08:47:40 UTC
Permalink
Post by Farnsworth
Post by Deanna Earley
Data = GetData()
Result = StrConv(Data, vbUnicode)
What's the declaration for Data and GetData?
Sorry, a byte array:
Dim Data() As Byte
Function GetData() As Byte()
(The later isn't what's actually in use but the type matches)
--
Deanna Earley (***@icode.co.uk)
iCatcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be printed, shredded then fed
to the rats. Please reply to the group.)
Deanna Earley
2014-10-24 13:47:44 UTC
Permalink
Post by Deanna Earley
I really hope that something has changed recently and that I'm not going
crazy but...
Does anyone know if the handling of StrConv() to convert an ANSI/local
code page byte array to a string has changed recently?
Data = GetData()
Result = StrConv(Data, vbUnicode)
This works most of the time, but in the last month, I've now seen it
raising error 7, Out of memory when the input byte array is empty (0
length, 0 to -1)
I'd swear on my life this has worked for the last I don't know how many
years...
I've recently had to rebuild my dev machine and is now 64-bit Windows 7
(with VB6 SP6 of course) which could be the cause but another 32-bit
machine does the same thing. Maybe a Windows Update?
It turns out I'd accidentally introduced a Data = "" in the source
function that triggered the error as opposed to the original
undimensioned array which correctly returns an empty string.
It must have been pure luck that I've not noticed it breaks with an
explicitly empty array until now...
Thanks for everyone's comments.
--
Deanna Earley (***@icode.co.uk)
iCatcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be printed, shredded then fed
to the rats. Please reply to the group.)
Arne Saknussemm
2014-10-27 11:32:52 UTC
Permalink
:: On Fri, 24 Oct 2014 14:47:44 +0100
:: (microsoft.public.vb.general.discussion,comp.lang.basic.visual.misc)
Post by Deanna Earley
It turns out I'd accidentally introduced a Data = "" in the source
function that triggered the error as opposed to the original
undimensioned array which correctly returns an empty string.
Oh well, the Earley bird catches the bug :) !
--
WARNING: The Law of Large Numbers also applies to assholes. The number
of assholes of any given form is never zero, nor can you make it so.
Deanna Earley
2014-10-27 12:14:17 UTC
Permalink
Post by Arne Saknussemm
:: On Fri, 24 Oct 2014 14:47:44 +0100
:: (microsoft.public.vb.general.discussion,comp.lang.basic.visual.misc)
Post by Deanna Earley
It turns out I'd accidentally introduced a Data = "" in the source
function that triggered the error as opposed to the original
undimensioned array which correctly returns an empty string.
Oh well, the Earley bird catches the bug :) !
*groan* :p
--
Deanna Earley (***@icode.co.uk)
iCatcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be printed, shredded then fed
to the rats. Please reply to the group.)
Arne Saknussemm
2014-10-28 10:22:18 UTC
Permalink
On Mon, 27 Oct 2014 12:14:17 +0000
"Deanna Earley" wrote in
microsoft.public.vb.general.discussion,comp.lang.basic.visual.misc
Post by Deanna Earley
Post by Arne Saknussemm
Oh well, the Earley bird catches the bug :) !
*groan* :p
forgive me, it was a bad day :)

Blue Planet
2014-10-27 16:11:38 UTC
Permalink
Post by Deanna Earley
It turns out I'd accidentally introduced a Data = "" in the source
function that triggered the error as opposed to the original undimensioned
array which correctly returns an empty string.
It must have been pure luck that I've not noticed it breaks with an
explicitly empty array until now...
Thanks for everyone's comments.
Glad you found it.

This is one of the downsides of the "empty" vs. "unallocated" dynamic Byte
array. It would be nice if this didn't occur, however the "assignment of an
empty String" trick itself was probably an unintended (those otherwise quite
handy) side-effect.

If we ever got a VB 7.0 we'd be at risk of the trick being broken. But then
again a new VB might have addressed all of these issues through broader
support for arrays, Strings, character encodings, etc.

Just one more thing the stubborn VB6 programmer needs to be aware of! ;)
Deanna Earley
2014-10-27 16:26:01 UTC
Permalink
Post by Blue Planet
Post by Deanna Earley
It turns out I'd accidentally introduced a Data = "" in the source
function that triggered the error as opposed to the original
undimensioned array which correctly returns an empty string.
It must have been pure luck that I've not noticed it breaks with an
explicitly empty array until now...
Thanks for everyone's comments.
Glad you found it.
This is one of the downsides of the "empty" vs. "unallocated" dynamic
Byte array. It would be nice if this didn't occur, however the
"assignment of an empty String" trick itself was probably an unintended
(those otherwise quite handy) side-effect.
True.
I settled for replacing all StrConv(..., vb(From)Unicode) with two
custom functions that range check the input(both for undimensioned and
zero length) that call MultiByteToWidechar() and WideCharToMultiByte().
This also allows me to force them to latin1, UTF-8, etc where required.
--
Deanna Earley (***@icode.co.uk)
iCatcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be printed, shredded then fed
to the rats. Please reply to the group.)
Loading...