Results 1 to 13 of 13

Thread: Run Time Error: "10"

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    England
    Posts
    18

    Run Time Error: "10"

    k, this is a complicated question, and no offense, but if you dont know the answer please do not comment or answer because i keep getting annoying emails telling me someone has replied

    its a very technical question, something that i cannot work out.

    I have a project, a project group consisting of a main project, an AX OCX file, and an AX DLL

    the dll has one class module in it, and is set as Multiuse.

    i am running vb6 sp5 (from visual studio) on win98-2e

    i keep getting this error
    Run-time error 10:
    "This Array is fixed or temporarily locked"

    now on the microsoft site, they do have a page about this in the support section

    http://support.microsoft.com/default...EN-US;Q176049&

    however, this does not help, basically, it says the error is only for vb5 (with sp2/3) and i have 6 and sp5, and it also says that the error can only appear when the program is in compiled form

    i get it in the IDE, i click F5 and it works, and i get the error

    the class module has an internal array, and is only instanced once in the program (and used all the time) - its an AX DLL simply because i required to pass a UDT to it, which works fine, however when i attempt to redim one of the arrays, i get the error

    according to MS, this error should have been irradicated in vb6, and i get it :|


    if anyone has come accross this problem before, and has a solution, plz tell me
    since this is a busy forum, i will be "bumping" the post until someone tells me how to resolve it or that i cant resolve it :|
    on my page, it doesnt show any work-arounds, i dont know if it should, my IE is apalling atm, and refuses to work properly.


    thx for ur help if anyone can help hehe

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    http://msdn.microsoft.com/library/de...rraylocked.asp

    Do any of the reasons listed apply to your situation? If so, you'll probably need to find a different way of doing what you need to do.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    England
    Posts
    18
    hm im not sure, the first and third for sure dont, but the second might do

    the array is not fixed, its an open ended array
    the array itself is private to the class, and its not passed (nor is any element of the array) passed between functions in the class


    if you have msn then it would be easier to discuss,andi could send u the class for you to look for yourself to see what you think is going wrong (i wouldnt like to post the code here cos the code isnt small)

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Well, I don't use IM's, but if you wanted to zip up the code and post it here, I'll give it a once over.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    England
    Posts
    18
    well i think i tried to send u a PM but i think i senti t to me, so that didnt work heh


    how can i post the zip here?

    ooh nm i found it

    (^^ i thought i would put that there so you know im a mad professor type person)
    Attached Files Attached Files

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Is there like a ready made form to go with the class? Because its a bit hard to tell what's going on, and how to recreate the error.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    England
    Posts
    18
    bah i made a nice long detailed post explaining how it works but stupid forum logs me out and errors so it dont get sent

    basicaly, its part of an irc client, and the ignore system

    a user types a message, and it gets sent to the class (AddMessage() sub)

    the class checks to see if the user who sent the message already exists in its Users() array, and if it doesnt it calls AddUser() to add the user


    the class works on the basis that the user of the program decides 5 or more messages in say 3 seconds is a flood for say, inviting him into a channel

    if a user comes along, and sends 6 invites in 2 seconds, its flooding

    the program tells the class to add a messagfe for an invite, and for each user is different arrays that hold the values of the message times

    for 5 messages, the array (aMessages) would be 1 to 5, the 1st element holding the newest message time and the 5th representing the 5th oldest.
    the time is set using the timer function, and if the 1st time - 5th time is bigger than the time allowed for that floodtype then it raises the flood event

    i get the error on line 174, when i try to add user #2 (the first user is put in users(1))

    it doesnt error in the initialise when i redim it 1 to 1 and then redim the values, but for some reason it errors there

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    England
    Posts
    18
    bump :|

  9. #9
    New Member
    Join Date
    Dec 2023
    Posts
    5

    Resolved Re: Run Time Error: "10"

    I come from the future to solve this issue. To reproduce it, put this code in a class:

    Code:
    Private Type Item
        a As Long
        b As String
    End Type
    
    Private Items() As Item
    Private nItems As Long
    
    Private Function AddItem() As Long
        If nItems >= UBound(Items) Then
            ReDim Preserve Items(UBound(Items) + 1)
        End If
        nItems = nItems + 1
        AddItem = nItems - 1
    End Function
    
    Public Sub Create()
        ReDim Items(1)
        nItems = 0
    End Sub
    
    Public Sub Test()
    Dim Index As Long
        Do
            Index = AddItem()
            With Items(Index)
                .a = Index
                .b = CStr(Index)
                'AddItem
                Exit Do
            End With
        Loop Until nItems > 1000
        'AddItem
    End Sub
    Put this code in a class, create an instance of that class and call the Create() and Test() methods:

    Code:
    Sub Main()
    Dim Obj As cError10
        Set Obj = New cError10
        Obj.Create
        Obj.Test
    End Sub
    With the code as-is, everything works correctly. Uncomment either of the stray calls to AddItem(), and the code will fail with a runtime 10 at the ReDim inside AddItem().

    The first one is completely expected: if ReDim Preserve has to grow the array, it must reallocate it, possibly moving to a different memory location. Which it can't if it's being held by the reference in the With block. I guess this is what's happening in the OP's code: the explicit call to AddUser() is outside the With block, but inside it, it raises some events, and maybe the even receptor is doing something that in turn results in a call to AddUser(). We'd need a call stack to verify it (Ctrl+L in VB6's IDE), but it's plausible.

    The second one is what had my head scratching (and what made me find this old thread). Note that it is outside the With block. Not only outside, but after it. Also note that the With block is inside a loop, and that I'm exiting the loop from inside the With. If I move the Exit Do outside the With, it works as intended. It seems a bug in VB's code generation: if you jump out of a loop from inside a With block, the With frame won't get cleaned from the stack, and the variable will stay blocked until the end of the procedure or function.

    In short: to trigger this you need a loop, inside it a With block which references an array, inside it a sentence which exits the loop, and outside the loop a ReDim Preserve on the array. Each of these elements is needed. If you remove either, everything works fine.

    By the way, I use to write the ReDim Preserve this way:

    Code:
        ReDim Preserve Items(2 * UBound(Items))
    I feel this, along with reasonable default sizes for the arrays, greatly reduce the need for resizing, which is an expensive operation, as it involves copying all the array's contents to the new memory location. Also, it wastes some memory, but not the same as if we pre-allocated a big array for a worts-case scenario. I think this meets a nice middle ground.

    Mystery solved. It only took 22 years .

  10. #10
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,872

    Talking Re: Run Time Error: "10"

    It's not really a mystery that an array of UDTs gets locked inside a "With" block. The "End With" instruction unlocks the array (SafeArrayUnlock) so if you skip it then the array stays locked.

  11. #11
    New Member
    Join Date
    Dec 2023
    Posts
    5

    Re: Run Time Error: "10"

    Right, that's what I suspected at first in my code. But even if in this class I'm making extensive use of With for readability, I had been careful not to do anything inside a With block which messed with the array. Double checking on this took me some time (it's a text parser-renderer class, with more than 1800 lines of code), but it confirmed that I wasn't doing anything wrong. I then wrote a small test program (from which the code in my post is taken), and it worked perfectly. It took me some time fiddling with the original code, commenting and refactoring code, to pinpoint the Exit Do as the culprit. I added it to the test program, and it started failing.

    IMHO that breaks the specification. Any Exit is supposed to clean up all stack frames up the one referenced, freeing any used resources. Including, of course, unlocking the array.

  12. #12
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,872

    Re: Run Time Error: "10"

    You might want to rethink your logic anyway, doing "Redim Preserve" on an array of over a thousand elements is terribly inefficient...

  13. #13
    New Member
    Join Date
    Dec 2023
    Posts
    5

    Re: Run Time Error: "10"

    Yes, it is inefficient. But it is the only way if you don't know the dataset size beforehand and have to be memory and CPU efficient with small datasets. Even C provides the realloc() library function, with the same implications and problems as ReDim Preserve, for the exact same reasons. A two-tier array (an array of arrays, with the second level reserved on demand) would be faster on create, but it will be slower on read (check vartype and size of the first array, get the element, lock the second array, check vartype and size, and finally read). In most algorithms it's safe to assume that you will read the structure more often than you write to it, so you may perfectly end being slower.

    In fact, I always profile my programs extensively (I even have a timing class based on high performance counters), and I have found that most times this strategy works. That's also why the class I'm working on is so big: I'm not a fan of large code files, but in this case, performance is paramount (it's the difference between showing the text instantly vs. watching it draw line by line), and storing all the elements (paragraphs, lines, words and tags) in class-level arrays of structures instead of independent objects makes it a lot faster. The Words array is the most critical: it can be a few dozen items if the text consists of just a paragraph, or thousands (or even tens of thousands!) if I'm displaying a long text. Tags, on the other hand, aren't as critical: long texts tend to have a lower tag per word ratio than shorter ones, because they tend to be large blocks of uniform text with the occasional emphasis.

    In this case, if it is a bit slower on loading/parsing, which is done only once, and faster on display, which with scroll and window redraws can be done quite frequently, it will not only be faster and more CPU efficient overall, but it will also feel snappier.

    Edit: the ReDim (2 * Ubound()) trick also greatly reduces the number of reallocs (only ~12 for 5,000 items, for example), at the cost of some memory inefficiency. Which, with today's systems, I think it's a reasonable tradeoff - we are not running our apps in 8 megs of RAM anymore. If you analyze it, you end up not copying more memory than 2x the memory used by the largest dataset (i.e., you copy at most 8,191 elements if you need to alloc 5,000 items), which I find reasonable. If you start with a reasonably sized array, you end ReDimming far less: in the previous case, starting with a 100 element array, you only need to double it 6 times (to 6,400 elements) to hold 5,000 items.
    Last edited by ajgelado; Aug 5th, 2024 at 08:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width